stream-ruby 2.11.0 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f26943443ea8de097cbb217a65dece67cca94aba
4
- data.tar.gz: a04acc92c85377c27e9a9717200296fa7619f99d
3
+ metadata.gz: 0a849b72e60913e35cf4421a4fa2a0b21868e4c6
4
+ data.tar.gz: 2e4c8ede31bfdc3c52399314e85ec91b9bb56d0c
5
5
  SHA512:
6
- metadata.gz: 50c56c98e7495420010c69d3d0c0b343c3ab831dd72f519e4222f3018f3d1fe4a6e4230f68f71f46b683401babd3ec38d60722ef28d29d0c9c3ef0e176d1f03d
7
- data.tar.gz: a4d9c7ec93c0cd17f1e99e12fc5f36d347c306aca672deec071ffd3abebc219b337bc0c96f039cd6c308f5f0c45e54003ad6095f74b0ea12282473d50ed7da8d
6
+ metadata.gz: 0d0888b3107241fb182bc974aed6ee4ac3a3592d45af7d0c19c6704b703f31c157364398b1297302b2ebdd212694f8e96f428e98122c2ab2d5b494e2c25d901b
7
+ data.tar.gz: ce762dc8de16554e74476bf57b5722127c40ab665e58b27ad711f3f3e453f423ed2b81c17008f8b746558edf36052ab48cc7e5ccd88a915d3561b4b7d395f705
data/README.md CHANGED
@@ -11,12 +11,11 @@ You can sign up for a Stream account at https://getstream.io/get_started.
11
11
 
12
12
  #### Ruby version requirements and support
13
13
 
14
- This API Client project requires Ruby 2.2.8 at a minimum. We will support the following versions:
15
- - 2.2.8
16
- - 2.3.1
17
- - 2.3.5
18
- - 2.4.2
19
- - 2.5.1
14
+ This API Client project requires Ruby 2.2.10 at a minimum. We will support the following versions:
15
+ - 2.2.10
16
+ - 2.3.8
17
+ - 2.4.5
18
+ - 2.5.3
20
19
 
21
20
  See the [Travis configuration](.travis.yml) for details of how it is built and tested.
22
21
 
@@ -156,6 +155,6 @@ client.add_to_many(activity, feeds)
156
155
 
157
156
  ### Copyright and License Information
158
157
 
159
- Copyright (c) 2014-2017 Stream.io Inc, and individual contributors. All rights reserved.
158
+ Copyright (c) 2014-2018 Stream.io Inc, and individual contributors. All rights reserved.
160
159
 
161
160
  See the file "LICENSE" for information on the history of this software, terms & conditions for usage, and a DISCLAIMER OF ALL WARRANTIES.
@@ -21,7 +21,8 @@ module Stream
21
21
  unless activity_copy_limit.nil?
22
22
  query_params['activity_copy_limit'] = activity_copy_limit
23
23
  end
24
- make_signed_request(:post, '/follow_many/', query_params, follows)
24
+ signature = Stream::Signer.create_jwt_token('follower', '*', @api_secret, '*')
25
+ make_request(:post, '/follow_many/', signature, query_params, follows)
25
26
  end
26
27
 
27
28
  #
@@ -41,9 +42,10 @@ module Stream
41
42
  # @client.unfollow_many(unfollows)
42
43
  #
43
44
  def unfollow_many(unfollows)
44
- make_signed_request(:post, '/unfollow_many/', {}, unfollows)
45
+ signature = Stream::Signer.create_jwt_token('follower', '*', @api_secret, '*')
46
+ make_request(:post, '/unfollow_many/', signature, {}, unfollows)
45
47
  end
46
-
48
+
47
49
  #
48
50
  # Adds an activity to many feeds in one single request
49
51
  #
@@ -57,7 +59,8 @@ module Stream
57
59
  :feeds => feeds,
58
60
  :activity => activity_data
59
61
  }
60
- make_signed_request(:post, '/feed/add_to_many/', {}, data)
62
+ signature = Stream::Signer.create_jwt_token('feed', '*', @api_secret, '*')
63
+ make_request(:post, '/feed/add_to_many/', signature, {}, data)
61
64
  end
62
65
  end
63
66
  end
@@ -14,17 +14,15 @@ module Stream
14
14
  attr_reader :app_id
15
15
  attr_reader :client_options
16
16
 
17
- if RUBY_VERSION.to_f >= 2.1
18
- require 'stream/batch'
19
- require 'stream/signedrequest'
20
- require 'stream/personalization'
21
- require 'stream/collections'
22
- require 'stream/activities'
17
+ require 'stream/batch'
18
+ require 'stream/personalization'
19
+ require 'stream/collections'
20
+ require 'stream/activities'
21
+ require 'stream/reactions'
22
+ require 'stream/users'
23
23
 
24
- include Stream::SignedRequest
25
- include Stream::Batch
26
- include Stream::Activities
27
- end
24
+ include Stream::Batch
25
+ include Stream::Activities
28
26
 
29
27
  #
30
28
  # initializes a Stream API Client
@@ -38,6 +36,7 @@ module Stream
38
36
  # Stream::Client.new('my_key', 'my_secret', 'my_app_id', :location => 'us-east')
39
37
  #
40
38
  def initialize(api_key = '', api_secret = '', app_id = nil, opts = {})
39
+
41
40
  if api_key.nil? || api_key.empty?
42
41
  env_url = ENV['STREAM_URL']
43
42
  if env_url =~ Stream::STREAM_URL_COM_RE
@@ -46,7 +45,7 @@ module Stream
46
45
  re = Stream::STREAM_URL_IO_RE
47
46
  end
48
47
  raise ArgumentError, 'empty api_key parameter and missing or invalid STREAM_URL env variable' unless re
49
-
48
+
50
49
  matches = re.match(ENV['STREAM_URL'])
51
50
  api_key = matches['key']
52
51
  api_secret = matches['secret']
@@ -77,12 +76,31 @@ module Stream
77
76
  # @return [Stream::Feed]
78
77
  #
79
78
  def feed(feed_slug, user_id)
80
- token = @signer.sign(feed_slug, user_id)
81
- Stream::Feed.new(self, feed_slug, user_id, token)
79
+ Stream::Feed.new(self, feed_slug, user_id)
82
80
  end
83
81
 
82
+ # Creates a user token
83
+ #
84
+ # @deprecated Use Client#create_user_token instead
85
+ #
86
+ # @param [string] user_id the user_if of this token (e.g. User42)
87
+ # @param [hash] extra_data additional token data
88
+ #
89
+ # @return [string]
90
+ #
84
91
  def create_user_session_token(user_id, extra_data = {})
85
- return Stream::Signer.create_user_session_token(user_id, extra_data, api_secret)
92
+ create_user_token(user_id, extra_data)
93
+ end
94
+
95
+ # Creates a user token
96
+ #
97
+ # @param [string] user_id the user_if of this token (e.g. User42)
98
+ # @param [hash] extra_data additional token data
99
+ #
100
+ # @return [string]
101
+ #
102
+ def create_user_token(user_id, extra_data = {})
103
+ return Stream::Signer.create_user_token(user_id, extra_data, api_secret)
86
104
  end
87
105
 
88
106
  def personalization
@@ -93,6 +111,14 @@ module Stream
93
111
  CollectionsClient.new(api_key, api_secret, app_id, client_options)
94
112
  end
95
113
 
114
+ def reactions
115
+ ReactionsClient.new(api_key, api_secret, app_id, client_options)
116
+ end
117
+
118
+ def users
119
+ UsersClient.new(api_key, api_secret, app_id, client_options)
120
+ end
121
+
96
122
  def update_activity(activity)
97
123
  update_activities([activity])
98
124
  end
@@ -1,5 +1,33 @@
1
1
  module Stream
2
2
  class CollectionsClient < Client
3
+ def add(collection_name, collection_data, id: nil, user_id: nil)
4
+ data = {
5
+ id: id,
6
+ user_id: user_id,
7
+ data: collection_data,
8
+ }
9
+ uri = "/collections/#{collection_name}/"
10
+ make_collection_request(:post, {}, data, :endpoint => uri)
11
+ end
12
+
13
+ def get(collection_name, id)
14
+ uri = "collections/#{collection_name}/#{id}/"
15
+ make_collection_request(:get, {}, {}, :endpoint => uri)
16
+ end
17
+
18
+ def update(collection_name, id, data: nil)
19
+ data = {
20
+ data: data
21
+ }
22
+ uri = "collections/#{collection_name}/#{id}/"
23
+ make_collection_request(:put, {}, data, :endpoint => uri)
24
+ end
25
+
26
+ def delete(collection_name, id)
27
+ uri = "collections/#{collection_name}/#{id}/"
28
+ make_collection_request(:delete, {}, {}, :endpoint => uri)
29
+ end
30
+
3
31
  def upsert(collection, objects = [])
4
32
  data = {
5
33
  data: {
@@ -9,14 +37,14 @@ module Stream
9
37
  make_collection_request(:post, {}, data)
10
38
  end
11
39
 
12
- def get(collection, ids = [])
40
+ def select(collection, ids = [])
13
41
  params = {
14
42
  foreign_ids: ids.map { |id| "#{collection}:#{id}" }.join(',')
15
43
  }
16
44
  make_collection_request(:get, params, {})
17
45
  end
18
46
 
19
- def delete(collection, ids = [])
47
+ def delete_many(collection, ids = [])
20
48
  params = {
21
49
  collection_name: collection,
22
50
  ids: ids.join(',')
@@ -25,17 +53,16 @@ module Stream
25
53
  end
26
54
 
27
55
  def create_reference(collection, id)
28
- "SO:#{collection}:#{id}"
29
- end
30
-
31
- def create_user_reference(id)
32
- create_reference('user', id)
56
+ _id = id
57
+ if id.respond_to?(:keys) and !id["id"].nil?
58
+ _id = id["id"]
59
+ end
60
+ "SO:#{collection}:#{_id}"
33
61
  end
34
62
 
35
63
  private
36
64
 
37
- def make_collection_request(method, params, data)
38
- endpoint = '/meta/'
65
+ def make_collection_request(method, params, data, endpoint: '/collections/')
39
66
  auth_token = Stream::Signer.create_jwt_token('collections', '*', @api_secret, '*', '*')
40
67
  make_request(method, endpoint, auth_token, params, data)
41
68
  end
@@ -6,9 +6,8 @@ module Stream
6
6
  attr_reader :id
7
7
  attr_reader :slug
8
8
  attr_reader :user_id
9
- attr_reader :token
10
9
 
11
- def initialize(client, feed_slug, user_id, token)
10
+ def initialize(client, feed_slug, user_id)
12
11
  unless valid_feed_slug feed_slug
13
12
  raise StreamInputData, 'feed_slug can only contain alphanumeric characters plus underscores'
14
13
  end
@@ -23,7 +22,6 @@ module Stream
23
22
  @slug = feed_slug
24
23
  @feed_name = "#{feed_slug}#{user_id}"
25
24
  @feed_url = "#{feed_slug}/#{user_id}"
26
- @token = token
27
25
  end
28
26
 
29
27
  def readonly_token
@@ -39,30 +37,37 @@ module Stream
39
37
  end
40
38
 
41
39
  def get(params = {})
42
- uri = "/feed/#{@feed_url}/"
40
+ if params[:enrich] or params[:reactions]
41
+ uri = "/enrich/feed/#{@feed_url}/"
42
+ else
43
+ uri = "/feed/#{@feed_url}/"
44
+ end
43
45
  if params[:mark_read] && params[:mark_read].is_a?(Array)
44
46
  params[:mark_read] = params[:mark_read].join(',')
45
47
  end
46
48
  if params[:mark_seen] && params[:mark_seen].is_a?(Array)
47
49
  params[:mark_seen] = params[:mark_seen].join(',')
48
50
  end
49
- auth_token = create_jwt_token('feed', 'read')
51
+ if params[:reactions].respond_to?(:keys)
52
+ if params[:reactions][:own]
53
+ params[:withOwnReactions] = true
54
+ end
55
+ if params[:reactions][:recent]
56
+ params[:withRecentReactions] = true
57
+ end
58
+ if params[:reactions][:counts]
59
+ params[:withReactionCounts] = true
60
+ end
61
+ end
62
+ [:enrich, :reactions].each { |k| params.delete(k) }
50
63
 
64
+ auth_token = create_jwt_token('feed', 'read')
51
65
  @client.make_request(:get, uri, auth_token, params)
52
66
  end
53
67
 
54
- def sign_to_field(to)
55
- to.map do |feed_id|
56
- feed_slug, user_id = feed_id.split(':')
57
- feed = @client.feed(feed_slug, user_id)
58
- "#{feed.id} #{feed.token}"
59
- end
60
- end
61
-
62
68
  def add_activity(activity_data)
63
69
  uri = "/feed/#{@feed_url}/"
64
70
  data = activity_data.clone
65
- data[:to] &&= sign_to_field(data[:to])
66
71
  auth_token = create_jwt_token('feed', 'write')
67
72
 
68
73
  @client.make_request(:post, uri, auth_token, {}, data)
@@ -70,9 +75,6 @@ module Stream
70
75
 
71
76
  def add_activities(activities)
72
77
  uri = "/feed/#{@feed_url}/"
73
- activities.each do |activity|
74
- activity[:to] &&= sign_to_field(activity[:to])
75
- end
76
78
  data = {:activities => activities}
77
79
  auth_token = create_jwt_token('feed', 'write')
78
80
 
@@ -102,6 +104,27 @@ module Stream
102
104
  @client.make_request(:post, '/activities/', auth_token, {}, 'activities' => activities)
103
105
  end
104
106
 
107
+ def update_activity_to_targets(foreign_id, time, new_targets: nil, added_targets: nil, removed_targets: nil)
108
+ uri = "/feed_targets/#{@feed_url}/activity_to_targets/"
109
+ data = {
110
+ 'foreign_id': foreign_id,
111
+ 'time': time
112
+ }
113
+
114
+ if !new_targets.nil?
115
+ data['new_targets'] = new_targets
116
+ end
117
+ if !added_targets.nil?
118
+ data['added_targets'] = added_targets
119
+ end
120
+ if !removed_targets.nil?
121
+ data['removed_targets'] = removed_targets
122
+ end
123
+ auth_token = create_jwt_token('feed_targets', 'write')
124
+
125
+ @client.make_request(:post, uri, auth_token, {}, data)
126
+ end
127
+
105
128
  def follow(target_feed_slug, target_user_id, activity_copy_limit = 300)
106
129
  uri = "/feed/#{@feed_url}/follows/"
107
130
  activity_copy_limit = 0 if activity_copy_limit < 0
@@ -109,7 +132,6 @@ module Stream
109
132
 
110
133
  follow_data = {
111
134
  target: "#{target_feed_slug}:#{target_user_id}",
112
- target_token: @client.feed(target_feed_slug, target_user_id).token,
113
135
  activity_copy_limit: activity_copy_limit
114
136
  }
115
137
  auth_token = create_jwt_token('follower', 'write')
@@ -0,0 +1,84 @@
1
+ module Stream
2
+ class ReactionsClient < Client
3
+ def add(kind, activity_id, user_id, data: nil, target_feeds: nil)
4
+ data = {
5
+ kind: kind,
6
+ activity_id: activity_id,
7
+ user_id: user_id,
8
+ data: data,
9
+ target_feeds: target_feeds
10
+ }
11
+ make_reaction_request(:post, {}, data)
12
+ end
13
+
14
+ def get(reaction_id)
15
+ uri = "/reaction/#{reaction_id}/"
16
+ make_reaction_request(:get, {}, {}, :endpoint => uri)
17
+ end
18
+
19
+ def update(reaction_id, data: nil, target_feeds: nil)
20
+ data = {
21
+ data: data,
22
+ target_feeds: target_feeds
23
+ }
24
+ uri = "/reaction/#{reaction_id}/"
25
+ make_reaction_request(:put, {}, data, :endpoint => uri)
26
+ end
27
+
28
+ def delete(reaction_id)
29
+ uri = "/reaction/#{reaction_id}/"
30
+ make_reaction_request(:delete, {}, data, :endpoint => uri)
31
+ end
32
+
33
+ def add_child(kind, parent_id, user_id, data: nil, target_feeds: nil)
34
+ data = {
35
+ kind: kind,
36
+ parent: parent_id,
37
+ user_id: user_id,
38
+ data: data,
39
+ target_feeds: target_feeds
40
+ }
41
+ make_reaction_request(:post, {}, data)
42
+ end
43
+
44
+ def filter(params = {})
45
+ lookup_field = ""
46
+ lookup_value = ""
47
+ kind = params.fetch(:kind, "")
48
+ if params[:reaction_id]
49
+ lookup_field = "reaction_id"
50
+ lookup_value = params[:reaction_id]
51
+ elsif params[:activity_id]
52
+ lookup_field = "activity_id"
53
+ lookup_value = params[:activity_id]
54
+ elsif params[:user_id]
55
+ lookup_field = "user_id"
56
+ lookup_value = params[:user_id]
57
+ end
58
+ unless lookup_field.empty?
59
+ params.delete(lookup_field.to_sym)
60
+ end
61
+ if kind.nil? || kind.empty?
62
+ uri = "/reaction/#{lookup_field}/#{lookup_value}/"
63
+ else
64
+ uri = "/reaction/#{lookup_field}/#{lookup_value}/#{kind}/"
65
+ end
66
+ make_reaction_request(:get, params, {}, :endpoint => uri)
67
+ end
68
+
69
+ def create_reference(id)
70
+ _id = id
71
+ if id.respond_to?(:keys) and !id["id"].nil?
72
+ _id = id["id"]
73
+ end
74
+ "SR:#{_id}"
75
+ end
76
+
77
+ private
78
+
79
+ def make_reaction_request(method, params, data, endpoint: '/reaction/')
80
+ auth_token = Stream::Signer.create_jwt_token('reactions', '*', @api_secret, '*', '*')
81
+ make_request(method, endpoint, auth_token, params, data)
82
+ end
83
+ end
84
+ end
@@ -7,24 +7,9 @@ module Stream
7
7
 
8
8
  def initialize(key)
9
9
  @key = key.to_s
10
- @sha1 = OpenSSL::Digest.new('sha1')
11
10
  end
12
11
 
13
- def urlsafe_encodeb64(value)
14
- value.tr('+', '-').tr('/', '_').gsub(/^=+/, '').gsub(/=+$/, '')
15
- end
16
-
17
- def sign_message(message)
18
- key = Digest::SHA1.digest @key.to_s
19
- token = Base64.strict_encode64(OpenSSL::HMAC.digest(@sha1, key, message))
20
- urlsafe_encodeb64(token)
21
- end
22
-
23
- def sign(feed_slug, user_id)
24
- sign_message("#{feed_slug}#{user_id}")
25
- end
26
-
27
- def self.create_user_session_token(user_id, payload = {}, api_secret)
12
+ def self.create_user_token(user_id, payload = {}, api_secret)
28
13
  payload['user_id'] = user_id
29
14
  return JWT.encode(payload, api_secret, 'HS256')
30
15
  end
@@ -0,0 +1,47 @@
1
+ module Stream
2
+ class UsersClient < Client
3
+ def add(user_id, data: nil, get_or_create: false)
4
+ data = {
5
+ id: user_id,
6
+ data: data
7
+ }
8
+ params = {
9
+ get_or_create: get_or_create
10
+ }
11
+ make_user_request(:post, params, data)
12
+ end
13
+
14
+ def get(user_id)
15
+ uri = "/user/#{user_id}/"
16
+ make_user_request(:get, {}, {}, :endpoint => uri)
17
+ end
18
+
19
+ def update(user_id, data: nil)
20
+ data = {
21
+ data: data
22
+ }
23
+ uri = "/user/#{user_id}/"
24
+ make_user_request(:put, {}, data, :endpoint => uri)
25
+ end
26
+
27
+ def delete(user_id)
28
+ uri = "/user/#{user_id}/"
29
+ make_user_request(:delete, {}, {}, :endpoint => uri)
30
+ end
31
+
32
+ def create_reference(id)
33
+ _id = id
34
+ if id.respond_to?(:keys) and !id["id"].nil?
35
+ _id = id["id"]
36
+ end
37
+ "SU:#{_id}"
38
+ end
39
+
40
+ private
41
+
42
+ def make_user_request(method, params, data, endpoint: '/user/')
43
+ auth_token = Stream::Signer.create_jwt_token('users', '*', @api_secret, '*', '*')
44
+ make_request(method, endpoint, auth_token, params, data)
45
+ end
46
+ end
47
+ end
@@ -1,3 +1,3 @@
1
1
  module Stream
2
- VERSION = '2.11.0'.freeze
2
+ VERSION = '3.0.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stream-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.11.0
4
+ version: 3.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tommaso Barbugli
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2018-10-30 00:00:00.000000000 Z
13
+ date: 2018-12-19 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: faraday
@@ -32,20 +32,6 @@ dependencies:
32
32
  - - "<"
33
33
  - !ruby/object:Gem::Version
34
34
  version: '1.0'
35
- - !ruby/object:Gem::Dependency
36
- name: http_signatures
37
- requirement: !ruby/object:Gem::Requirement
38
- requirements:
39
- - - "~>"
40
- - !ruby/object:Gem::Version
41
- version: '0'
42
- type: :runtime
43
- prerelease: false
44
- version_requirements: !ruby/object:Gem::Requirement
45
- requirements:
46
- - - "~>"
47
- - !ruby/object:Gem::Version
48
- version: '0'
49
35
  - !ruby/object:Gem::Dependency
50
36
  name: jwt
51
37
  requirement: !ruby/object:Gem::Requirement
@@ -128,9 +114,10 @@ files:
128
114
  - lib/stream/exceptions.rb
129
115
  - lib/stream/feed.rb
130
116
  - lib/stream/personalization.rb
131
- - lib/stream/signedrequest.rb
117
+ - lib/stream/reactions.rb
132
118
  - lib/stream/signer.rb
133
119
  - lib/stream/url.rb
120
+ - lib/stream/users.rb
134
121
  - lib/stream/version.rb
135
122
  homepage: http://github.com/GetStream/stream-ruby
136
123
  licenses:
@@ -1,43 +0,0 @@
1
- require 'http_signatures'
2
- require 'net/http'
3
- require 'time'
4
-
5
- module Stream
6
- module SignedRequest
7
- module ClassMethods
8
- def supports_signed_requests;
9
- end
10
- end
11
-
12
- def self.included(klass)
13
- klass.extend ClassMethods
14
- end
15
-
16
- def make_signed_request(method, relative_url, params = {}, data = {})
17
- query_params = make_query_params(params)
18
- context = HttpSignatures::Context.new(
19
- keys: {@api_key => @api_secret},
20
- algorithm: 'hmac-sha256',
21
- headers: %w(date)
22
- )
23
- method_map = {
24
- :get => Net::HTTP::Get,
25
- :delete => Net::HTTP::Delete,
26
- :put => Net::HTTP::Put,
27
- :post => Net::HTTP::Post
28
- }
29
- request_date = Time.now.rfc822
30
- message = method_map[method].new(
31
- "#{get_http_client.base_path}#{relative_url}?#{URI.encode_www_form(query_params)}",
32
- 'date' => request_date
33
- )
34
- context.signer.sign(message)
35
- headers = {
36
- Authorization: message['Signature'],
37
- Date: request_date,
38
- 'X-Api-Key' => api_key
39
- }
40
- get_http_client.make_http_request(method, relative_url, query_params, data, headers)
41
- end
42
- end
43
- end