55require 'stream/url'
66
77module Stream
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
9- STREAM_URL_IO_RE = %r{https\: \/ \ / (?<key>\w +)\ : (?<secret>\w +)@((api\. )|((?<location>[-\w ]+)\. ))?(?<api_hostname>getstream\. io)\ / [\w =-\ ? %&]+app_id=(?<app_id>\d +)}i
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 . freeze
9+ STREAM_URL_IO_RE = %r{https:/ /(?<key>\w +):(?<secret>\w +)@((api\. )|((?<location>[-\w ]+)\. ))?(?<api_hostname>getstream\. io)/[\w =-?%&]+app_id=(?<app_id>\d +)}i . freeze
1010
1111 class Client
1212 attr_reader :api_key
@@ -36,7 +36,6 @@ class Client
3636 # Stream::Client.new('my_key', 'my_secret', 'my_app_id', :location => 'us-east')
3737 #
3838 def initialize ( api_key = '' , api_secret = '' , app_id = nil , opts = { } )
39-
4039 if api_key . nil? || api_key . empty?
4140 env_url = ENV [ 'STREAM_URL' ]
4241 if env_url =~ Stream ::STREAM_URL_COM_RE
@@ -100,7 +99,7 @@ def create_user_session_token(user_id, extra_data = {})
10099 # @return [string]
101100 #
102101 def create_user_token ( user_id , extra_data = { } )
103- return Stream ::Signer . create_user_token ( user_id , extra_data , api_secret )
102+ Stream ::Signer . create_user_token ( user_id , extra_data , api_secret )
104103 end
105104
106105 def personalization
@@ -128,16 +127,21 @@ def update_activities(activities)
128127 make_request ( :post , '/activities/' , auth_token , { } , 'activities' => activities )
129128 end
130129
130+ def og ( uri )
131+ auth_token = Stream ::Signer . create_jwt_token ( '*' , '*' , @api_secret , '*' )
132+ make_request ( :get , '/og' , auth_token , { url : uri } )
133+ end
134+
131135 def get_default_params
132- { : api_key => @api_key }
136+ { api_key : @api_key }
133137 end
134138
135139 def get_http_client
136- @http_client ||= StreamHTTPClient . new ( url_generator )
140+ @get_http_client ||= StreamHTTPClient . new ( url_generator )
137141 end
138142
139143 def make_query_params ( params )
140- Hash [ get_default_params . merge ( params ) . sort_by { |k , v | k . to_s } ]
144+ Hash [ get_default_params . merge ( params ) . sort_by { |k , _v | k . to_s } ]
141145 end
142146
143147 def make_request ( method , relative_url , signature , params = { } , data = { } , headers = { } )
@@ -187,7 +191,7 @@ def make_http_request(method, relative_url, params = nil, data = nil, headers =
187191
188192 case response [ :status ] . to_i
189193 when 200 ..203
190- return ::JSON . parse ( response [ :body ] )
194+ ::JSON . parse ( response [ :body ] )
191195 end
192196 end
193197 end
@@ -196,16 +200,16 @@ class RaiseHttpException < Faraday::Middleware
196200 def call ( env )
197201 @app . call ( env ) . on_complete do |response |
198202 case response [ :status ] . to_i
199- when 200 ..203
200- return response
201- when 401
202- raise StreamApiResponseException , error_message ( response , 'Bad feed' )
203- when 403
204- raise StreamApiResponseException , error_message ( response , 'Bad auth/headers' )
205- when 404
206- raise StreamApiResponseException , error_message ( response , 'url not found' )
207- when 204 ...600
208- raise StreamApiResponseException , error_message ( response , _build_error_message ( response . body ) )
203+ when 200 ..203
204+ return response
205+ when 401
206+ raise StreamApiResponseException , error_message ( response , 'Bad feed' )
207+ when 403
208+ raise StreamApiResponseException , error_message ( response , 'Bad auth/headers' )
209+ when 404
210+ raise StreamApiResponseException , error_message ( response , 'url not found' )
211+ when 204 ...600
212+ raise StreamApiResponseException , error_message ( response , _build_error_message ( response . body ) )
209213 end
210214 end
211215 end
@@ -229,7 +233,7 @@ def _build_error_message(response)
229233 end
230234
231235 def error_message ( response , body = nil )
232- "#{ response [ :method ] . to_s . upcase } #{ response [ :url ] } : #{ [ response [ :status ] . to_s + ':' , body ] . compact . join ( ' ' ) } "
236+ "#{ response [ :method ] . to_s . upcase } #{ response [ :url ] } : #{ [ " #{ response [ :status ] } :" , body ] . compact . join ( ' ' ) } "
233237 end
234238 end
235239end
0 commit comments