Skip to content

Commit 94fff73

Browse files
authored
Setup rubocop and og (#124)
* drop ruby < 2.5 * setup rubocop for linting and fix issues automatically * add og scraper
1 parent 0ff883b commit 94fff73

21 files changed

Lines changed: 602 additions & 564 deletions

.rubocop.yml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
AllCops:
2+
DisabledByDefault: false
3+
NewCops: enable
4+
TargetRubyVersion: 2.5
5+
6+
Layout/LineLength:
7+
Enabled: false
8+
9+
Lint/MixedRegexpCaptureTypes:
10+
Enabled: false
11+
Lint/RedundantCopDisableDirective:
12+
Enabled: false
13+
Lint/RedundantSafeNavigation:
14+
Enabled: false
15+
16+
17+
Metrics/AbcSize:
18+
Enabled: false
19+
Metrics/BlockLength:
20+
Enabled: false
21+
Metrics/ClassLength:
22+
Enabled: false
23+
Metrics/MethodLength:
24+
Enabled: false
25+
Metrics/ParameterLists:
26+
Enabled: false
27+
Metrics/CyclomaticComplexity:
28+
Enabled: false
29+
Metrics/PerceivedComplexity:
30+
Enabled: false
31+
32+
Naming/AccessorMethodName:
33+
Enabled: false
34+
35+
Security/Open:
36+
Enabled: false
37+
38+
Style/AccessorGrouping:
39+
Enabled: false
40+
Style/Documentation:
41+
Enabled: false
42+
Style/DoubleCopDisableDirective:
43+
Enabled: false
44+
Style/FrozenStringLiteralComment:
45+
Enabled: false

.travis.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ language: ruby
22
before_install:
33
- gem update bundler
44
rvm:
5-
- 2.2
6-
- 2.3
7-
- 2.4
85
- 2.5
96
- 2.6
7+
- 2.7
8+
script:
9+
- bundle exec rake rubocop
10+
- bundle exec rake test

Gemfile

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
source "https://rubygems.org/"
1+
source 'https://rubygems.org/'
22

33
gemspec
44

5+
group :dev do
6+
gem 'rubocop', require: false
7+
end
8+
59
group :test do
6-
gem "faraday"
7-
gem "rack"
8-
gem "simplecov"
9-
gem "yard"
10+
gem 'faraday'
11+
gem 'rack'
12+
gem 'yard'
1013
end

README.md

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,7 @@ You can sign up for a Stream account at https://getstream.io/get_started.
1010

1111
#### Ruby version requirements and support
1212

13-
This API Client project requires Ruby 2.2.10 at a minimum. We will support the following versions:
14-
15-
- 2.2.10
16-
- 2.3.8
17-
- 2.4.5
18-
- 2.5.5
19-
- 2.6.2
13+
This API Client project requires Ruby 2.5.x at a minimum.
2014

2115
See the [Travis configuration](.travis.yml) for details of how it is built and tested.
2216

@@ -81,7 +75,7 @@ client.update_activities([activity_data])
8175
user_feed_1.remove_activity('e561de8f-00f1-11e4-b400-0cc47a024be0')
8276

8377
# Remove activities by their foreign_id
84-
user_feed_1.remove_activity('tweet:1', foreign_id = true)
78+
user_feed_1.remove_activity('tweet:1', foreign_id: true)
8579

8680
# Follow another feed
8781
user_feed_1.follow('flat', '42')
@@ -152,6 +146,9 @@ activity = {
152146
actor: 'User:2', verb: 'pin', object: 'Place:42', target: 'Board:1'
153147
}
154148
client.add_to_many(activity, feeds)
149+
150+
# Retrive open graph information
151+
client.og('https://google.com')
155152
```
156153

157154
### Copyright and License Information

Rakefile

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1-
require "bundler/gem_tasks"
2-
require "rspec/core/rake_task"
1+
require 'bundler/gem_tasks'
2+
require 'rspec/core/rake_task'
33

44
RSpec::Core::RakeTask.new
55

6-
task :default => [:spec]
7-
task :test => [:spec]
6+
require 'rubocop/rake_task'
7+
RuboCop::RakeTask.new(:rubocop) do |t|
8+
t.options = ['--display-cop-names']
9+
end
10+
11+
task default: [:spec]
12+
task test: [:spec]

lib/stream/activities.rb

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
module Stream
22
module Activities
3-
43
#
54
# Get activities directly, via ID or Foreign ID + timestamp
65
#
@@ -28,13 +27,13 @@ def get_activities(params = {})
2827
if params[:foreign_id_times]
2928
foreign_ids = []
3029
timestamps = []
31-
params[:foreign_id_times].each{|e|
30+
params[:foreign_id_times].each do |e|
3231
foreign_ids << e[:foreign_id]
3332
timestamps << e[:time]
34-
}
33+
end
3534
params = {
3635
foreign_ids: foreign_ids,
37-
timestamps: timestamps,
36+
timestamps: timestamps
3837
}
3938
end
4039
signature = Stream::Signer.create_jwt_token('activities', '*', @api_secret, '*')
@@ -142,8 +141,8 @@ def activity_partial_update(data = {})
142141
# ])
143142
#
144143
def batch_activity_partial_update(changes = [])
145-
signature = Stream::Signer.create_jwt_token('activities', '*', @api_secret, '*')
146-
make_request(:post, '/activity/', signature, {}, {:changes => changes})
144+
signature = Stream::Signer.create_jwt_token('activities', '*', @api_secret, '*')
145+
make_request(:post, '/activity/', signature, {}, { changes: changes })
147146
end
148147
end
149148
end

lib/stream/batch.rb

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ module Batch
1616
#
1717
def follow_many(follows, activity_copy_limit = nil)
1818
query_params = {}
19-
unless activity_copy_limit.nil?
20-
query_params['activity_copy_limit'] = activity_copy_limit
21-
end
19+
query_params['activity_copy_limit'] = activity_copy_limit unless activity_copy_limit.nil?
2220
signature = Stream::Signer.create_jwt_token('follower', '*', @api_secret, '*')
2321
make_request(:post, '/follow_many/', signature, query_params, follows)
2422
end
@@ -52,8 +50,8 @@ def unfollow_many(unfollows)
5250
#
5351
def add_to_many(activity_data, feeds)
5452
data = {
55-
:feeds => feeds,
56-
:activity => activity_data
53+
feeds: feeds,
54+
activity: activity_data
5755
}
5856
signature = Stream::Signer.create_jwt_token('feed', '*', @api_secret, '*')
5957
make_request(:post, '/feed/add_to_many/', signature, {}, data)

lib/stream/client.rb

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
require 'stream/url'
66

77
module Stream
8-
STREAM_URL_COM_RE = %r{https\:\/\/(?<key>\w+)\:(?<secret>\w+)@((api\.)|((?<location>[-\w]+)\.))?(?<api_hostname>stream-io-api\.com)\/[\w=-\?%&]+app_id=(?<app_id>\d+)}i
9-
STREAM_URL_IO_RE = %r{https\:\/\/(?<key>\w+)\:(?<secret>\w+)@((api\.)|((?<location>[-\w]+)\.))?(?<api_hostname>getstream\.io)\/[\w=-\?%&]+app_id=(?<app_id>\d+)}i
8+
STREAM_URL_COM_RE = %r{https://(?<key>\w+):(?<secret>\w+)@((api\.)|((?<location>[-\w]+)\.))?(?<api_hostname>stream-io-api\.com)/[\w=-?%&]+app_id=(?<app_id>\d+)}i.freeze
9+
STREAM_URL_IO_RE = %r{https://(?<key>\w+):(?<secret>\w+)@((api\.)|((?<location>[-\w]+)\.))?(?<api_hostname>getstream\.io)/[\w=-?%&]+app_id=(?<app_id>\d+)}i.freeze
1010

1111
class Client
1212
attr_reader :api_key
@@ -36,7 +36,6 @@ class Client
3636
# Stream::Client.new('my_key', 'my_secret', 'my_app_id', :location => 'us-east')
3737
#
3838
def initialize(api_key = '', api_secret = '', app_id = nil, opts = {})
39-
4039
if api_key.nil? || api_key.empty?
4140
env_url = ENV['STREAM_URL']
4241
if env_url =~ Stream::STREAM_URL_COM_RE
@@ -100,7 +99,7 @@ def create_user_session_token(user_id, extra_data = {})
10099
# @return [string]
101100
#
102101
def create_user_token(user_id, extra_data = {})
103-
return Stream::Signer.create_user_token(user_id, extra_data, api_secret)
102+
Stream::Signer.create_user_token(user_id, extra_data, api_secret)
104103
end
105104

106105
def personalization
@@ -128,16 +127,21 @@ def update_activities(activities)
128127
make_request(:post, '/activities/', auth_token, {}, 'activities' => activities)
129128
end
130129

130+
def og(uri)
131+
auth_token = Stream::Signer.create_jwt_token('*', '*', @api_secret, '*')
132+
make_request(:get, '/og', auth_token, { url: uri })
133+
end
134+
131135
def get_default_params
132-
{:api_key => @api_key}
136+
{ api_key: @api_key }
133137
end
134138

135139
def get_http_client
136-
@http_client ||= StreamHTTPClient.new(url_generator)
140+
@get_http_client ||= StreamHTTPClient.new(url_generator)
137141
end
138142

139143
def make_query_params(params)
140-
Hash[get_default_params.merge(params).sort_by {|k, v| k.to_s}]
144+
Hash[get_default_params.merge(params).sort_by { |k, _v| k.to_s }]
141145
end
142146

143147
def make_request(method, relative_url, signature, params = {}, data = {}, headers = {})
@@ -187,7 +191,7 @@ def make_http_request(method, relative_url, params = nil, data = nil, headers =
187191

188192
case response[:status].to_i
189193
when 200..203
190-
return ::JSON.parse(response[:body])
194+
::JSON.parse(response[:body])
191195
end
192196
end
193197
end
@@ -196,16 +200,16 @@ class RaiseHttpException < Faraday::Middleware
196200
def call(env)
197201
@app.call(env).on_complete do |response|
198202
case response[:status].to_i
199-
when 200..203
200-
return response
201-
when 401
202-
raise StreamApiResponseException, error_message(response, 'Bad feed')
203-
when 403
204-
raise StreamApiResponseException, error_message(response, 'Bad auth/headers')
205-
when 404
206-
raise StreamApiResponseException, error_message(response, 'url not found')
207-
when 204...600
208-
raise StreamApiResponseException, error_message(response, _build_error_message(response.body))
203+
when 200..203
204+
return response
205+
when 401
206+
raise StreamApiResponseException, error_message(response, 'Bad feed')
207+
when 403
208+
raise StreamApiResponseException, error_message(response, 'Bad auth/headers')
209+
when 404
210+
raise StreamApiResponseException, error_message(response, 'url not found')
211+
when 204...600
212+
raise StreamApiResponseException, error_message(response, _build_error_message(response.body))
209213
end
210214
end
211215
end
@@ -229,7 +233,7 @@ def _build_error_message(response)
229233
end
230234

231235
def error_message(response, body = nil)
232-
"#{response[:method].to_s.upcase} #{response[:url]}: #{[response[:status].to_s + ':', body].compact.join(' ')}"
236+
"#{response[:method].to_s.upcase} #{response[:url]}: #{["#{response[:status]}:", body].compact.join(' ')}"
233237
end
234238
end
235239
end

lib/stream/collections.rb

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,28 @@ def add(collection_name, collection_data, id: nil, user_id: nil)
44
data = {
55
id: id,
66
user_id: user_id,
7-
data: collection_data,
7+
data: collection_data
88
}
99
uri = "/collections/#{collection_name}/"
10-
make_collection_request(:post, {}, data, :endpoint => uri)
10+
make_collection_request(:post, {}, data, endpoint: uri)
1111
end
1212

1313
def get(collection_name, id)
1414
uri = "collections/#{collection_name}/#{id}/"
15-
make_collection_request(:get, {}, {}, :endpoint => uri)
15+
make_collection_request(:get, {}, {}, endpoint: uri)
1616
end
1717

1818
def update(collection_name, id, data: nil)
1919
data = {
2020
data: data
2121
}
2222
uri = "collections/#{collection_name}/#{id}/"
23-
make_collection_request(:put, {}, data, :endpoint => uri)
23+
make_collection_request(:put, {}, data, endpoint: uri)
2424
end
2525

2626
def delete(collection_name, id)
2727
uri = "collections/#{collection_name}/#{id}/"
28-
make_collection_request(:delete, {}, {}, :endpoint => uri)
28+
make_collection_request(:delete, {}, {}, endpoint: uri)
2929
end
3030

3131
def upsert(collection, objects = [])
@@ -53,11 +53,9 @@ def delete_many(collection, ids = [])
5353
end
5454

5555
def create_reference(collection, id)
56-
_id = id
57-
if id.respond_to?(:keys) and !id["id"].nil?
58-
_id = id["id"]
59-
end
60-
"SO:#{collection}:#{_id}"
56+
k = id
57+
k = id['id'] if id.respond_to?(:keys) && !id['id'].nil?
58+
"SO:#{collection}:#{k}"
6159
end
6260

6361
private

0 commit comments

Comments
 (0)