Class: Stream::Client

Inherits:
Object
  • Object
show all
Includes:
Activities, Batch, SignedRequest
Defined in:
lib/stream/client.rb

Direct Known Subclasses

CollectionsClient, PersonalizationClient

Instance Attribute Summary collapse

Instance Method Summary collapse

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

Examples:

initialise the client to connect to EU-West location

Stream::Client.new('my_key', 'my_secret', 'my_app_id', :location => 'us-east')

Parameters:

  • api_key (string) (defaults to: '')

    your application api_key

  • api_secret (string) (defaults to: '')

    your application secret

  • app_id (string) (defaults to: nil)

    the id of your application (optional)

  • opts (hash) (defaults to: {})

    extra options



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_keyObject (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_secretObject (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_idObject (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_optionsObject (readonly)

Returns the value of attribute client_options.



15
16
17
# File 'lib/stream/client.rb', line 15

def client_options
  @client_options
end

Instance Method Details

#collectionsObject



88
89
90
# File 'lib/stream/client.rb', line 88

def collections
  CollectionsClient.new(api_key, api_secret, app_id, client_options)
end

#feed(feed_slug, user_id) ⇒ Stream::Feed

Creates a feed instance

Parameters:

  • feed_slug (string)

    the feed slug (eg. flat, aggregated…)

  • user_id (user_id)

    the user_id of this feed (eg. User42)

Returns:



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_paramsObject



101
102
103
# File 'lib/stream/client.rb', line 101

def get_default_params
  {:api_key => @api_key}
end

#get_http_clientObject



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

#personalizationObject



84
85
86
# File 'lib/stream/client.rb', line 84

def personalization
  PersonalizationClient.new(api_key, api_secret, app_id, client_options)
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