Class: ACTV::Client
Overview
Wrapper for the ACTV REST API
Constant Summary
Constants included from Configurable
ACTV::Configurable::AUTH_KEYS, ACTV::Configurable::CONFIG_KEYS
Instance Attribute Summary collapse
-
#oauth_token ⇒ Object
readonly
Returns the value of attribute oauth_token.
Instance Method Summary collapse
-
#article(id, params = {}) ⇒ ACTV::Article
Returns an article with the specified ID.
-
#articles(q, params = {}) ⇒ ACTV::SearchResults
Returns articles that match a specified query.
- #asset(id, params = {}) ⇒ Object
- #asset_stats(asset_id) ⇒ Object
-
#assets(q, params = {}) ⇒ ACTV::SearchResults
(also: #search)
Returns assets that match a specified query.
- #avatar_url(options = {}) ⇒ Object
-
#connection ⇒ Faraday::Connection
Returns a Faraday::Connection object.
-
#credentials? ⇒ Boolean
Check whether credentials are present.
-
#delete(path, params = {}, options = {}) ⇒ Object
Perform an HTTP DELETE request.
- #display_name_exists?(display_name, params = {}) ⇒ Boolean
- #event(id, params = {}) ⇒ Object
-
#event_results(assetId, assetTypeId, options = {}) ⇒ ACTV::EventResult
Returns a result with the specified asset ID and asset type ID.
- #events(q, params = {}) ⇒ Object
-
#find_asset_by_url(url) ⇒ ACTV::Asset
Returns an asset with the specified url path.
- #find_by_endurance_id(endurance_id) ⇒ Object
-
#get(path, params = {}, options = {}) ⇒ Object
Perform an HTTP GET request.
-
#initialize(options = {}) ⇒ Client
constructor
Initialized a new Client object.
- #is_advantage_member?(options = {}) ⇒ Boolean
-
#me(params = {}) ⇒ ACTV::User
Returns the currently logged in user.
-
#multi_search(*options) ⇒ Object
Returns results with multiple queries.
-
#organizer(id, params = {}) ⇒ Object
Returns an organizer with the specified ID.
-
#organizers(params = {}) ⇒ ACTV::Organizer
Returns all organizers.
-
#popular_articles(params = {}) ⇒ ACTV::SearchResults
Returns popular assets that match a specified query.
-
#popular_events(params = {}) ⇒ ACTV::SearchResults
Returns popular assets that match a specified query.
-
#popular_interests(params = {}, options = {}) ⇒ ACTV::PopularInterestSearchResults
Returns popular interests.
-
#popular_searches(options = {}) ⇒ ACTV::PopularSearchSearchResults
Returns popular searches.
-
#post(path, params = {}, options = {}) ⇒ Object
Perform an HTTP POST request.
-
#put(path, params = {}, options = {}) ⇒ Object
Perform an HTTP UPDATE request.
-
#request(method, path, params, options) ⇒ Object
Perform an HTTP Request.
-
#upcoming_events(params = {}) ⇒ ACTV::SearchResults
Returns upcoming assets that match a specified query.
- #update_me(user, params = {}) ⇒ Object
- #user_name_exists?(user_name, params = {}) ⇒ Boolean
- #video(id, params = {}) ⇒ Object
- #videos(q, params = {}) ⇒ Object
Methods included from Configurable
Constructor Details
Instance Attribute Details
#oauth_token ⇒ Object (readonly)
Returns the value of attribute oauth_token.
45 46 47 |
# File 'lib/actv/client.rb', line 45 def oauth_token @oauth_token end |
Instance Method Details
#article(id, params = {}) ⇒ ACTV::Article
Returns an article with the specified ID
149 150 151 152 153 154 155 156 157 158 |
# File 'lib/actv/client.rb', line 149 def article id, params={} request_string = "/v2/assets/#{id}" is_preview, params = params_include_preview? params request_string += '/preview' if is_preview response = get "#{request_string}.json", params article = ACTV::Article.new response[:body] article.is_article? ? article : nil end |
#articles(q, params = {}) ⇒ ACTV::SearchResults
Returns articles that match a specified query.
136 137 138 139 |
# File 'lib/actv/client.rb', line 136 def articles(q, params={}) response = get("/v2/search.json", params.merge({query: q, category: 'articles'})) ACTV::ArticleSearchResults.from_response(response) end |
#asset(id, params = {}) ⇒ Object
72 73 74 75 76 77 |
# File 'lib/actv/client.rb', line 72 def asset id, params={} params = params.with_indifferent_access is_preview = params.delete(:preview) == "true" response = request_response id, params, is_preview asset_from_response response end |
#asset_stats(asset_id) ⇒ Object
303 304 305 306 |
# File 'lib/actv/client.rb', line 303 def asset_stats asset_id response = get("/v2/assets/#{asset_id}/stats") ACTV::AssetStatsResult.new response[:body] end |
#assets(q, params = {}) ⇒ ACTV::SearchResults Also known as: search
Returns assets that match a specified query.
66 67 68 69 |
# File 'lib/actv/client.rb', line 66 def assets(q, params={}) response = get("/v2/search.json", params.merge(query: q)) ACTV::SearchResults.from_response(response) end |
#avatar_url(options = {}) ⇒ Object
341 342 343 |
# File 'lib/actv/client.rb', line 341 def avatar_url(={}) get("/v2/me/avatar_url", )[:body][:avatar_url] end |
#connection ⇒ Faraday::Connection
Returns a Faraday::Connection object
368 369 370 |
# File 'lib/actv/client.rb', line 368 def connection @connection ||= Faraday.new(@endpoint, @connection_options.merge(:builder => @middleware)) end |
#credentials? ⇒ Boolean
Check whether credentials are present
408 409 410 |
# File 'lib/actv/client.rb', line 408 def credentials? credentials.values.all? end |
#delete(path, params = {}, options = {}) ⇒ Object
Perform an HTTP DELETE request
361 362 363 |
# File 'lib/actv/client.rb', line 361 def delete(path, params={}, ={}) request(:delete, path, params, ) end |
#display_name_exists?(display_name, params = {}) ⇒ Boolean
333 334 335 |
# File 'lib/actv/client.rb', line 333 def display_name_exists?(display_name, params={}) get("/v2/users/display_name/#{URI.escape(display_name)}", params)[:body][:exists] end |
#event(id, params = {}) ⇒ Object
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/actv/client.rb', line 165 def event(id, params={}) request_string = "/v2/assets/#{id}" is_preview, params = params_include_preview? params request_string += '/preview' if is_preview response = get("#{request_string}.json", params) if response[:body].is_a? Array response[:body].map do |item| ACTV::Event.new item end else event = ACTV::Event.new response[:body] event = ACTV::Evergreen.new(event) if event.evergreen? event.is_article? ? nil : event end end |
#event_results(assetId, assetTypeId, options = {}) ⇒ ACTV::EventResult
Returns a result with the specified asset ID and asset type ID
269 270 271 272 273 274 275 276 |
# File 'lib/actv/client.rb', line 269 def event_results(assetId, assetTypeId, ={}) begin response = get("/api/v1/events/#{assetId}/#{assetTypeId}.json", {}, ) ACTV::EventResult.from_response(response) rescue nil end end |
#events(q, params = {}) ⇒ Object
160 161 162 163 |
# File 'lib/actv/client.rb', line 160 def events(q, params={}) response = get("/v2/search.json", params.merge({query: q, category: 'event'})) ACTV::EventSearchResults.from_response(response) end |
#find_asset_by_url(url) ⇒ ACTV::Asset
Returns an asset with the specified url path
113 114 115 116 117 118 |
# File 'lib/actv/client.rb', line 113 def find_asset_by_url(url) url_md5 = Digest::MD5.hexdigest(url) response = get("/v2/seourls/#{url_md5}?load_asset=true") ACTV::Asset.from_response(response) end |
#find_by_endurance_id(endurance_id) ⇒ Object
120 121 122 123 124 125 |
# File 'lib/actv/client.rb', line 120 def find_by_endurance_id endurance_id response = get "/v2/search.json", find_by_endurance_id_params(endurance_id) ACTV::SearchResults.from_response(response).results.select do |asset| asset.registrationUrlAdr.end_with?(endurance_id.to_s) and asset.assetParentAsset[:assetGuid].nil? end end |
#get(path, params = {}, options = {}) ⇒ Object
Perform an HTTP GET request
346 347 348 |
# File 'lib/actv/client.rb', line 346 def get(path, params={}, ={}) request(:get, path, params, ) end |
#is_advantage_member?(options = {}) ⇒ Boolean
337 338 339 |
# File 'lib/actv/client.rb', line 337 def is_advantage_member?(={}) get("/v2/me/is_advantage_member", )[:body][:is_advantage_member] end |
#me(params = {}) ⇒ ACTV::User
Returns the currently logged in user
315 316 317 318 319 320 |
# File 'lib/actv/client.rb', line 315 def me(params={}) response = get("/v2/me.json", params) user = ACTV::User.from_response(response) user.access_token = @oauth_token user end |
#multi_search(*options) ⇒ Object
Returns results with multiple queries
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 |
# File 'lib/actv/client.rb', line 281 def multi_search(*) results = [] query_index = 0 = .inject({}) do |hash, | hash.merge! "query_#{query_index}" => "[#{URI.encode_www_form }]" query_index += 1 hash end if .present? response = get("/v2/multisearch", ) response[:body].each_value do |sub_query| sub_query[:results].each do |asset| results << ACTV::Asset.from_response(body: asset) end end end results end |
#organizer(id, params = {}) ⇒ Object
Returns an organizer with the specified ID
87 88 89 90 |
# File 'lib/actv/client.rb', line 87 def organizer(id, params={}) response = get("/v3/organizers/#{id}.json", params) ACTV::Organizer.from_response response end |
#organizers(params = {}) ⇒ ACTV::Organizer
Returns all organizers
101 102 103 104 |
# File 'lib/actv/client.rb', line 101 def organizers(params={}) response = get("/v3/organizers.json", params) ACTV::OrganizerResults.from_response response end |
#popular_articles(params = {}) ⇒ ACTV::SearchResults
Returns popular assets that match a specified query.
229 230 231 232 |
# File 'lib/actv/client.rb', line 229 def popular_articles(params={}) response = get("/v2/articles/popular", params) ACTV::SearchResults.from_response(response) end |
#popular_events(params = {}) ⇒ ACTV::SearchResults
Returns popular assets that match a specified query.
203 204 205 206 |
# File 'lib/actv/client.rb', line 203 def popular_events(params={}) response = get("/v2/events/popular", params) ACTV::SearchResults.from_response(response) end |
#popular_interests(params = {}, options = {}) ⇒ ACTV::PopularInterestSearchResults
Returns popular interests
242 243 244 245 |
# File 'lib/actv/client.rb', line 242 def popular_interests(params={}, ={}) response = get("/interest/_search", params, ) ACTV::PopularInterestSearchResults.from_response(response) end |
#popular_searches(options = {}) ⇒ ACTV::PopularSearchSearchResults
Returns popular searches
255 256 257 258 259 |
# File 'lib/actv/client.rb', line 255 def popular_searches(={}) #response = get("/v2/articles/popular", params) #ACTV::ArticleSearchResults.from_response(response) ["Couch to 5k","Kids' Camps","Swimming Classes","Half Marathons in Southern CA","Gyms in Solana Beach","Dignissim Qui Blandit","Dolore Te Feugait","Lorem Ipsum","Convnetio Ibidem","Aliquam Jugis"] end |
#post(path, params = {}, options = {}) ⇒ Object
Perform an HTTP POST request
351 352 353 |
# File 'lib/actv/client.rb', line 351 def post(path, params={}, ={}) request(:post, path, params, ) end |
#put(path, params = {}, options = {}) ⇒ Object
Perform an HTTP UPDATE request
356 357 358 |
# File 'lib/actv/client.rb', line 356 def put(path, params={}, ={}) request(:put, path, params, ) end |
#request(method, path, params, options) ⇒ Object
Perform an HTTP Request
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 |
# File 'lib/actv/client.rb', line 373 def request(method, path, params, ) uri = [:endpoint] || @endpoint uri = URI(uri) unless uri.respond_to?(:host) uri += path request_headers = {} params[:api_key] = @api_key unless @api_key.nil? if self.credentials? # When posting a file, don't sign any params signature_params = if [:post, :put].include?(method.to_sym) && params.values.any?{|value| value.is_a?(File) || (value.is_a?(Hash) && (value[:io].is_a?(IO) || value[:io].is_a?(StringIO)))} {} else params end = SimpleOAuth::Header.new(method, uri, signature_params, credentials) request_headers[:authorization] = .to_s.sub('OAuth', "Bearer") end connection.url_prefix = [:endpoint] || @endpoint connection.run_request(method.to_sym, path, nil, request_headers) do |request| unless params.empty? case request.method when :post, :put request.body = params else request.params.update(params) end end yield request if block_given? end.env rescue Faraday::Error::ClientError raise ACTV::Error::ClientError end |
#upcoming_events(params = {}) ⇒ ACTV::SearchResults
Returns upcoming assets that match a specified query.
216 217 218 219 |
# File 'lib/actv/client.rb', line 216 def upcoming_events(params={}) response = get("/v2/events/upcoming", params) ACTV::SearchResults.from_response(response) end |
#update_me(user, params = {}) ⇒ Object
322 323 324 325 326 327 |
# File 'lib/actv/client.rb', line 322 def update_me(user, params={}) response = put("/v2/me.json", params.merge(user)) user = ACTV::User.from_response(response) user.access_token = @oauth_token user end |
#user_name_exists?(user_name, params = {}) ⇒ Boolean
329 330 331 |
# File 'lib/actv/client.rb', line 329 def user_name_exists?(user_name, params={}) get("/v2/users/user_name/#{user_name}", params)[:body][:exists] end |
#video(id, params = {}) ⇒ Object
183 184 185 186 187 188 |
# File 'lib/actv/client.rb', line 183 def video(id, params={}) request_string = "/v2/assets/#{id}" response = get "#{request_string}.json", params ACTV::Video.new response[:body] end |
#videos(q, params = {}) ⇒ Object
190 191 192 193 |
# File 'lib/actv/client.rb', line 190 def videos(q, params={}) response = get("/v2/search.json", params.merge({query: q, category: 'videos'})) ACTV::VideoSearchResults.from_response(response) end |