stream-ruby 2.5.10 → 2.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a3b58bcc7cb83f9424b45eb1e268e476d7f05717
4
- data.tar.gz: a3d1ebe35bb26a98b51704acb074e3500d1d4863
3
+ metadata.gz: 499ca2934dbfc39668a3d3095aa6a1f616138d9d
4
+ data.tar.gz: 61ec2335f9983b4f111a873a6db1ea816324e28b
5
5
  SHA512:
6
- metadata.gz: 93974c1a54ed500e6503632142f350c9381cbc77655566ce459b2f5f8acf63401b570135b78434a6b6945ffce3c601abfe6b85328c2dd5f366e6524e78d147fe
7
- data.tar.gz: 114e765ba286365dea5aa1ef3611c85c7775904909bd64c3db5b5a98ced47650f6e89d142ebdd45261896583def6aa5eff31f9e1882bf522d7aae274d36377a9
6
+ metadata.gz: 7436d395e632dc67481c8e1f49d6011cac64ea4a7ef506b43cd022fa5170774c57ae52ba7635c254c2cbf28ea146d16c689577233fc518f3ee72f1c6e310da5c
7
+ data.tar.gz: 238c2e10cd26f1248cd2fe88d2b58668e440e08986d9a83c080aa44f7cb7378ddbceaa59576f862e5d68a875982a22e4c54dcaeb959389c2ac22dd484b7dadce
data/README.md CHANGED
@@ -16,6 +16,7 @@ This API Client project requires Ruby 2.2.8 at a minimum. We will support the fo
16
16
  - 2.3.1
17
17
  - 2.3.5
18
18
  - 2.4.2
19
+ - 2.5.1
19
20
 
20
21
  See the [Travis configuration](.travis.yml) for details of how it is built and tested.
21
22
 
data/lib/stream/client.rb CHANGED
@@ -2,6 +2,7 @@ require 'faraday'
2
2
  require 'stream/errors'
3
3
  require 'stream/feed'
4
4
  require 'stream/signer'
5
+ require 'stream/url'
5
6
 
6
7
  module Stream
7
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
@@ -16,6 +17,8 @@ module Stream
16
17
  if RUBY_VERSION.to_f >= 2.1
17
18
  require 'stream/batch'
18
19
  require 'stream/signedrequest'
20
+ require 'stream/personalization'
21
+ require 'stream/collections'
19
22
 
20
23
  include Stream::SignedRequest
21
24
  include Stream::Batch
@@ -33,15 +36,16 @@ module Stream
33
36
  # Stream::Client.new('my_key', 'my_secret', 'my_app_id', :location => 'us-east')
34
37
  #
35
38
  def initialize(api_key = '', api_secret = '', app_id = nil, opts = {})
36
- if ENV['STREAM_URL'] =~ Stream::STREAM_URL_COM_RE && (api_key.nil? || api_key.empty?)
37
- matches = Stream::STREAM_URL_COM_RE.match(ENV['STREAM_URL'])
38
- api_key = matches['key']
39
- api_secret = matches['secret']
40
- app_id = matches['app_id']
41
- opts[:location] = matches['location']
42
- opts[:api_hostname] = matches['api_hostname']
43
- elsif ENV['STREAM_URL'] =~ Stream::STREAM_URL_IO_RE && (api_key.nil? || api_key.empty?)
44
- matches = Stream::STREAM_URL_IO_RE.match(ENV['STREAM_URL'])
39
+ if api_key.nil? || api_key.empty?
40
+ env_url = ENV['STREAM_URL']
41
+ if env_url =~ Stream::STREAM_URL_COM_RE
42
+ re = Stream::STREAM_URL_COM_RE
43
+ elsif env_url =~ Stream::STREAM_URL_IO_RE
44
+ re = Stream::STREAM_URL_IO_RE
45
+ end
46
+ raise ArgumentError, 'empty api_key parameter and missing or invalid STREAM_URL env variable' unless re
47
+
48
+ matches = re.match(ENV['STREAM_URL'])
45
49
  api_key = matches['key']
46
50
  api_secret = matches['secret']
47
51
  app_id = matches['app_id']
@@ -49,21 +53,17 @@ module Stream
49
53
  opts[:api_hostname] = matches['api_hostname']
50
54
  end
51
55
 
52
- if api_key.nil? || api_key.empty?
53
- raise ArgumentError, 'empty api_key parameter and missing or invalid STREAM_URL env variable'
54
- end
55
-
56
56
  @api_key = api_key
57
57
  @api_secret = api_secret
58
58
  @app_id = app_id
59
59
  @signer = Stream::Signer.new(api_secret)
60
60
 
61
61
  @client_options = {
62
- api_version: opts.fetch(:api_version, 'v1.0'),
63
- location: opts.fetch(:location, nil),
64
- default_timeout: opts.fetch(:default_timeout, 3),
65
- api_key: @api_key,
66
- api_hostname: opts.fetch(:api_hostname, 'stream-io-api.com')
62
+ api_version: opts.fetch(:api_version, 'v1.0'),
63
+ location: opts.fetch(:location, nil),
64
+ default_timeout: opts.fetch(:default_timeout, 3),
65
+ api_key: @api_key,
66
+ api_hostname: opts.fetch(:api_hostname, 'stream-io-api.com')
67
67
  }
68
68
  end
69
69
 
@@ -79,6 +79,14 @@ module Stream
79
79
  Stream::Feed.new(self, feed_slug, user_id, token)
80
80
  end
81
81
 
82
+ def personalization
83
+ PersonalizationClient.new(api_key, api_secret, app_id, client_options)
84
+ end
85
+
86
+ def collections
87
+ CollectionsClient.new(api_key, api_secret, app_id, client_options)
88
+ end
89
+
82
90
  def update_activity(activity)
83
91
  update_activities([activity])
84
92
  end
@@ -93,7 +101,7 @@ module Stream
93
101
  end
94
102
 
95
103
  def get_http_client
96
- @http_client ||= StreamHTTPClient.new(@client_options)
104
+ @http_client ||= StreamHTTPClient.new(url_generator)
97
105
  end
98
106
 
99
107
  def make_query_params(params)
@@ -106,6 +114,12 @@ module Stream
106
114
 
107
115
  get_http_client.make_http_request(method, relative_url, make_query_params(params), data, headers)
108
116
  end
117
+
118
+ private
119
+
120
+ def url_generator
121
+ APIURLGenerator.new(@client_options)
122
+ end
109
123
  end
110
124
 
111
125
  class StreamHTTPClient
@@ -115,59 +129,34 @@ module Stream
115
129
  attr_reader :options
116
130
  attr_reader :base_path
117
131
 
118
- def initialize(client_params)
119
- @options = client_params
120
- location_name = 'api'
121
- unless client_params[:location].nil?
122
- location_name = "#{client_params[:location]}-api"
123
- end
124
-
125
- protocol = 'https'
126
- port = ':443'
127
- if @options[:location] == 'qa' || @options[:location] == 'localhost'
128
- protocol = 'http'
129
- port = ':80'
130
- if @options[:location] == 'localhost'
131
- port = ':8000'
132
- end
133
- end
134
-
135
- api_hostname = 'stream-io-api.com'
136
- if @options[:api_hostname]
137
- api_hostname = @options[:api_hostname]
138
- end
139
-
140
- @base_path = "/api/#{@options[:api_version]}"
141
- base_url = "#{protocol}://#{location_name}.#{api_hostname}#{port}#{@base_path}"
142
-
143
- @conn = Faraday.new(:url => base_url) do |faraday|
144
- # faraday.request :url_encoded
132
+ def initialize(url_generator)
133
+ @options = url_generator.options
134
+ @conn = Faraday.new(url: url_generator.url) do |faraday|
145
135
  faraday.use RaiseHttpException
146
136
  faraday.options[:open_timeout] = @options[:default_timeout]
147
137
  faraday.options[:timeout] = @options[:default_timeout]
148
-
149
- # do this last
150
138
  faraday.adapter Faraday.default_adapter
151
139
  end
152
- @conn.path_prefix = @base_path
140
+ @base_path = url_generator.base_path
141
+ @conn.path_prefix = base_path
153
142
  end
154
143
 
155
144
  def make_http_request(method, relative_url, params = nil, data = nil, headers = nil)
156
145
  headers['Content-Type'] = 'application/json'
157
146
  headers['X-Stream-Client'] = "stream-ruby-client-#{Stream::VERSION}"
158
- params['api_key'] = @options[:api_key]
159
- relative_url = "#{@base_path}#{relative_url}?#{URI.encode_www_form(params)}"
160
- body = data.to_json if %w(post put).include? method.to_s
147
+ base_url = [base_path, relative_url].join('/').gsub(%r{/+}, '/')
148
+ url = "#{base_url}?#{URI.encode_www_form(params)}"
149
+ body = data.to_json if %w[post put].include? method.to_s
161
150
  response = @conn.run_request(
162
- method,
163
- relative_url,
164
- body,
165
- headers
151
+ method,
152
+ url,
153
+ body,
154
+ headers
166
155
  )
167
156
 
168
157
  case response[:status].to_i
169
- when 200..203
170
- return ::JSON.parse(response[:body])
158
+ when 200..203
159
+ return ::JSON.parse(response[:body])
171
160
  end
172
161
  end
173
162
  end
@@ -0,0 +1,35 @@
1
+ module Stream
2
+ class CollectionsClient < Client
3
+ def upsert(collection, objects = [])
4
+ data = {
5
+ data: {
6
+ collection => objects
7
+ }
8
+ }
9
+ make_collection_request(:post, {}, data)
10
+ end
11
+
12
+ def get(collection, ids = [])
13
+ params = {
14
+ foreign_ids: ids.map { |id| "#{collection}:#{id}" }.join(',')
15
+ }
16
+ make_collection_request(:get, params, {})
17
+ end
18
+
19
+ def delete(collection, ids = [])
20
+ params = {
21
+ collection_name: collection,
22
+ ids: ids.join(',')
23
+ }
24
+ make_collection_request(:delete, params, {})
25
+ end
26
+
27
+ private
28
+
29
+ def make_collection_request(method, params, data)
30
+ endpoint = '/meta/'
31
+ auth_token = Stream::Signer.create_jwt_token('collections', '*', @api_secret, '*', '*')
32
+ make_request(method, endpoint, auth_token, params, data)
33
+ end
34
+ end
35
+ end
data/lib/stream/feed.rb CHANGED
@@ -101,13 +101,6 @@ module Stream
101
101
  @client.make_request(:post, '/activities/', auth_token, {}, 'activities' => activities)
102
102
  end
103
103
 
104
- def delete
105
- uri = "/feed/#{@feed_url}/"
106
- auth_token = create_jwt_token('feed', 'delete')
107
-
108
- @client.make_request(:delete, uri, auth_token)
109
- end
110
-
111
104
  def follow(target_feed_slug, target_user_id, activity_copy_limit = 300)
112
105
  uri = "/feed/#{@feed_url}/follows/"
113
106
  activity_copy_limit = 0 if activity_copy_limit < 0
@@ -0,0 +1,29 @@
1
+ require 'stream/url'
2
+
3
+ module Stream
4
+ class PersonalizationClient < Client
5
+ def url_generator
6
+ PersonalizationURLGenerator.new(@client_options)
7
+ end
8
+
9
+ def get(resource, params = {})
10
+ make_personalization_request(:get, resource, params, {})
11
+ end
12
+
13
+ def post(resource, params = {}, data = {})
14
+ make_personalization_request(:post, resource, params, data: data)
15
+ end
16
+
17
+ def delete(resource, params = {})
18
+ make_personalization_request(:delete, resource, params, {})
19
+ end
20
+
21
+ private
22
+
23
+ def make_personalization_request(method, resource, params, data)
24
+ endpoint = "/#{resource}/"
25
+ auth_token = Stream::Signer.create_jwt_token('personalization', '*', @api_secret, '*', '*')
26
+ make_request(method, endpoint, auth_token, params, data)
27
+ end
28
+ end
29
+ end
@@ -18,7 +18,7 @@ module Stream
18
18
  context = HttpSignatures::Context.new(
19
19
  keys: {@api_key => @api_secret},
20
20
  algorithm: 'hmac-sha256',
21
- headers: %w((request-target) Date)
21
+ headers: %w(date)
22
22
  )
23
23
  method_map = {
24
24
  :get => Net::HTTP::Get,
@@ -29,7 +29,7 @@ module Stream
29
29
  request_date = Time.now.rfc822
30
30
  message = method_map[method].new(
31
31
  "#{get_http_client.base_path}#{relative_url}?#{URI.encode_www_form(query_params)}",
32
- 'Date' => request_date
32
+ 'date' => request_date
33
33
  )
34
34
  context.signer.sign(message)
35
35
  headers = {
data/lib/stream/url.rb ADDED
@@ -0,0 +1,57 @@
1
+ module Stream
2
+ class URLGenerator
3
+ attr_reader :options
4
+ attr_reader :base_path
5
+ attr_reader :url
6
+ end
7
+
8
+ class APIURLGenerator < URLGenerator
9
+ def initialize(options)
10
+ @options = options
11
+ location = make_location(options[:location])
12
+ location ||= "api"
13
+ api_version = options[:api_version] ? options[:api_version] : 'v1.0'
14
+ if ENV['STREAM_URL']
15
+ uri = URI.parse(ENV['STREAM_URL'])
16
+ scheme = uri.scheme
17
+ host = uri.host
18
+ port = uri.port
19
+ else
20
+ scheme = 'https'
21
+ host = options[:api_hostname]
22
+ port = 443
23
+ end
24
+ unless ENV['STREAM_URL'] =~ /localhost/
25
+ host_parts = host.split('.')
26
+ host = host_parts.slice(1..-1).join('.') if host_parts.length == 3
27
+ host = "#{location}.#{host}" if location
28
+ end
29
+ @base_path = "/api/#{api_version}"
30
+ @url = "#{scheme}://#{host}:#{port}#{@base_path}"
31
+ end
32
+
33
+ private
34
+
35
+ def make_location(loc)
36
+ case loc
37
+ when 'us-east'
38
+ 'us-east-api'
39
+ when 'eu-west'
40
+ 'eu-west-api'
41
+ when 'singapore'
42
+ 'singapore-api'
43
+ else
44
+ loc
45
+ end
46
+ end
47
+ end
48
+
49
+ class PersonalizationURLGenerator < URLGenerator
50
+ def initialize(options)
51
+ @options = options
52
+ host = 'personalization.stream-io-api.com'
53
+ @base_path = '/personalization/v1.0'
54
+ @url = "https://#{host}#{@base_path}"
55
+ end
56
+ end
57
+ end
@@ -1,3 +1,3 @@
1
1
  module Stream
2
- VERSION = '2.5.10'.freeze
2
+ VERSION = '2.6.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,15 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stream-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.5.10
4
+ version: 2.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tommaso Barbugli
8
8
  - Ian Douglas
9
+ - Federico Ruggi
9
10
  autorequire:
10
11
  bindir: bin
11
12
  cert_chain: []
12
- date: 2017-12-01 00:00:00.000000000 Z
13
+ date: 2018-04-17 00:00:00.000000000 Z
13
14
  dependencies:
14
15
  - !ruby/object:Gem::Dependency
15
16
  name: faraday
@@ -115,11 +116,14 @@ files:
115
116
  - lib/stream/base.rb
116
117
  - lib/stream/batch.rb
117
118
  - lib/stream/client.rb
119
+ - lib/stream/collections.rb
118
120
  - lib/stream/errors.rb
119
121
  - lib/stream/exceptions.rb
120
122
  - lib/stream/feed.rb
123
+ - lib/stream/personalization.rb
121
124
  - lib/stream/signedrequest.rb
122
125
  - lib/stream/signer.rb
126
+ - lib/stream/url.rb
123
127
  - lib/stream/version.rb
124
128
  homepage: http://github.com/GetStream/stream-ruby
125
129
  licenses:
@@ -141,7 +145,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
141
145
  version: '0'
142
146
  requirements: []
143
147
  rubyforge_project:
144
- rubygems_version: 2.6.13
148
+ rubygems_version: 2.5.2
145
149
  signing_key:
146
150
  specification_version: 4
147
151
  summary: A gem that provides a client interface for getstream.io