Class: Checkdin::Client

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Activities, Campaigns, Clients, CustomActivities, Leaderboard, PointAccountEntries, Promotions, PushApiSubscriptions, TwitterHashtagStreams, Users, Votes, WonRewards
Defined in:
lib/checkdin/client.rb

Constant Summary collapse

DEFAULT_API_URL =
"https://app.checkd.in/api/v1"

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from TwitterHashtagStreams

#twitter_hashtag_stream, #twitter_hashtag_streams

Methods included from PointAccountEntries

#point_account_entries

Methods included from PushApiSubscriptions

#disable_push_api_subscription, #enable_push_api_subscription, #push_api_subscription, #push_api_subscriptions

Methods included from Clients

#client_details

Methods included from Votes

#votes

Methods included from Leaderboard

#campaign_leaderboard, #classification_leaderboard, #leaderboard

Methods included from CustomActivities

#create_custom_activity

Methods included from Users

#blacklist, #blacklisted, #create_user, #create_user_authentication, #create_user_point_redemption, #user, #user_authentication, #user_authentications, #user_point_accounts, #users, #view_user_full_description, #whitelabel_user_lookup, #whitelist

Methods included from Activities

#activities, #activity, #add_vote_on_activity

Methods included from WonRewards

#retract_won_reward, #won_reward, #won_rewards

Methods included from Promotions

#create_promotion, #promotion, #promotion_votes_leaderboard, #promotions

Methods included from Campaigns

#campaign, #campaigns

Constructor Details

#initialize(options = {}) ⇒ Client

Initialize the client class that will be used for all checkd.in API requests. Requires a valid client_id and client_secret - more info at developer.checkd.in

Parameters:

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

Options Hash (options):

  • String (Object)

    :client_id Your foursquare app’s client_id

  • String (Object)

    :client_secret Your foursquare app’s client_secret

  • String (Object)

    :api_url A custom API url, does not usually have to be overridden

  • Hash (Object)

    :ssl Additional SSL options (like the path to certificate file)

Raises:

  • (ArgumentError)


36
37
38
39
40
41
42
43
# File 'lib/checkdin/client.rb', line 36

def initialize(options={})
  @client_id     = options.delete(:client_id)
  @client_secret = options.delete(:client_secret)
  @api_url       = options.delete(:api_url) || DEFAULT_API_URL
  @ssl           = options.delete(:ssl) || {}

  raise ArgumentError.new("Unexpected argument given: #{options.inspect}") unless options.blank?
end

Instance Attribute Details

#api_urlObject (readonly)

Base URL for api requests.



23
24
25
# File 'lib/checkdin/client.rb', line 23

def api_url
  @api_url
end

#client_idObject (readonly)

Returns the value of attribute client_id.



20
21
22
# File 'lib/checkdin/client.rb', line 20

def client_id
  @client_id
end

#client_secretObject (readonly)

Returns the value of attribute client_secret.



20
21
22
# File 'lib/checkdin/client.rb', line 20

def client_secret
  @client_secret
end

#sslObject (readonly)

Returns the value of attribute ssl.



21
22
23
# File 'lib/checkdin/client.rb', line 21

def ssl
  @ssl
end

Instance Method Details

#connectionObject

Sets up the connection to be used for all requests based on options passed during initialization.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/checkdin/client.rb', line 46

def connection
  @connection ||= begin
    params = {
      :client_id     => client_id,
      :client_secret => client_secret
    }

    Faraday::Connection.new(:url => api_url, :ssl => ssl, :params => params, :headers => default_headers) do |builder|
      builder.use Faraday::Request::Multipart
      builder.use Faraday::Request::UrlEncoded

      builder.use FaradayMiddleware::Mashify
      builder.use FaradayMiddleware::ParseJson

      builder.adapter Faraday.default_adapter
    end
  end
end

#return_error_or_body(response) ⇒ Object

Helper method to return errors or desired response data as appropriate.

Added just for convenience to avoid having to traverse farther down the response just to get to returned data.



69
70
71
72
73
74
75
# File 'lib/checkdin/client.rb', line 69

def return_error_or_body(response)
  if response.status / 100 == 2
    response.body
  else
    raise Checkdin::APIError.new(response.status, response.body)
  end
end