toopher_api 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/demo/toopher_demo.rb CHANGED
@@ -27,10 +27,10 @@ user.chomp!
27
27
 
28
28
  pairing = toopher.pair(phrase, user)
29
29
 
30
- while(!pairing['enabled'])
30
+ while(!pairing.enabled)
31
31
  puts 'waiting for authorization...'
32
32
  sleep(1)
33
- pairing = toopher.get_pairing_status(pairing['id'])
33
+ pairing = toopher.get_pairing_status(pairing.id)
34
34
  end
35
35
 
36
36
  puts 'paired successfully!'
@@ -45,12 +45,12 @@ while (true)
45
45
  action.chomp!
46
46
 
47
47
  puts 'sending authentication request...'
48
- auth = toopher.authenticate(pairing['id'], terminal_name, action)
48
+ auth = toopher.authenticate(pairing.id, terminal_name, action)
49
49
 
50
50
  while(auth['pending'])
51
51
  puts 'waiting for authentication...'
52
52
  sleep(1)
53
- auth = toopher.get_authentication_status(auth['id'])
53
+ auth = toopher.get_authentication_status(auth.id)
54
54
  end
55
55
 
56
56
  puts "Successfully authorized action '" + action + "'. Enter another action to authorize again, or [Ctrl-C] to exit"
data/lib/toopher_api.rb CHANGED
@@ -34,6 +34,12 @@ class ToopherAPI
34
34
 
35
35
  DEFAULT_BASE_URL = 'https://toopher-api.appspot.com/v1/'
36
36
 
37
+ # Creates a Toopher API consumer
38
+ #
39
+ # @param [String] key Your Toopher API Key
40
+ # @param [String] secret Your Toopher API Secret
41
+ # @param [Hash] options OAuth Options hash.
42
+ # @param [string] base_url The base URL to use for the Toopher API
37
43
  def initialize(key,secret,options={}, base_url = DEFAULT_BASE_URL)
38
44
  consumer_key = key
39
45
  consumer_secret = secret
@@ -46,15 +52,22 @@ class ToopherAPI
46
52
  @oauth_options = options
47
53
  end
48
54
 
55
+ # Create the pairing between a particular user and their mobile device
56
+ #
57
+ # @param [String] pairing_phrase The pairing phrase (from the user's mobile device).
58
+ # @param [String] user_name User name
59
+ #
60
+ # @return [PairingStatus] Information about the pairing request
49
61
  def pair(pairing_phrase, user_name)
50
- return make_pair_response(post('pairings/create', {
62
+ return PairingStatus.new(post('pairings/create', {
51
63
  'pairing_phrase' => pairing_phrase,
52
64
  'user_name' => user_name
53
65
  }))
54
66
  end
55
67
 
68
+ # Check on the status of a previous pairing request
56
69
  def get_pairing_status(pairing_request_id)
57
- return make_pair_response(get('pairings/' + pairing_request_id))
70
+ return PairingStatus.new(get('pairings/' + pairing_request_id))
58
71
  end
59
72
 
60
73
  def authenticate(pairing_id, terminal_name, action_name = '')
@@ -63,33 +76,14 @@ class ToopherAPI
63
76
  'terminal_name' => terminal_name
64
77
  }
65
78
  action_name.empty? or (parameters['action_name'] = action_name)
66
- return make_auth_response(post('authentication_requests/initiate', parameters))
79
+ return AuthenticationStatus.new(post('authentication_requests/initiate', parameters))
67
80
  end
68
81
 
69
82
  def get_authentication_status(authentication_request_id)
70
- return make_auth_response(get('authentication_requests/' + authentication_request_id))
83
+ return AuthenticationStatus.new(get('authentication_requests/' + authentication_request_id))
71
84
  end
72
85
 
73
86
  private
74
- def make_pair_response(result)
75
- return {
76
- 'id' => result['id'],
77
- 'enabled' => result['enabled'],
78
- 'user_id' => result['user']['id'],
79
- 'user_name' => result['user']['name']
80
- }
81
- end
82
- def make_auth_response(result)
83
- return {
84
- 'id' => result['id'],
85
- 'pending' => result['pending'],
86
- 'granted' => result['granted'],
87
- 'automated' => result['automated'],
88
- 'reason' => result['reason'],
89
- 'terminal_id' => result['terminal']['id'],
90
- 'terminal_name' => result['terminal']['name']
91
- }
92
- end
93
87
  def post(endpoint, parameters)
94
88
  url = URI.parse(@base_url + endpoint)
95
89
  req = Net::HTTP::Post.new(url.path)
@@ -115,3 +109,37 @@ class ToopherAPI
115
109
  return decoded
116
110
  end
117
111
  end
112
+
113
+ class PairingStatus
114
+ attr_accessor :id
115
+ attr_accessor :enabled
116
+ attr_accessor :user_id
117
+ attr_accessor :user_name
118
+
119
+ def initialize(json_obj)
120
+ @id = json_obj['id']
121
+ @enabled = json_obj['enabled']
122
+ @user_id = json_obj['user']['id']
123
+ @user_name = json_obj['user']['name']
124
+ end
125
+ end
126
+
127
+ class AuthenticationStatus
128
+ attr_accessor :id
129
+ attr_accessor :pending
130
+ attr_accessor :granted
131
+ attr_accessor :automated
132
+ attr_accessor :reason
133
+ attr_accessor :terminal_id
134
+ attr_accessor :terminal_name
135
+
136
+ def initialize(json_obj)
137
+ @id = json_obj['id']
138
+ @pending = json_obj['pending']
139
+ @granted = json_obj['granted']
140
+ @automated = json_obj['automated']
141
+ @reason = json_obj['reason']
142
+ @terminal_id = json_obj['terminal']['id']
143
+ @terminal_name = json_obj['terminal']['name']
144
+ end
145
+ end
@@ -39,10 +39,10 @@ class TestToopher < Test::Unit::TestCase
39
39
 
40
40
  toopher = ToopherAPI.new('key', 'secret', {:nonce => 'nonce', :timestamp => '0' })
41
41
  pairing = toopher.pair('immediate_pair', 'user')
42
- assert(pairing['id'] == '1', 'bad pairing id')
43
- assert(pairing['enabled'] == true, 'pairing not enabled')
44
- assert(pairing['user_id'] == '1', 'bad user id')
45
- assert(pairing['user_name'] == 'user', 'bad user name')
42
+ assert(pairing.id == '1', 'bad pairing id')
43
+ assert(pairing.enabled == true, 'pairing not enabled')
44
+ assert(pairing.user_id == '1', 'bad user id')
45
+ assert(pairing.user_name == 'user', 'bad user name')
46
46
 
47
47
  end
48
48
  def test_get_pairing_status()
@@ -59,16 +59,16 @@ class TestToopher < Test::Unit::TestCase
59
59
 
60
60
  toopher = ToopherAPI.new('key', 'secret', {:nonce => 'nonce', :timestamp => '0' })
61
61
  pairing = toopher.get_pairing_status('1')
62
- assert(pairing['id'] == '1', 'bad pairing id')
63
- assert(pairing['enabled'] == true, 'pairing not enabled')
64
- assert(pairing['user_id'] == '1', 'bad user id')
65
- assert(pairing['user_name'] == 'paired user', 'bad user name')
62
+ assert(pairing.id == '1', 'bad pairing id')
63
+ assert(pairing.enabled == true, 'pairing not enabled')
64
+ assert(pairing.user_id == '1', 'bad user id')
65
+ assert(pairing.user_name == 'paired user', 'bad user name')
66
66
 
67
67
  pairing = toopher.get_pairing_status('2')
68
- assert(pairing['id'] == '2', 'bad pairing id')
69
- assert(pairing['enabled'] == false, 'pairing should not be enabled')
70
- assert(pairing['user_id'] == '2', 'bad user id')
71
- assert(pairing['user_name'] == 'unpaired user', 'bad user name')
68
+ assert(pairing.id == '2', 'bad pairing id')
69
+ assert(pairing.enabled == false, 'pairing should not be enabled')
70
+ assert(pairing.user_id == '2', 'bad user id')
71
+ assert(pairing.user_name == 'unpaired user', 'bad user name')
72
72
  end
73
73
 
74
74
  def test_create_authentication_with_no_action()
@@ -83,13 +83,13 @@ class TestToopher < Test::Unit::TestCase
83
83
 
84
84
  toopher = ToopherAPI.new('key', 'secret', {:nonce => 'nonce', :timestamp => '0' })
85
85
  auth = toopher.authenticate('1', 'term name')
86
- assert(auth['id'] == '1', 'wrong auth id')
87
- assert(auth['pending'] == false, 'wrong auth pending')
88
- assert(auth['granted'] == true, 'wrong auth granted')
89
- assert(auth['automated'] == true, 'wrong auth automated')
90
- assert(auth['reason'] == 'some reason', 'wrong auth reason')
91
- assert(auth['terminal_id'] == '1', 'wrong auth terminal id')
92
- assert(auth['terminal_name'] == 'term name', 'wrong auth terminal name')
86
+ assert(auth.id == '1', 'wrong auth id')
87
+ assert(auth.pending == false, 'wrong auth pending')
88
+ assert(auth.granted == true, 'wrong auth granted')
89
+ assert(auth.automated == true, 'wrong auth automated')
90
+ assert(auth.reason == 'some reason', 'wrong auth reason')
91
+ assert(auth.terminal_id == '1', 'wrong auth terminal id')
92
+ assert(auth.terminal_name == 'term name', 'wrong auth terminal name')
93
93
  end
94
94
 
95
95
  def test_get_authentication_status()
@@ -106,22 +106,22 @@ class TestToopher < Test::Unit::TestCase
106
106
 
107
107
  toopher = ToopherAPI.new('key', 'secret', {:nonce => 'nonce', :timestamp => '0' })
108
108
  auth = toopher.get_authentication_status('1')
109
- assert(auth['id'] == '1', 'wrong auth id')
110
- assert(auth['pending'] == false, 'wrong auth pending')
111
- assert(auth['granted'] == true, 'wrong auth granted')
112
- assert(auth['automated'] == true, 'wrong auth automated')
113
- assert(auth['reason'] == 'some reason', 'wrong auth reason')
114
- assert(auth['terminal_id'] == '1', 'wrong auth terminal id')
115
- assert(auth['terminal_name'] == 'term name', 'wrong auth terminal name')
109
+ assert(auth.id == '1', 'wrong auth id')
110
+ assert(auth.pending == false, 'wrong auth pending')
111
+ assert(auth.granted == true, 'wrong auth granted')
112
+ assert(auth.automated == true, 'wrong auth automated')
113
+ assert(auth.reason == 'some reason', 'wrong auth reason')
114
+ assert(auth.terminal_id == '1', 'wrong auth terminal id')
115
+ assert(auth.terminal_name == 'term name', 'wrong auth terminal name')
116
116
 
117
117
  auth = toopher.get_authentication_status('2')
118
- assert(auth['id'] == '2', 'wrong auth id')
119
- assert(auth['pending'] == true, 'wrong auth pending')
120
- assert(auth['granted'] == false, 'wrong auth granted')
121
- assert(auth['automated'] == false, 'wrong auth automated')
122
- assert(auth['reason'] == 'some other reason', 'wrong auth reason')
123
- assert(auth['terminal_id'] == '2', 'wrong auth terminal id')
124
- assert(auth['terminal_name'] == 'another term name', 'wrong auth terminal name')
118
+ assert(auth.id == '2', 'wrong auth id')
119
+ assert(auth.pending == true, 'wrong auth pending')
120
+ assert(auth.granted == false, 'wrong auth granted')
121
+ assert(auth.automated == false, 'wrong auth automated')
122
+ assert(auth.reason == 'some other reason', 'wrong auth reason')
123
+ assert(auth.terminal_id == '2', 'wrong auth terminal id')
124
+ assert(auth.terminal_name == 'another term name', 'wrong auth terminal name')
125
125
  end
126
126
 
127
127
  def test_toopher_request_error()
metadata CHANGED
@@ -1,115 +1,96 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: toopher_api
3
- version: !ruby/object:Gem::Version
4
- hash: 23
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
5
  prerelease:
6
- segments:
7
- - 1
8
- - 0
9
- - 0
10
- version: 1.0.0
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - Toopher, Inc.
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2012-12-05 00:00:00 Z
19
- dependencies:
20
- - !ruby/object:Gem::Dependency
12
+ date: 2012-12-05 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
21
15
  name: oauth
22
- prerelease: false
23
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
24
17
  none: false
25
- requirements:
26
- - - ">="
27
- - !ruby/object:Gem::Version
28
- hash: 1
29
- segments:
30
- - 0
31
- - 4
32
- - 7
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
33
21
  version: 0.4.7
34
22
  type: :runtime
35
- version_requirements: *id001
36
- - !ruby/object:Gem::Dependency
37
- name: json
38
23
  prerelease: false
39
- requirement: &id002 !ruby/object:Gem::Requirement
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 0.4.7
30
+ - !ruby/object:Gem::Dependency
31
+ name: json
32
+ requirement: !ruby/object:Gem::Requirement
40
33
  none: false
41
- requirements:
42
- - - ">="
43
- - !ruby/object:Gem::Version
44
- hash: 1
45
- segments:
46
- - 1
47
- - 7
48
- - 5
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
49
37
  version: 1.7.5
50
38
  type: :runtime
51
- version_requirements: *id002
52
- - !ruby/object:Gem::Dependency
53
- name: webmock
54
39
  prerelease: false
55
- requirement: &id003 !ruby/object:Gem::Requirement
40
+ version_requirements: !ruby/object:Gem::Requirement
56
41
  none: false
57
- requirements:
58
- - - ">="
59
- - !ruby/object:Gem::Version
60
- hash: 51
61
- segments:
62
- - 1
63
- - 9
64
- - 0
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: 1.7.5
46
+ - !ruby/object:Gem::Dependency
47
+ name: webmock
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
65
53
  version: 1.9.0
66
54
  type: :development
67
- version_requirements: *id003
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: 1.9.0
68
62
  description: Synchronous interface to the toopher.com authentication api.
69
63
  email: support@toopher.com
70
64
  executables: []
71
-
72
65
  extensions: []
73
-
74
66
  extra_rdoc_files: []
75
-
76
- files:
67
+ files:
77
68
  - Rakefile
78
69
  - lib/toopher_api.rb
79
70
  - test/test_toopher_api.rb
80
71
  - demo/toopher_demo.rb
81
72
  homepage: http://toopher.org
82
73
  licenses: []
83
-
84
74
  post_install_message:
85
75
  rdoc_options: []
86
-
87
- require_paths:
76
+ require_paths:
88
77
  - lib
89
- required_ruby_version: !ruby/object:Gem::Requirement
78
+ required_ruby_version: !ruby/object:Gem::Requirement
90
79
  none: false
91
- requirements:
92
- - - ">="
93
- - !ruby/object:Gem::Version
94
- hash: 3
95
- segments:
96
- - 0
97
- version: "0"
98
- required_rubygems_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ! '>='
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
85
  none: false
100
- requirements:
101
- - - ">="
102
- - !ruby/object:Gem::Version
103
- hash: 3
104
- segments:
105
- - 0
106
- version: "0"
86
+ requirements:
87
+ - - ! '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
107
90
  requirements: []
108
-
109
91
  rubyforge_project:
110
92
  rubygems_version: 1.8.24
111
93
  signing_key:
112
94
  specification_version: 3
113
95
  summary: Interface to the toopher.com authentication api
114
96
  test_files: []
115
-