Class: ToopherAPI

Inherits:
Object
  • Object
show all
Defined in:
lib/toopher_api.rb

Overview

Abstracts calls to the Toopher OAuth webservice

Constant Summary collapse

DEFAULT_BASE_URL =

Default URL for the Toopher webservice API. Can be overridden in the constructor if necessary.

'https://toopher-api.appspot.com/v1/'

Instance Method Summary collapse

Constructor Details

#initialize(key, secret, options = {}, base_url = DEFAULT_BASE_URL) ⇒ ToopherAPI

Creates a Toopher API consumer

Parameters:

  • key (String)

    Your Toopher API Key

  • secret (String)

    Your Toopher API Secret

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

    OAuth Options hash.

  • base_url (string) (defaults to: DEFAULT_BASE_URL)

    The base URL to use for the Toopher API



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/toopher_api.rb', line 46

def initialize(key,secret,options={}, base_url = DEFAULT_BASE_URL)
  consumer_key = key
  consumer_secret = secret

  consumer_key.empty? and raise ArgumentError, "Toopher consumer key cannot be empty!"
  consumer_secret.empty? and raise ArgumentError, "Toopher consumer secret cannot be empty!"

  @base_url = base_url
  @oauth_consumer = OAuth::Consumer.new(consumer_key, consumer_secret)
  @oauth_options = options
end

Instance Method Details

#authenticate(pairing_id, terminal_name = '', action_name = '', options = {}) ⇒ AuthenticationStatus

Authenticate an action with Toopher

Parameters:

  • pairing_id (String)

    The unique string identifier id returned by a previous pairing request.

  • terminal_name (String) (defaults to: '')

    A human recognizable string which represents the terminal from which the user is making the request. This is displayed to the user on the mobile app when authenticating. If this is not included, then a terminal_id returned from a previous request must be provided (see below). These should be unique values for each different device from which a user connects to your service (as best you can detect).

  • action_name (String) (defaults to: '')

    Optional action name, defaults to “log in” (displayed to the user)

Returns:



87
88
89
90
91
92
93
94
# File 'lib/toopher_api.rb', line 87

def authenticate(pairing_id, terminal_name = '', action_name = '', options = {})
  parameters = {
    'pairing_id' => pairing_id,
    'terminal_name' => terminal_name
  }
  action_name.empty? or (parameters['action_name'] = action_name)
  return AuthenticationStatus.new(post('authentication_requests/initiate', parameters.merge(options)))
end

#get_authentication_status(authentication_request_id) ⇒ Object

Check on the status of a previous authentication request

Parameters:

  • authentication_request_id (String)

    The unique string identifier id returned by a previous authentication request.



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

def get_authentication_status(authentication_request_id)
  return AuthenticationStatus.new(get('authentication_requests/' + authentication_request_id))
end

#get_pairing_status(pairing_request_id) ⇒ PairingStatus

Check on the status of a previous pairing request

Parameters:

  • pairing_request_id (String)

    The unique string identifier id returned by a previous pairing request.

Returns:



76
77
78
# File 'lib/toopher_api.rb', line 76

def get_pairing_status(pairing_request_id)
  return PairingStatus.new(get('pairings/' + pairing_request_id))
end

#pair(pairing_phrase, user_name, options = {}) ⇒ PairingStatus

Create the pairing between a particular user and their mobile device

Parameters:

  • pairing_phrase (String)

    The pairing phrase generated by a user’s mobile application.

  • user_name (String)

    A human recognizable string which represents the user making the request (usually their username). This is displayed to the user on the mobile app when authenticating.

Returns:



64
65
66
67
68
69
# File 'lib/toopher_api.rb', line 64

def pair(pairing_phrase, user_name, options = {})
  return PairingStatus.new(post('pairings/create', {
    'pairing_phrase' => pairing_phrase,
    'user_name' => user_name
  }.merge(options)))
end