spark_api 1.0.2 → 1.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (90) hide show
  1. data/README.md +86 -3
  2. data/VERSION +1 -1
  3. data/lib/spark_api.rb +1 -0
  4. data/lib/spark_api/authentication/oauth2.rb +43 -2
  5. data/lib/spark_api/configuration/oauth2_configurable.rb +3 -1
  6. data/lib/spark_api/models.rb +1 -0
  7. data/lib/spark_api/models/listing.rb +17 -1
  8. data/lib/spark_api/models/rental_calendar.rb +26 -0
  9. data/lib/spark_api/options_hash.rb +18 -0
  10. data/script/combined_flow_example.rb +55 -0
  11. data/script/oauth2_example.rb +2 -2
  12. data/spec/fixtures/listings/rental_calendar.json +19 -0
  13. data/spec/fixtures/listings/with_rental_calendar.json +52 -0
  14. data/spec/unit/spark_api/authentication/oauth2_spec.rb +79 -1
  15. data/spec/unit/spark_api/models/listing_spec.rb +7 -0
  16. data/spec/unit/spark_api/models/rental_calendar_spec.rb +30 -0
  17. data/spec/unit/spark_api/options_hash_spec.rb +14 -0
  18. metadata +15 -96
  19. data/bin/spark_api~ +0 -8
  20. data/lib/spark_api/authentication/api_auth.rb~ +0 -104
  21. data/lib/spark_api/authentication/base_auth.rb~ +0 -47
  22. data/lib/spark_api/authentication/oauth2.rb~ +0 -199
  23. data/lib/spark_api/authentication/oauth2_impl/grant_type_base.rb~ +0 -87
  24. data/lib/spark_api/authentication/oauth2_impl/grant_type_code.rb~ +0 -49
  25. data/lib/spark_api/authentication/oauth2_impl/grant_type_password.rb~ +0 -45
  26. data/lib/spark_api/authentication/oauth2_impl/grant_type_refresh.rb~ +0 -36
  27. data/lib/spark_api/authentication/oauth2_impl/middleware.rb~ +0 -39
  28. data/lib/spark_api/authentication/oauth2_impl/password_provider.rb~ +0 -25
  29. data/lib/spark_api/cli.rb~ +0 -158
  30. data/lib/spark_api/cli/api_auth.rb~ +0 -8
  31. data/lib/spark_api/cli/oauth2.rb~ +0 -14
  32. data/lib/spark_api/cli/setup.rb~ +0 -47
  33. data/lib/spark_api/configuration.rb~ +0 -54
  34. data/lib/spark_api/configuration/yaml.rb~ +0 -101
  35. data/lib/spark_api/faraday.rb~ +0 -64
  36. data/lib/spark_api/models.rb~ +0 -33
  37. data/lib/spark_api/models/account.rb~ +0 -115
  38. data/lib/spark_api/models/base.rb~ +0 -118
  39. data/lib/spark_api/models/connect_prefs.rb~ +0 -10
  40. data/lib/spark_api/models/constraint.rb~ +0 -16
  41. data/lib/spark_api/models/contact.rb~ +0 -49
  42. data/lib/spark_api/models/custom_fields.rb~ +0 -12
  43. data/lib/spark_api/models/document.rb~ +0 -11
  44. data/lib/spark_api/models/finders.rb~ +0 -45
  45. data/lib/spark_api/models/idx_link.rb~ +0 -47
  46. data/lib/spark_api/models/listing.rb~ +0 -197
  47. data/lib/spark_api/models/listing_cart.rb~ +0 -72
  48. data/lib/spark_api/models/market_statistics.rb~ +0 -33
  49. data/lib/spark_api/models/message.rb~ +0 -21
  50. data/lib/spark_api/models/note.rb~ +0 -41
  51. data/lib/spark_api/models/notification.rb~ +0 -42
  52. data/lib/spark_api/models/open_house.rb~ +0 -24
  53. data/lib/spark_api/models/photo.rb~ +0 -70
  54. data/lib/spark_api/models/property_types.rb~ +0 -7
  55. data/lib/spark_api/models/saved_search.rb~ +0 -16
  56. data/lib/spark_api/models/shared_listing.rb~ +0 -35
  57. data/lib/spark_api/models/standard_fields.rb~ +0 -50
  58. data/lib/spark_api/models/subresource.rb~ +0 -19
  59. data/lib/spark_api/models/system_info.rb~ +0 -14
  60. data/lib/spark_api/models/tour_of_home.rb~ +0 -24
  61. data/lib/spark_api/models/video.rb~ +0 -16
  62. data/lib/spark_api/models/virtual_tour.rb~ +0 -18
  63. data/lib/spark_api/multi_client.rb~ +0 -59
  64. data/lib/spark_api/paginate.rb~ +0 -109
  65. data/lib/spark_api/primary_array.rb~ +0 -29
  66. data/lib/spark_api/request.rb~ +0 -96
  67. data/lib/spark_api/response.rb~ +0 -70
  68. data/lib/spark_api/version.rb~ +0 -4
  69. data/script/console~ +0 -6
  70. data/script/example.rb~ +0 -27
  71. data/spec/unit/flexmls_api_spec.rb~ +0 -23
  72. data/spec/unit/spark_api/authentication/api_auth_spec.rb~ +0 -169
  73. data/spec/unit/spark_api/authentication/base_auth_spec.rb~ +0 -10
  74. data/spec/unit/spark_api/authentication/oauth2_impl/grant_type_base_spec.rb~ +0 -10
  75. data/spec/unit/spark_api/authentication/oauth2_spec.rb~ +0 -205
  76. data/spec/unit/spark_api/authentication_spec.rb~ +0 -38
  77. data/spec/unit/spark_api/configuration/yaml_spec.rb~ +0 -72
  78. data/spec/unit/spark_api/configuration_spec.rb~ +0 -122
  79. data/spec/unit/spark_api/faraday_spec.rb~ +0 -90
  80. data/spec/unit/spark_api/models/contact_spec.rb~ +0 -108
  81. data/spec/unit/spark_api/models/listing_cart_spec.rb~ +0 -127
  82. data/spec/unit/spark_api/models/listing_spec.rb~ +0 -320
  83. data/spec/unit/spark_api/models/message_spec.rb~ +0 -47
  84. data/spec/unit/spark_api/models/note_spec.rb~ +0 -63
  85. data/spec/unit/spark_api/models/notification_spec.rb~ +0 -62
  86. data/spec/unit/spark_api/models/shared_listing_spec.rb~ +0 -45
  87. data/spec/unit/spark_api/multi_client_spec.rb~ +0 -56
  88. data/spec/unit/spark_api/paginate_spec.rb~ +0 -224
  89. data/spec/unit/spark_api/primary_array_spec.rb~ +0 -41
  90. data/spec/unit/spark_api/request_spec.rb~ +0 -344
@@ -1,41 +0,0 @@
1
- require './spec/spec_helper'
2
-
3
- class PrimaryModel
4
- include FlexmlsApi::Primary
5
- attr_accessor :Primary, :id, :attributes
6
- def initialize(id, prime = false)
7
- @id = id
8
- @Primary = prime
9
- @attributes = {"Primary" => prime }
10
- end
11
- end
12
-
13
- describe FlexmlsApi::PrimaryArray do
14
- it "should give me the primary element" do
15
- a = PrimaryModel.new(1)
16
- b = PrimaryModel.new(2)
17
- c = PrimaryModel.new(3)
18
- d = PrimaryModel.new(4, true)
19
- e = PrimaryModel.new(5)
20
- tester = subject.class.new([d,e])
21
- tester.primary.should eq(d)
22
- tester = subject.class.new([a,b,c,d,e])
23
- tester.primary.should eq(d)
24
- # Note, it doesn't care if there is more than one primary, just returns first in the list.
25
- b.Primary = true
26
- tester.primary.should eq(b)
27
- end
28
- it "should return nil when there is no primary element" do
29
- a = PrimaryModel.new(1)
30
- b = PrimaryModel.new(2)
31
- c = PrimaryModel.new(3)
32
- d = PrimaryModel.new(4)
33
- e = PrimaryModel.new(5)
34
- tester = subject.class.new([])
35
- tester.primary.should be(nil)
36
- tester = subject.class.new([a,b,c,d,e])
37
- tester.primary.should be(nil)
38
- end
39
- end
40
-
41
-
@@ -1,344 +0,0 @@
1
- require './spec/spec_helper'
2
-
3
- describe FlexmlsApi do
4
- describe FlexmlsApi::ClientError do
5
- subject { FlexmlsApi::ClientError.new({:message=>"OMG FAIL", :code=>1234, :status=>500}) }
6
- it "should print a helpful to_s" do
7
- subject.to_s.should == "OMG FAIL"
8
- subject.message.should == "OMG FAIL"
9
- end
10
- it "should have an api code" do
11
- subject.code.should == 1234
12
- end
13
- it "should have an http status" do
14
- subject.status.should == 500
15
- end
16
- it "should raise and exception with attached message" do
17
- expect { raise subject.class, {:message=>"My Message", :code=>1000, :status=>404}}.to raise_error(FlexmlsApi::ClientError) do |e|
18
- e.message.should == "My Message"
19
- e.code.should == 1000
20
- e.status.should == 404
21
- end
22
- expect { raise subject.class.new({:message=>"My Message", :code=>1000, :status=>404}) }.to raise_error(FlexmlsApi::ClientError) do |e|
23
- e.message.should == "My Message"
24
- e.code.should == 1000
25
- e.status.should == 404
26
- end
27
- expect { raise subject.class.new({:code=>1000, :status=>404}), "My Message"}.to raise_error(FlexmlsApi::ClientError) do |e|
28
- e.message.should == "My Message"
29
- e.code.should == 1000
30
- e.status.should == 404
31
- end
32
- expect { raise subject.class, "My Message"}.to raise_error(FlexmlsApi::ClientError) do |e|
33
- e.message.should == "My Message"
34
- e.code.should be == nil
35
- e.status.should be == nil
36
- end
37
- end
38
- end
39
-
40
- describe FlexmlsApi::ApiResponse do
41
- it "should asplode if given an invalid or empty response" do
42
- expect { FlexmlsApi::ApiResponse.new("KABOOOM") }.to raise_error(FlexmlsApi::InvalidResponse)
43
- expect { FlexmlsApi::ApiResponse.new({"D"=>{}}) }.to raise_error(FlexmlsApi::InvalidResponse)
44
- end
45
- it "should have results when successful" do
46
- r = FlexmlsApi::ApiResponse.new({"D"=>{"Success" => true, "Results" => []}})
47
- r.success?.should be(true)
48
- r.results.empty?.should be(true)
49
- end
50
- it "should have a message on error" do
51
- r = FlexmlsApi::ApiResponse.new({"D"=>{"Success" => false, "Message" => "I am a failure."}})
52
- r.success?.should be(false)
53
- r.message.should be == "I am a failure."
54
- end
55
- end
56
-
57
- describe FlexmlsApi::Request do
58
- before(:all) do
59
- stubs = Faraday::Adapter::Test::Stubs.new do |stub|
60
- stub.get('/v1/system?ApiSig=SignedToken&AuthToken=1234') { [200, {}, '{"D": {
61
- "Success": true,
62
- "Results": [{
63
- "Name": "My User",
64
- "OfficeId": "20070830184014994915000000",
65
- "Configuration": [],
66
- "Id": "20101202170654111629000000",
67
- "MlsId": "20000426143505724628000000",
68
- "Office": "test office",
69
- "Mls": "flexmls Web Demonstration Database"
70
- }]}
71
- }']
72
- }
73
- stub.get('/v1/marketstatistics/price?ApiSig=SignedToken&AuthToken=1234&Options=ActiveAverageListPrice') { [200, {}, '{"D": {
74
- "Success": true,
75
- "Results": [{
76
- "Dates": ["11/1/2010","10/1/2010","9/1/2010","8/1/2010","7/1/2010",
77
- "6/1/2010","5/1/2010","4/1/2010","3/1/2010","2/1/2010",
78
- "1/1/2010","12/1/2009"],
79
- "ActiveAverageListPrice": [100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000]
80
- }]}
81
- }']
82
- }
83
- stub.post('/v1/contacts?ApiSig=SignedToken&AuthToken=1234', '{"D":{"Contacts":[{"DisplayName":"Wades Contact","PrimaryEmail":"wade11@fbsdata.com"}]}}') { [201, {}, '{"D": {
84
- "Success": true,
85
- "Results": [{"ResourceUri": "1000"}]}}']
86
- }
87
- stub.put('/v1/contacts/1000?ApiSig=SignedToken&AuthToken=1234', '{"D":{"Contacts":[{"DisplayName":"WLMCEWENS Contact","PrimaryEmail":"wlmcewen789@fbsdata.com"}]}}') { [200, {}, '{"D": {
88
- "Success": true}}']
89
- }
90
- stub.delete('/v1/contacts/1000?ApiSig=SignedToken&AuthToken=1234') { [200, {}, '{"D": {
91
- "Success": true}}']
92
- }
93
- # EXPIRED RESPONSES
94
- stub.get('/v1/system?ApiSig=SignedToken&AuthToken=EXPIRED') { [401 , {}, '{"D": {
95
- "Success": false,
96
- "Message": "Session token has expired",
97
- "Code": 1020
98
- }}']
99
- }
100
- stub.post('/v1/contacts?ApiSig=SignedToken&AuthToken=EXPIRED', '{"D":{"Contacts":[{"DisplayName":"Wades Contact","PrimaryEmail":"wade11@fbsdata.com"}]}}') { [401 , {}, '{"D": {
101
- "Success": false,
102
- "Message": "Session token has expired",
103
- "Code": 1020
104
- }}']
105
- }
106
- # Test for really long float numbers
107
- stub.get('/v1/listings/1000?ApiSig=SignedToken&AuthToken=1234') { [200, {}, '{"D": {
108
- "Success": true,
109
- "Results": [{
110
- "ResourceUri":"/v1/listings/20101103161209156282000000",
111
- "StandardFields":{
112
- "BuildingAreaTotal":0.000000000000000000000000001,
113
- "ListPrice":9999999999999999999999999.99
114
- }
115
- }]}
116
- }']
117
- }
118
- # TEST escaped paths
119
- stub.get('/v1/test%20path%20with%20spaces?ApiSig=SignedToken&AuthToken=1234') { [200, {}, '{"D": {
120
- "Success": true,
121
- "Results": []
122
- }
123
- }']
124
- }
125
-
126
- end
127
- @connection = test_connection(stubs)
128
- end
129
-
130
- context "when successfully authenticated" do
131
- subject do
132
- class RequestTest
133
- include FlexmlsApi::Request
134
-
135
- attr_accessor *FlexmlsApi::Configuration::VALID_OPTION_KEYS
136
- attr_accessor :authenticator
137
- def initialize(session)
138
- @authenticator=MockApiAuthenticator.new(self)
139
- @authenticator.session=session
140
- end
141
- def authenticate()
142
- raise "Should not be invoked #{@session.inspect}"
143
- end
144
- def authenticated?
145
- true
146
- end
147
- def version()
148
- "v1"
149
- end
150
- attr_accessor :connection
151
- end
152
- my_s = mock_session()
153
- r = RequestTest.new(my_s)
154
- r.connection = @connection
155
- r
156
- end
157
- it "should get a service" do
158
- subject.get('/system')[0]["Name"].should == "My User"
159
- end
160
- it "should get a service with parameters" do
161
- subject.get('/marketstatistics/price', "Options" => "ActiveAverageListPrice")[0]["ActiveAverageListPrice"].should == [100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000]
162
- end
163
- it "should post to a service" do
164
- data = {"Contacts" => [{"DisplayName"=>"Wades Contact","PrimaryEmail"=>"wade11@fbsdata.com"}]}
165
- subject.post('/contacts', data)[0]["ResourceUri"].should == "1000"
166
- end
167
- it "should put to a service" do
168
- # This is a hypothetical unsupported service action at this time
169
- data = {"Contacts" => [{"DisplayName"=>"WLMCEWENS Contact","PrimaryEmail"=>"wlmcewen789@fbsdata.com"}]}
170
- subject.put('/contacts/1000', data).size.should be(0)
171
- # No validation here, if no error is raised, everything is hunky dory
172
- end
173
- it "should delete from a service" do
174
- # This is a hypothetical unsupported service action at this time
175
- subject.delete('/contacts/1000').size.should be(0)
176
- # No validation here, if no error is raised, everything is hunky dory
177
- end
178
-
179
- it "should escape a path correctly" do
180
- subject.get('/test path with spaces').length.should == 0
181
- # now try this with an already escaped path. Kaboom!
182
- expect { subject.get('/test%20path%20with%20spaces') }.to raise_error()
183
- end
184
-
185
- it "should give me BigDecimal results for large floating point numbers" do
186
- MultiJson.default_engine.should eq(:yajl)
187
- result = subject.get('/listings/1000')[0]
188
- result["StandardFields"]["BuildingAreaTotal"].should be_a(Float)
189
- pending("our JSON parser does not support large decimal types. Anyone feel like writing some c code?") do
190
- result["StandardFields"]["BuildingAreaTotal"].should be_a(BigDecimal)
191
- number = BigDecimal.new(result["StandardFields"]["BuildingAreaTotal"].to_s)
192
- number.to_s.should eq(BigDecimal.new("0.000000000000000000000000001").to_s)
193
- number = BigDecimal.new(result["StandardFields"]["ListPrice"].to_s)
194
- number.to_s.should eq(BigDecimal.new("9999999999999999999999999.99").to_s)
195
- end
196
- end
197
-
198
- end
199
-
200
- context "when unauthenticated" do
201
- subject do
202
- class RequestAuthTest
203
- include FlexmlsApi::Request
204
- attr_accessor *FlexmlsApi::Configuration::VALID_OPTION_KEYS
205
- attr_accessor :authenticator
206
- def initialize()
207
- @authenticator=MockApiAuthenticator.new(self)
208
- end
209
- def authenticate()
210
- @authenticator.session ||= mock_session()
211
- end
212
- def authenticated?
213
- @authenticator.authenticated?
214
- end
215
- def sign_token(path, params = {}, post_data="")
216
- "SignedToken"
217
- end
218
- def version()
219
- "v1"
220
- end
221
- attr_accessor :connection
222
- end
223
- r = RequestAuthTest.new
224
- r.connection = @connection
225
- r
226
- end
227
- it "should authenticate and then get a service" do
228
- subject.get('/system')[0]["Name"].should == "My User"
229
- end
230
- it "should authenticate and then post to a service" do
231
- data = {"Contacts" => [{"DisplayName"=>"Wades Contact","PrimaryEmail"=>"wade11@fbsdata.com"}]}
232
- subject.post('/contacts', data)[0]["ResourceUri"].should == "1000"
233
- end
234
- end
235
-
236
- context "when expired" do
237
- subject do
238
- class RequestExpiredTest
239
- include FlexmlsApi::Request
240
- attr_accessor *FlexmlsApi::Configuration::VALID_OPTION_KEYS
241
- attr_accessor :authenticator
242
- def initialize(session)
243
- @authenticator=MockApiAuthenticator.new(self)
244
- @authenticator.session=session
245
- @reauthenticated = false
246
- end
247
- def authenticate()
248
- @reauthenticated = true
249
- @authenticator.session = mock_session()
250
- end
251
- def authenticated?
252
- @authenticator.authenticated?
253
- end
254
-
255
- def sign_token(path, params = {}, post_data="")
256
- "SignedToken"
257
- end
258
- def version()
259
- "v1"
260
- end
261
- def reauthenticated?
262
- @reauthenticated == true
263
- end
264
- attr_accessor :connection
265
- end
266
- r = RequestExpiredTest.new(mock_expired_session())
267
- r.connection = @connection
268
- r
269
- end
270
- it "should reauthenticate and then get a service" do
271
- subject.get('/system')[0]["Name"].should == "My User"
272
- subject.reauthenticated?.should == true
273
- end
274
- it "should reauthenticate and then post to a service" do
275
- data = {"Contacts" => [{"DisplayName"=>"Wades Contact","PrimaryEmail"=>"wade11@fbsdata.com"}]}
276
- subject.post('/contacts', data)[0]["ResourceUri"].should == "1000"
277
- subject.reauthenticated?.should == true
278
- end
279
- end
280
-
281
- context "when expire response" do
282
- subject do
283
- session = FlexmlsApi::Authentication::Session.new("AuthToken" => "EXPIRED", "Expires" => (Time.now - 3600).to_s, "Roles" => "['idx']")
284
- r = RequestExpiredTest.new(session)
285
- r.connection = @connection
286
- r
287
- end
288
- it "should reauthenticate and then get a service" do
289
- subject.get('/system')[0]["Name"].should == "My User"
290
- subject.reauthenticated?.should == true
291
- end
292
- it "should reauthenticate and then post to a service" do
293
- data = {"Contacts" => [{"DisplayName"=>"Wades Contact","PrimaryEmail"=>"wade11@fbsdata.com"}]}
294
- subject.post('/contacts', data)[0]["ResourceUri"].should == "1000"
295
- subject.reauthenticated?.should == true
296
- end
297
- end
298
-
299
- context "when the server is being a real jerk on expire response" do
300
- subject do
301
- class RequestAlwaysExpiredJerkTest
302
- include FlexmlsApi::Request
303
- attr_accessor *FlexmlsApi::Configuration::VALID_OPTION_KEYS
304
- attr_accessor :authenticator
305
- def initialize()
306
- @authenticator=MockApiAuthenticator.new(self)
307
- @reauthenticated = 0
308
- end
309
- def authenticate()
310
- @reauthenticated += 1
311
- @authenticator.session = FlexmlsApi::Authentication::Session.new("AuthToken" => "EXPIRED", "Expires" => (Time.now + 60).to_s, "Roles" => "['idx']")
312
- end
313
- def authenticated?
314
- @authenticator.authenticated?
315
- end
316
- def sign_token(path, params = {}, post_data="")
317
- "SignedToken"
318
- end
319
- def version()
320
- "v1"
321
- end
322
- def reauthenticated
323
- @reauthenticated
324
- end
325
- attr_accessor :connection
326
- end
327
- r = RequestAlwaysExpiredJerkTest.new
328
- r.connection = @connection
329
- r
330
- end
331
- it "should fail horribly on a get" do
332
- expect { subject.get('/system')}.to raise_error(FlexmlsApi::PermissionDenied){ |e| e.code.should == FlexmlsApi::ResponseCodes::SESSION_TOKEN_EXPIRED }
333
- subject.reauthenticated.should == 2
334
- end
335
- it "should fail horribly on a post" do
336
- data = {"Contacts" => [{"DisplayName"=>"Wades Contact","PrimaryEmail"=>"wade11@fbsdata.com"}]}
337
- expect { subject.post('/contacts', data)}.to raise_error(FlexmlsApi::PermissionDenied){ |e| e.code.should == FlexmlsApi::ResponseCodes::SESSION_TOKEN_EXPIRED }
338
- subject.reauthenticated.should == 2
339
- end
340
- end
341
-
342
- end
343
-
344
- end