big-door 0.0.0

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.
Files changed (42) hide show
  1. data/.bundle/config +2 -0
  2. data/.document +5 -0
  3. data/.gitignore +22 -0
  4. data/Gemfile +12 -0
  5. data/LICENSE +20 -0
  6. data/README.rdoc +17 -0
  7. data/Rakefile +59 -0
  8. data/VERSION +1 -0
  9. data/features/big-door.feature +26 -0
  10. data/features/step_definitions/big-door_steps.rb +83 -0
  11. data/features/support/env.rb +5 -0
  12. data/lib/big-door.rb +127 -0
  13. data/lib/big-door/base.rb +45 -0
  14. data/lib/big-door/currency.rb +25 -0
  15. data/lib/big-door/named_transaction.rb +25 -0
  16. data/lib/big-door/named_transaction_group.rb +27 -0
  17. data/lib/big-door/request.rb +7 -0
  18. data/lib/big-door/response_codes.rb +266 -0
  19. data/lib/big-door/user.rb +49 -0
  20. data/spec/.rspec +1 -0
  21. data/spec/big-door_spec.rb +105 -0
  22. data/spec/spec_helper.rb +19 -0
  23. data/spec/support/vcr.rb +7 -0
  24. data/vcr_cassettes/NamedTransaction/NamedTransaction.yml +213 -0
  25. data/vcr_cassettes/NamedTransaction/specific.yml +6043 -0
  26. data/vcr_cassettes/award/award_summary.yml +34 -0
  27. data/vcr_cassettes/currency/currency.yml +52 -0
  28. data/vcr_cassettes/currency/currency_type.yml +31 -0
  29. data/vcr_cassettes/currency/specific.yml +2980 -0
  30. data/vcr_cassettes/good/good_summary.yml +34 -0
  31. data/vcr_cassettes/level/level_summary.yml +34 -0
  32. data/vcr_cassettes/named_transaction/named_transactions.yml +107 -0
  33. data/vcr_cassettes/named_transaction_group/all.yml +145 -0
  34. data/vcr_cassettes/named_transaction_group/specific.yml +3171 -0
  35. data/vcr_cassettes/user/add_points.yml +36 -0
  36. data/vcr_cassettes/user/all.yml +473 -0
  37. data/vcr_cassettes/user/create.yml +393 -0
  38. data/vcr_cassettes/user/currency_balance.yml +738 -0
  39. data/vcr_cassettes/user/new.yml +5660 -0
  40. data/vcr_cassettes/user/new_currency_balance.yml +470 -0
  41. data/vcr_cassettes/user/specific.yml +213 -0
  42. metadata +175 -0
@@ -0,0 +1,25 @@
1
+ module BigDoor
2
+ class Currency
3
+ include ClassMethods
4
+
5
+ def initialize(params)
6
+ params.each_pair do |key, val|
7
+ self.class.class_eval do
8
+ # overwrite the .id method and all others
9
+ self.instance_eval do
10
+ attr_accessor key
11
+ end
12
+ end
13
+ instance_variable_set("@#{key}", val)
14
+ end
15
+ end
16
+
17
+ def self.all
18
+ perform_request('get', 'currency')
19
+ end
20
+
21
+ def self.find(params={})
22
+ perform_request('get', 'currency', params)
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,25 @@
1
+ module BigDoor
2
+ class NamedTransaction
3
+ include ClassMethods
4
+
5
+ def initialize(params)
6
+ params.each_pair do |key, val|
7
+ self.class.class_eval do
8
+ next if self.instance_methods.include?(key)
9
+ self.instance_eval do
10
+ attr_accessor key
11
+ end
12
+ end
13
+ instance_variable_set("@#{key}", val)
14
+ end
15
+ end
16
+
17
+ def self.all
18
+ perform_request('get', 'named_transaction')
19
+ end
20
+
21
+ def self.find(params={})
22
+ results = perform_request('get', 'named_transaction', params)
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,27 @@
1
+ module BigDoor
2
+ class NamedTransactionGroup
3
+ include ClassMethods
4
+
5
+ def initialize(params)
6
+ params.each_pair do |key, val|
7
+ self.class.class_eval do
8
+ next if self.instance_methods.include?(key) and key.to_s != 'id'
9
+ self.instance_eval do
10
+ attr_accessor key
11
+ end
12
+ end
13
+ instance_variable_set("@#{key}", val)
14
+ end
15
+
16
+ self.named_transactions = parse_out_classes(params['named_transactions'])
17
+ end
18
+
19
+ def self.all
20
+ perform_request('get', 'named_transaction_group')
21
+ end
22
+
23
+ def self.find(params={})
24
+ results = perform_request('get', 'named_transaction_group', params)
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,7 @@
1
+ module BigDoor
2
+ class Request
3
+ include HTTParty
4
+
5
+ format :json
6
+ end
7
+ end
@@ -0,0 +1,266 @@
1
+ module BigDoor
2
+ module ResponseCodes
3
+
4
+ def self.find(args)
5
+ if args.is_a? Numeric
6
+ args = {:code => args}
7
+ end
8
+ raise ArgumentError, "Expected a Hash but got a #{args.class}" unless args.is_a? Hash
9
+ CODES.find {|x| x[args.keys.first.to_sym] == args.values.first}
10
+ end
11
+
12
+ CODES = [
13
+ {:code => 0,
14
+ :response_condition => "Created",
15
+ :log => false,
16
+ :is_error => false,
17
+ :return_objects_url_with_error => false,
18
+ :http_response => 201,
19
+ :http_code_definition => "Created",
20
+ :reason_phrase => "Created",
21
+ :header_modifications => "",
22
+ :value => ""},
23
+ {:code => 1,
24
+ :response_condition => "Updated",
25
+ :log => false,
26
+ :is_error => false,
27
+ :return_objects_url_with_error => false,
28
+ :http_response => 200,
29
+ :http_code_definition => "OK",
30
+ :reason_phrase => "Updated",
31
+ :header_modifications => "",
32
+ :value => ""},
33
+ {:code => 2,
34
+ :response_condition => "Signature Failure Error",
35
+ :log => true,
36
+ :is_error => true,
37
+ :return_objects_url_with_error => false,
38
+ :http_response => 401,
39
+ :http_code_definition => "Unauthorized",
40
+ :reason_phrase => "Invalid signature; review the Request Signature process and retry",
41
+ :header_modifications => "WWW-Authenticate",
42
+ :value => "auth-scheme = \"Digest\" nonce = \"random number\""},
43
+ {:code => 3,
44
+ :response_condition => "User Cap Error",
45
+ :log => true,
46
+ :is_error => true,
47
+ :return_objects_url_with_error => true,
48
+ :http_response => 503,
49
+ :http_code_definition => "Service Unavailable",
50
+ :reason_phrase => "User Cap met; if applicable,
51
+ wait and retry or re-configure caps.",
52
+ :header_modifications => "",
53
+ :value => ""},
54
+ {:code => 4,
55
+ :response_condition => "Replay (pre) Error",
56
+ :log => true,
57
+ :is_error => true,
58
+ :return_objects_url_with_error => true,
59
+ :http_response => 503,
60
+ :http_code_definition => "Service Unavailable",
61
+ :reason_phrase => "Token previously used (pre); use a unique token",
62
+ :header_modifications => "",
63
+ :value => ""},
64
+ {:code => 5,
65
+ :response_condition => "No Named Group Primary Error",
66
+ :log => true,
67
+ :is_error => true,
68
+ :return_objects_url_with_error => true,
69
+ :http_response => 409,
70
+ :http_code_definition => "Conflict",
71
+ :reason_phrase => "No Named Group Primary; set a Primary Named Transaction for this group.",
72
+ :header_modifications => "",
73
+ :value => ""},
74
+ {:code => 6,
75
+ :response_condition => "Variable Amounts Disallowed Error [2]",
76
+ :log => false,
77
+ :is_error => true,
78
+ :return_objects_url_with_error => true,
79
+ :http_response => 409,
80
+ :http_code_definition => "Conflict",
81
+ :reason_phrase => "Variable amounts not allowed; retry without passing amount,
82
+ or reconfigure group.",
83
+ :header_modifications => "",
84
+ :value => ""},
85
+ {:code => 7,
86
+ :response_condition => "Balance Exceeded Error",
87
+ :log => true,
88
+ :is_error => true,
89
+ :return_objects_url_with_error => true,
90
+ :http_response => 402,
91
+ :http_code_definition => "Payment Required",
92
+ :reason_phrase => "Balance exceeded for this user; advise end-user",
93
+ :header_modifications => "",
94
+ :value => ""},
95
+ {:code => 8,
96
+ :response_condition => "Transaction Error",
97
+ :log => true,
98
+ :is_error => true,
99
+ :return_objects_url_with_error => true,
100
+ :http_response => 500,
101
+ :http_code_definition => "Internal Server Error",
102
+ :reason_phrase => "Transaction error; alert service provider",
103
+ :header_modifications => "",
104
+ :value => ""},
105
+ {:code => 9,
106
+ :response_condition => "Summary Update Error [3]",
107
+ :log => false,
108
+ :is_error => true,
109
+ :return_objects_url_with_error => true,
110
+ :http_response => 500,
111
+ :http_code_definition => "Internal Server Error",
112
+ :reason_phrase => "Summary update error; alert service provider",
113
+ :header_modifications => "",
114
+ :value => ""},
115
+ {:code => 10,
116
+ :response_condition => "Replay (post) Error",
117
+ :log => true,
118
+ :is_error => true,
119
+ :return_objects_url_with_error => true,
120
+ :http_response => 503,
121
+ :http_code_definition => "Service Unavailable",
122
+ :reason_phrase => "Token previously used (post); use a unique token",
123
+ :header_modifications => "",
124
+ :value => ""},
125
+ {:code => 11,
126
+ :response_condition => "Method Not Allowed",
127
+ :log => true,
128
+ :is_error => true,
129
+ :return_objects_url_with_error => false,
130
+ :http_response => 405,
131
+ :http_code_definition => "Method Not Allowed",
132
+ :reason_phrase => "HTTP Method not allowed on this resource",
133
+ :header_modifications => "",
134
+ :value => ""},
135
+ {:code => 12,
136
+ :response_condition => "Method Not Recognized",
137
+ :log => true,
138
+ :is_error => true,
139
+ :return_objects_url_with_error => false,
140
+ :http_response => 501,
141
+ :http_code_definition => "Not Implemented",
142
+ :reason_phrase => "HTTP Method not recognized.",
143
+ :header_modifications => "",
144
+ :value => ""},
145
+ {:code => 13,
146
+ :response_condition => "User Disabled",
147
+ :log => true,
148
+ :is_error => true,
149
+ :return_objects_url_with_error => true,
150
+ :http_response => 403,
151
+ :http_code_definition => "Forbidden",
152
+ :reason_phrase => "User has been disabled; contact us to re-enable",
153
+ :header_modifications => "",
154
+ :value => ""},
155
+ {:code => 14,
156
+ :response_condition => "Named Transaction is_source misconfigured",
157
+ :log => true,
158
+ :is_error => true,
159
+ :return_objects_url_with_error => true,
160
+ :http_response => 409,
161
+ :http_code_definition => "Conflict",
162
+ :reason_phrase => "Named Transaction has incorrect is_source flag. Sales of goods must not be sources",
163
+ :header_modifications => "",
164
+ :value => ""},
165
+ {:code => 15,
166
+ :response_condition => "Missing Argument",
167
+ :log => true,
168
+ :is_error => true,
169
+ :return_objects_url_with_error => false,
170
+ :http_response => 405,
171
+ :http_code_definition => "Method Not Allowed",
172
+ :reason_phrase => "HTTP Method not allowed because it was missing a required argument",
173
+ :header_modifications => "",
174
+ :value => ""},
175
+ {:code => 16,
176
+ :response_condition => "Invalid ID or value in GET",
177
+ :log => true,
178
+ :is_error => true,
179
+ :return_objects_url_with_error => false,
180
+ :http_response => 405,
181
+ :http_code_definition => "Method Not Allowed",
182
+ :reason_phrase => "HTTP Method not allowed because it attempted to use a not owned or otherwise invalid resource.",
183
+ :header_modifications => "",
184
+ :value => ""},
185
+ {:code => 17,
186
+ :response_condition => "Invalid ID or value in POST",
187
+ :log => true,
188
+ :is_error => true,
189
+ :return_objects_url_with_error => false,
190
+ :http_response => 405,
191
+ :http_code_definition => "Method Not Allowed",
192
+ :reason_phrase => "HTTP Method not allowed because it attempted to use a not owned or otherwise invalid resource.",
193
+ :header_modifications => "",
194
+ :value => ""},
195
+ {:code => 18,
196
+ :response_condition => "Timestamp expired",
197
+ :log => true,
198
+ :is_error => true,
199
+ :return_objects_url_with_error => false,
200
+ :http_response => 401,
201
+ :http_code_definition => "Unauthorized",
202
+ :reason_phrase => "Timestamp has expired; create a new request with a current timestamp.",
203
+ :header_modifications => "",
204
+ :value => ""},
205
+ {:code => 19,
206
+ :response_condition => "Challenge-response failure",
207
+ :log => true,
208
+ :is_error => true,
209
+ :return_objects_url_with_error => false,
210
+ :http_response => 401,
211
+ :http_code_definition => "Unauthorized",
212
+ :reason_phrase => "6 challenge response failures",
213
+ :header_modifications => "",
214
+ :value => ""},
215
+ {:code => 20,
216
+ :response_condition => "Challenge-response success",
217
+ :log => true,
218
+ :is_error => false,
219
+ :return_objects_url_with_error => false,
220
+ :http_response => 200,
221
+ :http_code_definition => "OK",
222
+ :reason_phrase => "Challenge-response accepted",
223
+ :header_modifications => "",
224
+ :value => ""},
225
+ {:code => 21,
226
+ :response_condition => "Malformed input parameter",
227
+ :log => true,
228
+ :is_error => true,
229
+ :return_objects_url_with_error => true,
230
+ :http_response => 400,
231
+ :http_code_definition => "Bad Request",
232
+ :reason_phrase => "Token must be a valid hexidecimal GUID",
233
+ :header_modifications => "",
234
+ :value => ""},
235
+ {:code => 22,
236
+ :response_condition => "Disabled Named Good Collection",
237
+ :log => true,
238
+ :is_error => true,
239
+ :return_objects_url_with_error => true,
240
+ :http_response => 409,
241
+ :http_code_definition => "Conflict",
242
+ :reason_phrase => "An associated NamedGood has been disabled via its NamedGoodCollection; contact service provider",
243
+ :header_modifications => "",
244
+ :value => ""},
245
+ {:code => 23,
246
+ :response_condition => "Disabled Named Good",
247
+ :log => true,
248
+ :is_error => true,
249
+ :return_objects_url_with_error => true,
250
+ :http_response => 409,
251
+ :http_code_definition => "Conflict",
252
+ :reason_phrase => "An associated NamedGood has been disabled; contact service provider",
253
+ :header_modifications => "",
254
+ :value => ""},
255
+ {:code => 24,
256
+ :response_condition => "Resource Found",
257
+ :log => true,
258
+ :is_error => false,
259
+ :return_objects_url_with_error => false,
260
+ :http_response => 200,
261
+ :http_code_definition => "OK",
262
+ :reason_phrase => "The resource was found. Request this resource with verbosity > 0 to retrieve data.",
263
+ :header_modifications => "",
264
+ :value => ""}]
265
+ end
266
+ end
@@ -0,0 +1,49 @@
1
+ # [{"sent_good_summaries"=>[], "level_summaries"=>[], "read_only"=>0, "received_good_summaries"=>[], "guid"=>"a24ff58ca0d711df966120ad2f73afad", "created_timestamp"=>1281043384, "modified_timestamp"=>1281043384, "end_user_login"=>"testers", "award_summaries"=>[], "currency_balances"=>[], "resource_name"=>"end_user"}, {}]
2
+
3
+ module BigDoor
4
+ class User
5
+ include ClassMethods
6
+
7
+ def initialize(params)
8
+ params.each_pair do |key, val|
9
+ self.class.class_eval do
10
+ next if self.instance_methods.include?(key)
11
+ self.instance_eval do
12
+ attr_accessor key
13
+ end
14
+ end
15
+ instance_variable_set("@#{key}", val)
16
+ end
17
+ end
18
+
19
+ def self.all
20
+ perform_request('get', 'end_user')
21
+ end
22
+
23
+ def self.create(params)
24
+ perform_request('post', 'end_user', params)
25
+ end
26
+
27
+ def self.find(login)
28
+ perform_request('get', 'end_user', {:id => login})
29
+ end
30
+
31
+ def transaction
32
+ perform_request('get', "end_user/#{self.end_user_login}/transaction")
33
+ end
34
+
35
+ def add_points(named_transaction_group, points)
36
+ perform_request('post', "named_transaction_group/#{named_transaction_group.id}/execute", {:id => self.end_user_login, :amount => points})
37
+ end
38
+
39
+ def get_currency_balance(currency=nil)
40
+ params = {}
41
+ params[:id] = currency.id.to_s unless currency.nil?
42
+ result = perform_request('get', "end_user/#{self.end_user_login}/currency_balance", params)
43
+ result = parse_out_classes(result)
44
+ result['currency'] = parse_out_classes(result['currency']) unless result.empty?
45
+
46
+ return result
47
+ end
48
+ end
49
+ end
data/spec/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,105 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+ require 'ruby-debug'
3
+
4
+ describe "BigDoor" do
5
+ before do
6
+ @big_door = BigDoor::Base.new(:app_key => '0d38d9cd41b0440bbf22c0720be58b27', :secret_key => '5d0b2aed7a7149a9aae97d33f6c23b83')
7
+ end
8
+
9
+ describe "handle errors" do
10
+ it "should handle a method call that isn't valid" do
11
+ lambda { @big_door.hello_world }.should raise_error ArgumentError
12
+ end
13
+
14
+ describe "response codes" do
15
+ it "should handle an integer" do
16
+ code = 13
17
+ BigDoor::ResponseCodes.find(code)[:code].should eql(code)
18
+ end
19
+
20
+ it "should handle a hash" do
21
+ BigDoor::ResponseCodes.find(:http_response => 201)[:code].should eql(0)
22
+ end
23
+
24
+ it "should raise an error if it's passed something that isn't a hash or number" do
25
+ lambda { BigDoor::ResponseCodes.find("I'm a n00b") }.should raise_error ArgumentError
26
+ end
27
+ end
28
+ end
29
+
30
+ describe "map BigDoor users to Users object" do
31
+ it "should map params to a user object" do
32
+ params = {"sent_good_summaries"=>[], "level_summaries"=>[], "read_only"=>0, "received_good_summaries"=>[], "guid"=>"a24ff58ca0d711df966120ad2f73afad", "created_timestamp"=>1281043384, "modified_timestamp"=>1281043384, "end_user_login"=>"testers", "award_summaries"=>[], "currency_balances"=>[], "resource_name"=>"end_user"}
33
+
34
+ user = BigDoor::User.new(params)
35
+ params.keys.each do |meth|
36
+ user.public_methods.should include(meth, "#{meth}=")
37
+ end
38
+ end
39
+
40
+ it "should get all users" do
41
+ VCR.use_cassette('user/all', :record => :new_episodes) do
42
+ @users = BigDoor::User.all
43
+ end
44
+ @users.each do |user|
45
+ user.class.should eql(BigDoor::User)
46
+ end
47
+ end
48
+
49
+ it "should create a user with an id that is an email address" do
50
+ email = "fakeemailer@trada.com"
51
+ VCR.use_cassette('user/create', :record => :new_episodes) do
52
+ @response = BigDoor::User.create(:end_user_login => email, :guid => SecureRandom.hex)
53
+ end
54
+
55
+ @response.end_user_login.should eql(email)
56
+ end
57
+
58
+ it "should get a specific user" do
59
+ VCR.use_cassette('user/specific', :record => :new_episodes) do
60
+ @user = BigDoor::User.find('testers')
61
+ end
62
+
63
+ [*@user].length.should eql(1)
64
+ @user.end_user_login.should eql('testers')
65
+ end
66
+
67
+ it "should get a specific user that uses an email address" do
68
+ email = 'fakeemailer@trada.com'
69
+ VCR.use_cassette('user/specific', :record => :new_episodes) do
70
+ @user = BigDoor::User.find(email)
71
+ end
72
+
73
+ [*@user].length.should eql(1)
74
+ @user.end_user_login.should eql(email)
75
+ end
76
+ end
77
+
78
+ describe "handle currency objects" do
79
+ it "should return all the currency objects" do
80
+ VCR.use_cassette('currency/currency', :record => :new_episodes) do
81
+ @currency = BigDoor::Currency.all
82
+ end
83
+ @currency.class.should eql(BigDoor::Currency)
84
+ end
85
+ end
86
+
87
+ describe "handle named_transaction objects" do
88
+ it "should return all the named_transaction objects" do
89
+ VCR.use_cassette('named_transaction/named_transactions', :record => :new_episodes) do
90
+ @named_transactions = BigDoor::NamedTransaction.all
91
+ end
92
+ @named_transactions.first.class.should eql(BigDoor::NamedTransaction)
93
+ end
94
+ end
95
+
96
+ describe "handle named_transaction_group objects" do
97
+ it "should return all the named_transaction_group objects" do
98
+ VCR.use_cassette('named_transaction_group/all', :record => :new_episodes) do
99
+ @named_transactions_group = BigDoor::NamedTransactionGroup.all
100
+ end
101
+ @named_transactions_group.first.class.should eql(BigDoor::NamedTransactionGroup)
102
+ [*@named_transactions_group.first.named_transactions].first.class.should eql(BigDoor::NamedTransaction)
103
+ end
104
+ end
105
+ end