Class: Stream::Client
- Inherits:
-
Object
- Object
- Stream::Client
- Includes:
- Activities, Batch, SignedRequest
- Defined in:
- lib/stream/client.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
-
#api_secret ⇒ Object
readonly
Returns the value of attribute api_secret.
-
#app_id ⇒ Object
readonly
Returns the value of attribute app_id.
-
#client_options ⇒ Object
readonly
Returns the value of attribute client_options.
Instance Method Summary collapse
- #collections ⇒ Object
-
#feed(feed_slug, user_id) ⇒ Stream::Feed
Creates a feed instance.
- #get_default_params ⇒ Object
- #get_http_client ⇒ Object
-
#initialize(api_key = '', api_secret = '', app_id = nil, opts = {}) ⇒ Client
constructor
initializes a Stream API Client.
- #make_query_params(params) ⇒ Object
- #make_request(method, relative_url, signature, params = {}, data = {}, headers = {}) ⇒ Object
- #personalization ⇒ Object
- #update_activities(activities) ⇒ Object
- #update_activity(activity) ⇒ Object
Methods included from Activities
#activity_partial_update, #get_activities
Methods included from Batch
#add_to_many, #follow_many, #unfollow_many
Methods included from SignedRequest
included, #make_signed_request
Constructor Details
#initialize(api_key = '', api_secret = '', app_id = nil, opts = {}) ⇒ Client
initializes a Stream API Client
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/stream/client.rb', line 40 def initialize(api_key = '', api_secret = '', app_id = nil, opts = {}) if api_key.nil? || api_key.empty? env_url = ENV['STREAM_URL'] if env_url =~ Stream::STREAM_URL_COM_RE re = Stream::STREAM_URL_COM_RE elsif env_url =~ Stream::STREAM_URL_IO_RE re = Stream::STREAM_URL_IO_RE end raise ArgumentError, 'empty api_key parameter and missing or invalid STREAM_URL env variable' unless re matches = re.match(ENV['STREAM_URL']) api_key = matches['key'] api_secret = matches['secret'] app_id = matches['app_id'] opts[:location] = matches['location'] opts[:api_hostname] = matches['api_hostname'] end @api_key = api_key @api_secret = api_secret @app_id = app_id @signer = Stream::Signer.new(api_secret) @client_options = { api_key: @api_key, api_version: opts[:api_version] || 'v1.0', location: opts[:location], default_timeout: opts[:default_timeout] || 3, api_hostname: opts[:api_hostname] || 'stream-io-api.com' } end |
Instance Attribute Details
#api_key ⇒ Object (readonly)
Returns the value of attribute api_key.
12 13 14 |
# File 'lib/stream/client.rb', line 12 def api_key @api_key end |
#api_secret ⇒ Object (readonly)
Returns the value of attribute api_secret.
13 14 15 |
# File 'lib/stream/client.rb', line 13 def api_secret @api_secret end |
#app_id ⇒ Object (readonly)
Returns the value of attribute app_id.
14 15 16 |
# File 'lib/stream/client.rb', line 14 def app_id @app_id end |
#client_options ⇒ Object (readonly)
Returns the value of attribute client_options.
15 16 17 |
# File 'lib/stream/client.rb', line 15 def @client_options end |
Instance Method Details
#collections ⇒ Object
88 89 90 |
# File 'lib/stream/client.rb', line 88 def collections CollectionsClient.new(api_key, api_secret, app_id, ) end |
#feed(feed_slug, user_id) ⇒ Stream::Feed
Creates a feed instance
79 80 81 82 |
# File 'lib/stream/client.rb', line 79 def feed(feed_slug, user_id) token = @signer.sign(feed_slug, user_id) Stream::Feed.new(self, feed_slug, user_id, token) end |
#get_default_params ⇒ Object
101 102 103 |
# File 'lib/stream/client.rb', line 101 def get_default_params {:api_key => @api_key} end |
#get_http_client ⇒ Object
105 106 107 |
# File 'lib/stream/client.rb', line 105 def get_http_client @http_client ||= StreamHTTPClient.new(url_generator) end |
#make_query_params(params) ⇒ Object
109 110 111 |
# File 'lib/stream/client.rb', line 109 def make_query_params(params) Hash[get_default_params.merge(params).sort_by {|k, v| k.to_s}] end |
#make_request(method, relative_url, signature, params = {}, data = {}, headers = {}) ⇒ Object
113 114 115 116 117 118 |
# File 'lib/stream/client.rb', line 113 def make_request(method, relative_url, signature, params = {}, data = {}, headers = {}) headers['Authorization'] = signature headers['stream-auth-type'] = 'jwt' get_http_client.make_http_request(method, relative_url, make_query_params(params), data, headers) end |
#personalization ⇒ Object
84 85 86 |
# File 'lib/stream/client.rb', line 84 def personalization PersonalizationClient.new(api_key, api_secret, app_id, ) end |
#update_activities(activities) ⇒ Object
96 97 98 99 |
# File 'lib/stream/client.rb', line 96 def update_activities(activities) auth_token = Stream::Signer.create_jwt_token('activities', '*', @api_secret, '*') make_request(:post, '/activities/', auth_token, {}, 'activities' => activities) end |
#update_activity(activity) ⇒ Object
92 93 94 |
# File 'lib/stream/client.rb', line 92 def update_activity(activity) update_activities([activity]) end |