Class: ACTV::Client

Inherits:
Object
  • Object
show all
Includes:
Configurable
Defined in:
lib/actv/client.rb

Overview

Note:

Wrapper for the ACTV REST API

Constant Summary

Constants included from Configurable

ACTV::Configurable::AUTH_KEYS, ACTV::Configurable::CONFIG_KEYS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Configurable

#configure, keys

Constructor Details

#initialize(options = {}) ⇒ Client

Initialized a new Client object

@return

Parameters:

  • options (Hash) (defaults to: {})


35
36
37
38
39
# File 'lib/actv/client.rb', line 35

def initialize(options={})
  ACTV::Configurable.keys.each do |key|
    instance_variable_set("@#{key}", options[key] || ACTV.options[key])
  end
end

Instance Attribute Details

#oauth_tokenObject (readonly)

Returns the value of attribute oauth_token.



29
30
31
# File 'lib/actv/client.rb', line 29

def oauth_token
  @oauth_token
end

Instance Method Details

#article(id) ⇒ ACTV::Article

Returns an article with the specified ID

Examples:

Return the article with the id BA288960-2718-4B20-B380-8F939596B123

ACTV.article("BA288960-2718-4B20-B380-8F939596B123")

Parameters:

  • id (String)

    An article ID.

  • options (Hash)

    A customizable set of options.

Returns:



155
156
157
158
159
# File 'lib/actv/client.rb', line 155

def article(id)
  response = get("/v2/assets/#{id}.json")
  article = ACTV::Article.from_response(response)
  article.is_article? ? article : nil
end

#articles(q, params = {}) ⇒ ACTV::SearchResults

Returns articles that match a specified query.

Examples:

Returns articles related to running

ACTV.articles('running')
ACTV.articles('running')

Parameters:

  • q (String)

    A search term.

  • options (Hash)

    A customizable set of options.

Returns:



142
143
144
145
# File 'lib/actv/client.rb', line 142

def articles(q, params={})
  response = get("/v2/search.json", params.merge({query: q, category: 'articles'}))
  ACTV::ArticleSearchResults.from_response(response)
end

#asset(id, params = {}) ⇒ ACTV::Asset

Returns an asset with the specified ID in an array

Examples:

Return the asset with the id BA288960-2718-4B20-B380-8F939596B123

ACTV.asset("BA288960-2718-4B20-B380-8F939596B123")

Parameters:

  • id (String)

    An assset ID.

  • options (Hash)

    A customizable set of options.

Returns:



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/actv/client.rb', line 64

def asset(id, params={})
  is_preview, params = params_include_preview? params

  if is_preview
    request_string = "/v2/assets/#{id}/preview"
    response = get("#{request_string}.json", params)
  else
    request_string = "/v2/assets"
    params = params.merge :id => id
    response = post("#{request_string}.json", params)
  end

  if response[:body].is_a? Array
    response[:body].map do |item|
      ACTV::Asset.from_response body: item
    end
  else
    [ACTV::Asset.from_response(response)]
  end
end

#asset_stats(asset_id) ⇒ Object



267
268
269
270
# File 'lib/actv/client.rb', line 267

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.

Examples:

Returns assets related to running

ACTV.assets('running')
ACTV.search('running')

Parameters:

  • q (String)

    A search term.

  • options (Hash)

    A customizable set of options.

Returns:



50
51
52
53
# File 'lib/actv/client.rb', line 50

def assets(q, params={})
  response = get("/v2/search.json", params.merge(query: q))
  ACTV::SearchResults.from_response(response)
end

#avatar_url(options = {}) ⇒ Object



305
306
307
# File 'lib/actv/client.rb', line 305

def avatar_url(options={})
  get("/v2/me/avatar_url", options)[:body][:avatar_url]
end

#connectionFaraday::Connection

Returns a Faraday::Connection object

Returns:

  • (Faraday::Connection)


332
333
334
# File 'lib/actv/client.rb', line 332

def connection
  @connection ||= Faraday.new(@endpoint, @connection_options.merge(:builder => @middleware))
end

#credentials?Boolean

Check whether credentials are present

Returns:

  • (Boolean)


372
373
374
# File 'lib/actv/client.rb', line 372

def credentials?
  credentials.values.all?
end

#delete(path, params = {}, options = {}) ⇒ Object

Perform an HTTP DELETE request



325
326
327
# File 'lib/actv/client.rb', line 325

def delete(path, params={}, options={})
  request(:delete, path, params, options)
end

#display_name_exists?(display_name, params = {}) ⇒ Boolean

Returns:

  • (Boolean)


297
298
299
# File 'lib/actv/client.rb', line 297

def display_name_exists?(display_name, params={})
  get("/v2/users/display_name/#{URI.escape(display_name)}", params)[:body][:exists]
end

#event(id, params = {}) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/actv/client.rb', line 166

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.from_response body: item
    end
  else
    event = ACTV::Event.from_response(response)
    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

Examples:

Return the result with the assetId 286F5731-9800-4C6E-ADD5-0E3B72392CA7 and assetTypeId 3BF82BBE-CF88-4E8C-A56F-78F5CE87E4C6

ACTV.event_results("286F5731-9800-4C6E-ADD5-0E3B72392CA7","3BF82BBE-CF88-4E8C-A56F-78F5CE87E4C6")

Parameters:

  • assetId (String)

    An asset ID.

  • assetTypeId (String)

    An asset type ID.

Returns:



258
259
260
261
262
263
264
265
# File 'lib/actv/client.rb', line 258

def event_results(assetId, assetTypeId, options={})
  begin
    response = get("/api/v1/events/#{assetId}/#{assetTypeId}.json", {}, options)
    ACTV::EventResult.from_response(response)
  rescue
    nil
  end
end

#events(q, params = {}) ⇒ Object



161
162
163
164
# File 'lib/actv/client.rb', line 161

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

Examples:

ACTV.asset_by_path("http://www.active.com/miami-fl/running/miami-marathon-and-half-marathon-2014")

Parameters:

  • path (String)

Returns:



119
120
121
122
123
124
# File 'lib/actv/client.rb', line 119

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



126
127
128
129
130
131
# File 'lib/actv/client.rb', line 126

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



310
311
312
# File 'lib/actv/client.rb', line 310

def get(path, params={}, options={})
  request(:get, path, params, options)
end

#is_advantage_member?(options = {}) ⇒ Boolean

Returns:

  • (Boolean)


301
302
303
# File 'lib/actv/client.rb', line 301

def is_advantage_member?(options={})
  get("/v2/me/is_advantage_member", options)[:body][:is_advantage_member]
end

#me(params = {}) ⇒ ACTV::User

Returns the currently logged in user

Examples:

Return current_user if authentication was susccessful

ACTV.me

Parameters:

  • options (Hash)

    A customizable set of options.

Returns:



279
280
281
282
283
284
# File 'lib/actv/client.rb', line 279

def me(params={})
  response = get("/v2/me.json", params)
  user = ACTV::User.from_response(response)
  user.access_token =  @oauth_token
  user
end

#organizer(id, params = {}) ⇒ Object

Returns an organizer with the specified ID

Examples:

Return the organizer with the id AA388860-2718-4B20-B380-8F939596B123

ACTV.organizer("AA388860-2718-4B20-B380-8F939596B123")

Parameters:

  • id (String)

    An assset ID.

  • options (Hash)

    A customizable set of options.

Returns:

  • ACTV::Organizer The requested organizer.



93
94
95
96
# File 'lib/actv/client.rb', line 93

def organizer(id, params={})
  response = get("/v3/organizers/#{id}.json", params)
  ACTV::Organizer.from_response response
end

#organizers(params = {}) ⇒ ACTV::Organizer

Returns all organizers

Examples:

Returns organizers

ACTV.organizers
ACTV.organizers({per_page: 8, current_page: 2})

Parameters:

  • options (Hash)

    A customizable set of options.

Returns:



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

def organizers(params={})
  response = get("/v3/organizers.json", params)
  ACTV::OrganizerResults.from_response response
end

Returns popular assets that match a specified query.

Examples:

Returns articles related to running

ACTV.popular_articles()
ACTV.popular_articles("topic:running")

Parameters:

  • options (Hash)

    A customizable set of options.

Returns:



218
219
220
221
# File 'lib/actv/client.rb', line 218

def popular_articles(params={})
  response = get("/v2/articles/popular", params)
  ACTV::ArticleSearchResults.from_response(response)
end

Returns popular assets that match a specified query.

Examples:

Returns articles related to running

ACTV.popular_events()
ACTV.popular_events("topic:running")

Parameters:

  • options (Hash)

    A customizable set of options.

Returns:



192
193
194
195
# File 'lib/actv/client.rb', line 192

def popular_events(params={})
  response = get("/v2/events/popular", params)
  ACTV::SearchResults.from_response(response)
end

Returns popular interests

Examples:

Returns most popular interests

ACTV.popular_interests()
ACTV.popular_interests({per_page: 8})

Parameters:

  • options (Hash) (defaults to: {})

    A customizable set of options.

Returns:



231
232
233
234
# File 'lib/actv/client.rb', line 231

def popular_interests(params={}, options={})
  response = get("/interest/_search", params, options)
  ACTV::PopularInterestSearchResults.from_response(response)
end

Returns popular searches

Examples:

Returns most popular searches

ACTV.popular_searches()
ACTV.popular_searches({per_page: 8})

Parameters:

  • options (Hash) (defaults to: {})

    A customizable set of options.

Returns:

  • (ACTV::PopularSearchSearchResults)

    Return searches



244
245
246
247
248
# File 'lib/actv/client.rb', line 244

def popular_searches(options={})
  #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



315
316
317
# File 'lib/actv/client.rb', line 315

def post(path, params={}, options={})
  request(:post, path, params, options)
end

#put(path, params = {}, options = {}) ⇒ Object

Perform an HTTP UPDATE request



320
321
322
# File 'lib/actv/client.rb', line 320

def put(path, params={}, options={})
  request(:put, path, params, options)
end

#request(method, path, params, options) ⇒ Object

Perform an HTTP Request



337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
# File 'lib/actv/client.rb', line 337

def request(method, path, params, options)
  uri = options[: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
    authorization = SimpleOAuth::Header.new(method, uri, signature_params, credentials)
    request_headers[:authorization] = authorization.to_s.sub('OAuth', "Bearer")
  end
  connection.url_prefix = options[: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.

Examples:

Returns articles related to running

ACTV.upcoming_events()
ACTV.upcoming_events("topic:running")

Parameters:

  • options (Hash)

    A customizable set of options.

Returns:



205
206
207
208
# File 'lib/actv/client.rb', line 205

def upcoming_events(params={})
  response = get("/v2/events/upcoming", params)
  ACTV::SearchResults.from_response(response)
end

#update_me(user, params = {}) ⇒ Object



286
287
288
289
290
291
# File 'lib/actv/client.rb', line 286

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

Returns:

  • (Boolean)


293
294
295
# File 'lib/actv/client.rb', line 293

def user_name_exists?(user_name, params={})
  get("/v2/users/user_name/#{user_name}", params)[:body][:exists]
end