Class: Stream::Client

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

Direct Known Subclasses

CollectionsClient, PersonalizationClient

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Batch

#add_to_many, #follow_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



38
39
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
# File 'lib/stream/client.rb', line 38

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



86
87
88
# File 'lib/stream/client.rb', line 86

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:



77
78
79
80
# File 'lib/stream/client.rb', line 77

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



99
100
101
# File 'lib/stream/client.rb', line 99

def get_default_params
  {:api_key => @api_key}
end

#get_http_clientObject



103
104
105
# File 'lib/stream/client.rb', line 103

def get_http_client
  @http_client ||= StreamHTTPClient.new(url_generator)
end

#make_query_params(params) ⇒ Object



107
108
109
# File 'lib/stream/client.rb', line 107

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



111
112
113
114
115
116
# File 'lib/stream/client.rb', line 111

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



82
83
84
# File 'lib/stream/client.rb', line 82

def personalization
  PersonalizationClient.new(api_key, api_secret, app_id, client_options)
end

#update_activities(activities) ⇒ Object



94
95
96
97
# File 'lib/stream/client.rb', line 94

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



90
91
92
# File 'lib/stream/client.rb', line 90

def update_activity(activity)
  update_activities([activity])
end