reward_station-gilman 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (34) hide show
  1. data/.gitignore +5 -0
  2. data/.rspec +1 -0
  3. data/.rvmrc +1 -0
  4. data/Gemfile +9 -0
  5. data/LICENSE +0 -0
  6. data/README.md +228 -0
  7. data/Rakefile +5 -0
  8. data/lib/reward_station.rb +9 -0
  9. data/lib/reward_station/client.rb +210 -0
  10. data/lib/reward_station/errors.rb +33 -0
  11. data/lib/reward_station/responses/award_points.xml +12 -0
  12. data/lib/reward_station/responses/award_points_invalid_token.xml +12 -0
  13. data/lib/reward_station/responses/create_user.xml +34 -0
  14. data/lib/reward_station/responses/create_user_exists.xml +9 -0
  15. data/lib/reward_station/responses/create_user_missing_info.xml +9 -0
  16. data/lib/reward_station/responses/return_point_summary.xml +22 -0
  17. data/lib/reward_station/responses/return_point_summary_invalid_token.xml +12 -0
  18. data/lib/reward_station/responses/return_popular_products.xml +362 -0
  19. data/lib/reward_station/responses/return_popular_products_invalid_token.xml +10 -0
  20. data/lib/reward_station/responses/return_token.xml +10 -0
  21. data/lib/reward_station/responses/return_token_invalid.xml +10 -0
  22. data/lib/reward_station/responses/return_user.xml +35 -0
  23. data/lib/reward_station/responses/return_user_invalid_token.xml +10 -0
  24. data/lib/reward_station/responses/return_user_invalid_user.xml +10 -0
  25. data/lib/reward_station/stub_client.rb +22 -0
  26. data/lib/reward_station/stub_response.rb +45 -0
  27. data/lib/reward_station/version.rb +3 -0
  28. data/lib/saml/auth_response.rb +176 -0
  29. data/lib/wsdl/reward_services.xml +511 -0
  30. data/reward_station.gemspec +25 -0
  31. data/spec/reward_station/service_spec.rb +351 -0
  32. data/spec/savon_helper.rb +82 -0
  33. data/spec/spec_helper.rb +13 -0
  34. metadata +132 -0
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+
4
+ require "reward_station/version"
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "reward_station-gilman"
8
+ s.version = RewardStation::VERSION
9
+ s.platform = Gem::Platform::RUBY
10
+ s.authors = ["Stepan Filatov", 'Cloud Castle Inc.']
11
+ s.email = ["filatov.st@gmail.com"]
12
+ s.homepage = "https://github.com/cloudcastle/reward_station"
13
+ s.summary = %q{Reward Station is a client library for rewardstation.com}
14
+ s.description = %q{}
15
+
16
+ s.rubyforge_project = "reward_station"
17
+
18
+ s.files = `git ls-files`.split("\n")
19
+ s.test_files = `git ls-files -- {spec}/*`.split("\n")
20
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
21
+ s.require_paths = ["lib"]
22
+
23
+ s.add_dependency 'savon', ">= 0.9.6"
24
+ s.add_dependency "ruby-saml-bekk", '>= 0.3.3'
25
+ end
@@ -0,0 +1,351 @@
1
+ require 'spec_helper'
2
+
3
+ describe RewardStation::Client do
4
+
5
+ let(:service) { RewardStation::Client.new :client_id => '100080', :client_password => 'fM6Rv4moz#', :token => "e285e1ed-2356-4676-a554-99d79e6284b0" }
6
+
7
+ describe "required options" do
8
+ it "should raise ArgumentError on missing client_id" do
9
+ lambda{ RewardStation::Client.new :client_password => "" }.should raise_error(ArgumentError)
10
+ end
11
+
12
+ it "should raise ArgumentError on missing client_password" do
13
+ lambda{ RewardStation::Client.new :client_id => "" }.should raise_error(ArgumentError)
14
+ end
15
+
16
+ it "should not rause ArgumentError if all required parameters present" do
17
+ lambda{ RewardStation::Client.new :client_id => "", :client_password => "" }.should_not raise_error(ArgumentError)
18
+ end
19
+ end
20
+
21
+ describe "new_token_callback" do
22
+ it "should raise ArgumentError if options is not lambda or proc" do
23
+ lambda{ RewardStation::Client.new :client_id => "", :client_password => "", :new_token_callback => "" }.should raise_error(ArgumentError)
24
+ end
25
+
26
+ it "should not raise ArgumentError if option is lambda or proc" do
27
+ lambda{ RewardStation::Client.new :client_id => "", :client_password => "", :new_token_callback => Proc.new { } }.should_not raise_error(ArgumentError)
28
+ end
29
+ end
30
+
31
+ describe 'stub' do
32
+ it "should create stub" do
33
+ RewardStation::Client.stub.should be_a(RewardStation::StubClient)
34
+ end
35
+
36
+ describe 'stub service' do
37
+ let(:service) {RewardStation::Client.stub}
38
+
39
+ it "should return token" do
40
+ service.return_token.should eq("e285e1ed-2356-4676-a554-99d79e6284b0")
41
+ end
42
+ end
43
+ end
44
+
45
+ describe "return_token" do
46
+
47
+ describe "on valid response" do
48
+ before { savon.stub(:return_token).and_return(:return_token) }
49
+
50
+ it "should return valid token" do
51
+ service.return_token.should eq("e285e1ed-2356-4676-a554-99d79e6284b0")
52
+ end
53
+
54
+ it "should not raise InvalidAccount exception" do
55
+ lambda{ service.return_token }.should_not raise_error(RewardStation::InvalidAccount)
56
+ end
57
+ end
58
+
59
+ describe "on invalid account response" do
60
+ before { savon.stub(:return_token).and_return(:return_token_invalid) }
61
+
62
+ it "should raise InvalidAccount exception" do
63
+ lambda{ service.return_token }.should raise_error(RewardStation::InvalidAccount)
64
+ end
65
+ end
66
+
67
+ describe "on soap error" do
68
+ before { savon.stub(:return_token).and_return.raises_soap_fault }
69
+
70
+ it "should raise ConnectionError exception" do
71
+ lambda{ service.return_token }.should raise_error(RewardStation::ConnectionError)
72
+ end
73
+ end
74
+
75
+ describe "on http error" do
76
+ before { savon.stub(:return_token).and_return(:code => 404).raises_http_error }
77
+
78
+ it "should raise HttpError exception" do
79
+ lambda{ service.return_token }.should raise_error(RewardStation::ConnectionError)
80
+ end
81
+ end
82
+ end
83
+
84
+ describe "award_points" do
85
+ let(:service) {
86
+ RewardStation::Client.new :client_id => '100080',
87
+ :client_password => 'fM6Rv4moz#',
88
+ :program_id => 90,
89
+ :point_reason_code_id => 129,
90
+ :token => "e285e1ed-2356-4676-a554-99d79e6284b0"
91
+ }
92
+
93
+ describe "on valid response" do
94
+ before { savon.stub(:award_points).and_return(:award_points) }
95
+
96
+ it "should return valid confirm code" do
97
+ service.award_points(130, 10, "Action 'Some' taken").should eq("9376")
98
+ end
99
+ end
100
+
101
+ describe "on invalid token response" do
102
+ before { savon.stub(:award_points).and_return(:award_points_invalid_token) }
103
+
104
+ it "should not raise InvalidToken exception" do
105
+ lambda{ service.award_points(130, 10, "Action 'Some' taken") }.should_not raise_error(RewardStation::InvalidToken)
106
+ end
107
+ end
108
+
109
+ describe "on soap error" do
110
+ before { savon.stub(:award_points).and_return.raises_soap_fault }
111
+
112
+ it "should raise ConnectionError exception" do
113
+ lambda{ service.award_points(130, 10, "Action 'Some' taken") }.should raise_error(RewardStation::ConnectionError)
114
+ end
115
+ end
116
+
117
+ describe "on http error" do
118
+ before { savon.stub(:award_points).and_return(:code => 404).raises_http_error }
119
+
120
+ it "should raise HttpError exception" do
121
+ lambda{ service.award_points(130, 10, "Action 'Some' taken") }.should raise_error(RewardStation::ConnectionError)
122
+ end
123
+ end
124
+
125
+ end
126
+
127
+ describe "return_user" do
128
+
129
+ describe "on valid response" do
130
+ before { savon.stub(:return_user).and_return(:return_user) }
131
+
132
+ it "should return valid user data" do
133
+ service.return_user(130).should eq(:user_id => '6725',
134
+ :client_id => '100080',
135
+ :user_name => 'john3@company.com',
136
+ :encrypted_password => nil,
137
+ :first_name => 'John',
138
+ :last_name => 'Smith',
139
+ :address_one => nil,
140
+ :address_two => nil,
141
+ :city => nil,
142
+ :state_code => nil,
143
+ :province => nil,
144
+ :postal_code => nil,
145
+ :country_code => 'USA',
146
+ :phone => nil,
147
+ :email => 'john@company.com',
148
+ :organization_id => '0',
149
+ :organization_name => nil,
150
+ :rep_type_id => '0',
151
+ :client_region_id => '0',
152
+ :is_active => true,
153
+ :point_balance => '10',
154
+ :manager_id => '0',
155
+ :error_message => nil)
156
+ end
157
+
158
+ it "should not raise InvalidToken exception" do
159
+ lambda{ service.return_user(130) }.should_not raise_error(RewardStation::InvalidToken)
160
+ end
161
+ end
162
+
163
+ describe "on invalid token response" do
164
+ before { savon.stub(:return_user).and_return(:return_user_invalid_token) }
165
+
166
+ it "should not raise InvalidToken exception" do
167
+ lambda{ service.return_user(130) }.should_not raise_error(RewardStation::InvalidToken)
168
+ end
169
+ end
170
+
171
+ describe "on invalid user response" do
172
+ before { savon.stub(:return_user).and_return(:return_user_invalid_user) }
173
+
174
+ it "should raise InvalidUser exception" do
175
+ lambda{ service.return_user(130) }.should raise_error(RewardStation::InvalidUser)
176
+ end
177
+ end
178
+
179
+ describe "on soap error" do
180
+ before { savon.stub(:return_user).and_return.raises_soap_fault }
181
+
182
+ it "should raise ConnectionError exception" do
183
+ lambda{ service.return_user(130) }.should raise_error(RewardStation::ConnectionError)
184
+ end
185
+ end
186
+
187
+ describe "on http error" do
188
+ before { savon.stub(:return_user).and_return(:code => 404).raises_http_error }
189
+
190
+ it "should raise HttpError exception" do
191
+ lambda{ service.return_user(130) }.should raise_error(RewardStation::ConnectionError)
192
+ end
193
+ end
194
+ end
195
+
196
+ describe "return_point_summary" do
197
+ describe "on valid request" do
198
+ before { savon.stub(:return_point_summary).and_return(:return_point_summary) }
199
+
200
+ it "should return valid summary" do
201
+ service.return_point_summary(130).should eq(:user_id => '577',
202
+ :is_active => true,
203
+ :points_earned => '465',
204
+ :points_redeemed => '0',
205
+ :points_credited => '0',
206
+ :points_balance => '465')
207
+ end
208
+
209
+ it "should not raise InvalidToken exception" do
210
+ lambda{ service.return_point_summary(130) }.should_not raise_error(RewardStation::InvalidToken)
211
+ end
212
+ end
213
+
214
+ describe "on invalid token request" do
215
+ before { savon.stub(:return_point_summary).and_return(:return_point_summary_invalid_token) }
216
+
217
+ it "should not raise InvalidToken exception" do
218
+ lambda{ service.return_point_summary(130) }.should_not raise_error(RewardStation::InvalidToken)
219
+ end
220
+ end
221
+
222
+ describe "on soap error" do
223
+ before { savon.stub(:return_point_summary).and_return.raises_soap_fault }
224
+
225
+ it "should raise ConnectionError exception" do
226
+ lambda{ service.return_point_summary(130) }.should raise_error(RewardStation::ConnectionError)
227
+ end
228
+ end
229
+
230
+ describe "on http error" do
231
+ before { savon.stub(:return_point_summary).and_return(:code => 404).raises_http_error }
232
+
233
+ it "should raise HttpError exception" do
234
+ lambda{ service.return_point_summary(130) }.should raise_error(RewardStation::ConnectionError)
235
+ end
236
+ end
237
+ end
238
+
239
+ describe "create_user" do
240
+ describe 'missing information' do
241
+
242
+ before { savon.stub(:update_user).and_return(:create_user_missing_info) }
243
+
244
+ it "should raise MissingInformation error" do
245
+ lambda { service.create_user :organization_id => '150' }.should raise_error(RewardStation::MissingInformation)
246
+ end
247
+ end
248
+ end
249
+
250
+ describe "update_user" do
251
+ let(:service) { RewardStation::Client.new :client_id => '100080', :client_password => 'fM6Rv4moz#', :organization_id => '150', :token => "e285e1ed-2356-4676-a554-99d79e6284b0" }
252
+
253
+ describe "on create user valid request" do
254
+ before { savon.stub(:update_user).and_return(:create_user) }
255
+
256
+ it "should return valid response" do
257
+ service.create_user(
258
+ :email => 'john5@company.com',
259
+ :first_name => 'John',
260
+ :last_name => 'Smith',
261
+ :user_name => 'john5@company.com',
262
+ :balance => 0
263
+ ).should eq(:user_id => '6727',
264
+ :client_id => '100080',
265
+ :user_name => 'john5@company.com',
266
+ :email => 'john5@company.com',
267
+ :encrypted_password => nil,
268
+ :first_name => 'John',
269
+ :last_name => 'Smith',
270
+ :address_one => nil,
271
+ :address_two => nil,
272
+ :city => nil,
273
+ :state_code => nil,
274
+ :province => nil,
275
+ :postal_code => nil,
276
+ :country_code => 'USA',
277
+ :phone => nil,
278
+ :organization_id => '150',
279
+ :organization_name => nil,
280
+ :rep_type_id => '0',
281
+ :client_region_id => '0',
282
+
283
+ :is_active => true,
284
+ :point_balance => '0',
285
+ :manager_id => '0',
286
+ :error_message => nil)
287
+ end
288
+
289
+ it "should not raise InvalidToken exception" do
290
+ lambda{
291
+ service.create_user(:email => 'john5@company.com',
292
+ :first_name => 'John',
293
+ :last_name => 'Smith',
294
+ :user_name => 'john5@company.com',
295
+ :balance => 0)
296
+ }.should_not raise_error(RewardStation::InvalidToken)
297
+ end
298
+ end
299
+
300
+ describe "on create user invalid request" do
301
+ before { savon.stub(:update_user).and_return(:create_user_exists) }
302
+
303
+ it "should raise UserAlreadyExists exception" do
304
+ lambda{
305
+ service.update_user(130, {
306
+ :email => 'john5@company.com',
307
+ :first_name => 'John',
308
+ :last_name => 'Smith',
309
+ :user_name => 'john5@company.com',
310
+ :balance => 0
311
+ })
312
+ }.should raise_error(RewardStation::UserAlreadyExists)
313
+ end
314
+ end
315
+
316
+ end
317
+
318
+ describe "return_popular_products" do
319
+ describe "on valid request" do
320
+ before { savon.stub(:return_popular_products).and_return(:return_popular_products) }
321
+
322
+ it "should return valid response" do
323
+ products = service.return_popular_products(130)
324
+ products.should be_a(Array)
325
+ products.size.should eq(35)
326
+ products.first.should eq(:product_id => 'MC770LLA',
327
+ :name => 'iPad 2 with Wifi - 32GB',
328
+ :description => 'The NEW Apple iPad 2 - Thinner, lighter, and full of great ideas. Once you pick up iPad 2, it’ll be hard to put down. That’s the idea behind the all-new design. It’s 33 percent thinner and up to 15 percent lighter, so it feels even more comfortable in your hands. And, it makes surfing the web, checking email, watching movies, and reading books so natural, you might forget there’s incredible technology under your fingers.<br><br><b>Dual-core A5 chip</b>.<br> Two powerful cores in one A5 chip mean iPad can do twice the work at once. You’ll notice the difference when you’re surfing the web, watching movies, making FaceTime video calls, gaming, and going from app to app to app. Multitasking is smoother, apps load faster, and everything just works better.<br><br><b>Superfast graphics</b>. <br>With up to nine times the graphics performance, gameplay on iPad is even smoother and more realistic. And faster graphics help apps perform better — especially those with video. You’ll see it when you’re scrolling through your photo library, editing video with iMovie, and viewing animations in Keynote.<br><br><b>Battery life keeps on going. So you can, too.</b><br> Even with the new thinner and lighter design, iPad has the same amazing 10-hour battery life. That’s enough juice for one flight across the ocean, or one movie-watching all-nighter, or a week’s commute across town. The power-efficient A5 chip and iOS keep battery life from fading away, so you can get carried away.<br><br><b>Two cameras.</b><br> You’ll see two cameras on iPad — one on the front and one on the back. They may be tiny, but they’re a big deal. They’re designed for FaceTime video calling, and they work together so you can talk to your favorite people and see them smile and laugh back at you. The front camera puts you and your friend face-to-face. Switch to the back camera during your video call to share where you are, who you’re with, or what’s going on around you. When you’re not using FaceTime, let the back camera roll if you see something movie-worthy. It’s HD, so whatever you shoot is a mini-masterpiece. And you can take wacky snapshots in Photo Booth. It’s the most fun a face can have.<br><br><b>Due to the demand for this item, please allow up to 8-10 weeks for delivery</b>.',
329
+ :points => '10927',
330
+ :category => 'Office & Computer',
331
+ :manufacturer => 'Apple',
332
+ :small_image_url => 'https://www.rewardstation.com/catalogimages/MC769LLA.gif',
333
+ :large_image_url => 'https://www.rewardstation.com/catalogimages/MC769LLA.jpg')
334
+ end
335
+
336
+ it "should not raise InvalidToken exception" do
337
+ lambda{ service.return_popular_products(130) }.should_not raise_error(RewardStation::InvalidToken)
338
+ end
339
+ end
340
+
341
+ describe "on create user invalid token request" do
342
+ before { savon.stub(:return_popular_products).and_return(:return_popular_products_invalid_token) }
343
+
344
+ it "should not raise InvalidToken exception" do
345
+ lambda{ service.return_popular_products(130) }.should_not raise_error(RewardStation::InvalidToken)
346
+ end
347
+ end
348
+
349
+ end
350
+
351
+ end
@@ -0,0 +1,82 @@
1
+ module Savon
2
+ # = Savon::Spec::Macros
3
+ #
4
+ # Include this module into your RSpec tests to mock/stub Savon SOAP requests.
5
+ module Macros
6
+ def savon
7
+ Savon::SOAP::Response.any_instance.stub(:soap_fault?).and_return(false)
8
+ Savon::SOAP::Response.any_instance.stub(:http_error?).and_return(false)
9
+ Savon::Mock.new
10
+ end
11
+ end
12
+
13
+ # = Savon::Spec::Mock
14
+ #
15
+ # Mocks/stubs SOAP requests executed by Savon.
16
+ class Mock
17
+
18
+ def expects(soap_action)
19
+ setup :expects, soap_action
20
+ self
21
+ end
22
+
23
+ def stub(soap_action)
24
+ setup :stub, soap_action
25
+ self
26
+ end
27
+
28
+ # Expects a given SOAP body Hash to be used.
29
+ def with(soap_body)
30
+ Savon::SOAP::XML.any_instance.expects(:body=).with(soap_body) if mock_method == :expects
31
+ self
32
+ end
33
+
34
+ def never
35
+ httpi_mock.never
36
+ self
37
+ end
38
+
39
+ # Sets up HTTPI to return a given +response+.
40
+ def and_return(response = nil)
41
+ http = { :code => 200, :headers => {}, :body => "" }
42
+
43
+ case response
44
+ when Symbol then http[:body] = RewardStation::StubResponse[soap_action, response]
45
+ when Hash then http.merge! response
46
+ when String then http[:body] = response
47
+ end
48
+
49
+ httpi_mock.and_return HTTPI::Response.new(http[:code], http[:headers], http[:body])
50
+ self
51
+ end
52
+
53
+ # Sets up Savon to respond like there was a SOAP fault.
54
+ def raises_soap_fault
55
+ Savon::SOAP::Response.any_instance.stub(:soap_fault?).and_return(true)
56
+ self
57
+ end
58
+
59
+ def raises_http_error
60
+ Savon::SOAP::Response.any_instance.stub(:soap_fault?).and_return(false)
61
+ Savon::SOAP::Response.any_instance.stub(:http_error?).and_return(true)
62
+ self
63
+ end
64
+
65
+ private
66
+
67
+ def setup(mock_method, soap_action)
68
+ self.mock_method = mock_method
69
+ self.soap_action = soap_action
70
+ self.httpi_mock = HTTPI.send(mock_method, :post)
71
+ end
72
+
73
+ attr_accessor :mock_method
74
+ attr_accessor :httpi_mock
75
+
76
+ attr_reader :soap_action
77
+ def soap_action=(soap_action)
78
+ @soap_action = soap_action.kind_of?(Symbol) ? soap_action.to_s.lower_camelcase : soap_action
79
+ end
80
+ end
81
+
82
+ end