spark_api 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (257) hide show
  1. data/History.txt +139 -0
  2. data/LICENSE +14 -0
  3. data/README.md +153 -0
  4. data/Rakefile +18 -0
  5. data/VERSION +1 -0
  6. data/bin/spark_api +8 -0
  7. data/bin/spark_api~ +8 -0
  8. data/lib/spark_api.rb +46 -0
  9. data/lib/spark_api/authentication.rb +55 -0
  10. data/lib/spark_api/authentication/api_auth.rb +104 -0
  11. data/lib/spark_api/authentication/api_auth.rb~ +104 -0
  12. data/lib/spark_api/authentication/base_auth.rb +47 -0
  13. data/lib/spark_api/authentication/base_auth.rb~ +47 -0
  14. data/lib/spark_api/authentication/oauth2.rb +198 -0
  15. data/lib/spark_api/authentication/oauth2.rb~ +199 -0
  16. data/lib/spark_api/authentication/oauth2_impl/grant_type_base.rb +87 -0
  17. data/lib/spark_api/authentication/oauth2_impl/grant_type_base.rb~ +87 -0
  18. data/lib/spark_api/authentication/oauth2_impl/grant_type_code.rb +48 -0
  19. data/lib/spark_api/authentication/oauth2_impl/grant_type_code.rb~ +49 -0
  20. data/lib/spark_api/authentication/oauth2_impl/grant_type_password.rb +44 -0
  21. data/lib/spark_api/authentication/oauth2_impl/grant_type_password.rb~ +45 -0
  22. data/lib/spark_api/authentication/oauth2_impl/grant_type_refresh.rb +35 -0
  23. data/lib/spark_api/authentication/oauth2_impl/grant_type_refresh.rb~ +36 -0
  24. data/lib/spark_api/authentication/oauth2_impl/middleware.rb +38 -0
  25. data/lib/spark_api/authentication/oauth2_impl/middleware.rb~ +39 -0
  26. data/lib/spark_api/authentication/oauth2_impl/password_provider.rb +24 -0
  27. data/lib/spark_api/authentication/oauth2_impl/password_provider.rb~ +25 -0
  28. data/lib/spark_api/cli.rb +158 -0
  29. data/lib/spark_api/cli.rb~ +158 -0
  30. data/lib/spark_api/cli/api_auth.rb +8 -0
  31. data/lib/spark_api/cli/api_auth.rb~ +8 -0
  32. data/lib/spark_api/cli/oauth2.rb +14 -0
  33. data/lib/spark_api/cli/oauth2.rb~ +14 -0
  34. data/lib/spark_api/cli/setup.rb +47 -0
  35. data/lib/spark_api/cli/setup.rb~ +47 -0
  36. data/lib/spark_api/client.rb +27 -0
  37. data/lib/spark_api/configuration.rb +54 -0
  38. data/lib/spark_api/configuration.rb~ +54 -0
  39. data/lib/spark_api/configuration/yaml.rb +101 -0
  40. data/lib/spark_api/configuration/yaml.rb~ +101 -0
  41. data/lib/spark_api/connection.rb +42 -0
  42. data/lib/spark_api/faraday.rb +64 -0
  43. data/lib/spark_api/faraday.rb~ +64 -0
  44. data/lib/spark_api/models.rb +33 -0
  45. data/lib/spark_api/models.rb~ +33 -0
  46. data/lib/spark_api/models/account.rb +115 -0
  47. data/lib/spark_api/models/account.rb~ +115 -0
  48. data/lib/spark_api/models/base.rb +118 -0
  49. data/lib/spark_api/models/base.rb~ +118 -0
  50. data/lib/spark_api/models/connect_prefs.rb +10 -0
  51. data/lib/spark_api/models/connect_prefs.rb~ +10 -0
  52. data/lib/spark_api/models/constraint.rb +16 -0
  53. data/lib/spark_api/models/constraint.rb~ +16 -0
  54. data/lib/spark_api/models/contact.rb +49 -0
  55. data/lib/spark_api/models/contact.rb~ +49 -0
  56. data/lib/spark_api/models/custom_fields.rb +12 -0
  57. data/lib/spark_api/models/custom_fields.rb~ +12 -0
  58. data/lib/spark_api/models/document.rb +11 -0
  59. data/lib/spark_api/models/document.rb~ +11 -0
  60. data/lib/spark_api/models/finders.rb +45 -0
  61. data/lib/spark_api/models/finders.rb~ +45 -0
  62. data/lib/spark_api/models/idx_link.rb +47 -0
  63. data/lib/spark_api/models/idx_link.rb~ +47 -0
  64. data/lib/spark_api/models/listing.rb +197 -0
  65. data/lib/spark_api/models/listing.rb~ +197 -0
  66. data/lib/spark_api/models/listing_cart.rb +72 -0
  67. data/lib/spark_api/models/listing_cart.rb~ +72 -0
  68. data/lib/spark_api/models/market_statistics.rb +33 -0
  69. data/lib/spark_api/models/market_statistics.rb~ +33 -0
  70. data/lib/spark_api/models/message.rb +21 -0
  71. data/lib/spark_api/models/message.rb~ +21 -0
  72. data/lib/spark_api/models/note.rb +41 -0
  73. data/lib/spark_api/models/note.rb~ +41 -0
  74. data/lib/spark_api/models/notification.rb +42 -0
  75. data/lib/spark_api/models/notification.rb~ +42 -0
  76. data/lib/spark_api/models/open_house.rb +24 -0
  77. data/lib/spark_api/models/open_house.rb~ +24 -0
  78. data/lib/spark_api/models/photo.rb +70 -0
  79. data/lib/spark_api/models/photo.rb~ +70 -0
  80. data/lib/spark_api/models/property_types.rb +7 -0
  81. data/lib/spark_api/models/property_types.rb~ +7 -0
  82. data/lib/spark_api/models/saved_search.rb +16 -0
  83. data/lib/spark_api/models/saved_search.rb~ +16 -0
  84. data/lib/spark_api/models/shared_listing.rb +35 -0
  85. data/lib/spark_api/models/shared_listing.rb~ +35 -0
  86. data/lib/spark_api/models/standard_fields.rb +50 -0
  87. data/lib/spark_api/models/standard_fields.rb~ +50 -0
  88. data/lib/spark_api/models/subresource.rb +19 -0
  89. data/lib/spark_api/models/subresource.rb~ +19 -0
  90. data/lib/spark_api/models/system_info.rb +14 -0
  91. data/lib/spark_api/models/system_info.rb~ +14 -0
  92. data/lib/spark_api/models/tour_of_home.rb +24 -0
  93. data/lib/spark_api/models/tour_of_home.rb~ +24 -0
  94. data/lib/spark_api/models/video.rb +16 -0
  95. data/lib/spark_api/models/video.rb~ +16 -0
  96. data/lib/spark_api/models/virtual_tour.rb +18 -0
  97. data/lib/spark_api/models/virtual_tour.rb~ +18 -0
  98. data/lib/spark_api/multi_client.rb +59 -0
  99. data/lib/spark_api/multi_client.rb~ +59 -0
  100. data/lib/spark_api/paginate.rb +109 -0
  101. data/lib/spark_api/paginate.rb~ +109 -0
  102. data/lib/spark_api/primary_array.rb +29 -0
  103. data/lib/spark_api/primary_array.rb~ +29 -0
  104. data/lib/spark_api/request.rb +96 -0
  105. data/lib/spark_api/request.rb~ +96 -0
  106. data/lib/spark_api/response.rb +70 -0
  107. data/lib/spark_api/response.rb~ +70 -0
  108. data/lib/spark_api/version.rb +4 -0
  109. data/lib/spark_api/version.rb~ +4 -0
  110. data/script/console +6 -0
  111. data/script/console~ +6 -0
  112. data/script/example.rb +27 -0
  113. data/script/example.rb~ +27 -0
  114. data/spec/fixtures/accounts/all.json +160 -0
  115. data/spec/fixtures/accounts/my.json +74 -0
  116. data/spec/fixtures/accounts/my_portal.json +20 -0
  117. data/spec/fixtures/accounts/my_put.json +5 -0
  118. data/spec/fixtures/accounts/my_save.json +5 -0
  119. data/spec/fixtures/accounts/office.json +142 -0
  120. data/spec/fixtures/accounts/password_save.json +6 -0
  121. data/spec/fixtures/authentication_failure.json +7 -0
  122. data/spec/fixtures/base.json +13 -0
  123. data/spec/fixtures/contacts/contacts.json +28 -0
  124. data/spec/fixtures/contacts/my.json +19 -0
  125. data/spec/fixtures/contacts/new.json +11 -0
  126. data/spec/fixtures/contacts/new_empty.json +8 -0
  127. data/spec/fixtures/contacts/new_notify.json +11 -0
  128. data/spec/fixtures/contacts/post.json +10 -0
  129. data/spec/fixtures/contacts/tags.json +11 -0
  130. data/spec/fixtures/count.json +10 -0
  131. data/spec/fixtures/empty.json +3 -0
  132. data/spec/fixtures/errors/expired.json +7 -0
  133. data/spec/fixtures/errors/failure.json +5 -0
  134. data/spec/fixtures/errors/failure_with_constraint.json +17 -0
  135. data/spec/fixtures/errors/failure_with_msg.json +7 -0
  136. data/spec/fixtures/generic_delete.json +1 -0
  137. data/spec/fixtures/generic_failure.json +5 -0
  138. data/spec/fixtures/listing_carts/add_listing.json +13 -0
  139. data/spec/fixtures/listing_carts/add_listing_post.json +5 -0
  140. data/spec/fixtures/listing_carts/empty.json +5 -0
  141. data/spec/fixtures/listing_carts/listing_cart.json +19 -0
  142. data/spec/fixtures/listing_carts/new.json +12 -0
  143. data/spec/fixtures/listing_carts/post.json +10 -0
  144. data/spec/fixtures/listing_carts/remove_listing.json +13 -0
  145. data/spec/fixtures/listings/constraints.json +18 -0
  146. data/spec/fixtures/listings/constraints_with_pagination.json +24 -0
  147. data/spec/fixtures/listings/document_index.json +19 -0
  148. data/spec/fixtures/listings/multiple.json +69 -0
  149. data/spec/fixtures/listings/no_subresources.json +38 -0
  150. data/spec/fixtures/listings/open_houses.json +21 -0
  151. data/spec/fixtures/listings/photos/index.json +469 -0
  152. data/spec/fixtures/listings/photos/new.json +12 -0
  153. data/spec/fixtures/listings/photos/post.json +20 -0
  154. data/spec/fixtures/listings/put.json +5 -0
  155. data/spec/fixtures/listings/put_expiration_date.json +5 -0
  156. data/spec/fixtures/listings/saved_search.json +17 -0
  157. data/spec/fixtures/listings/shared_listing_get.json +14 -0
  158. data/spec/fixtures/listings/shared_listing_new.json +9 -0
  159. data/spec/fixtures/listings/shared_listing_post.json +10 -0
  160. data/spec/fixtures/listings/tour_of_homes.json +23 -0
  161. data/spec/fixtures/listings/videos_index.json +18 -0
  162. data/spec/fixtures/listings/virtual_tours_index.json +42 -0
  163. data/spec/fixtures/listings/with_documents.json +52 -0
  164. data/spec/fixtures/listings/with_permissions.json +44 -0
  165. data/spec/fixtures/listings/with_photos.json +110 -0
  166. data/spec/fixtures/listings/with_supplement.json +39 -0
  167. data/spec/fixtures/listings/with_videos.json +54 -0
  168. data/spec/fixtures/listings/with_vtour.json +48 -0
  169. data/spec/fixtures/logo_fbs.png +0 -0
  170. data/spec/fixtures/messages/new.json +14 -0
  171. data/spec/fixtures/messages/new_empty.json +7 -0
  172. data/spec/fixtures/messages/new_with_recipients.json +15 -0
  173. data/spec/fixtures/messages/post.json +5 -0
  174. data/spec/fixtures/notes/add.json +11 -0
  175. data/spec/fixtures/notes/agent_shared.json +11 -0
  176. data/spec/fixtures/notes/agent_shared_empty.json +7 -0
  177. data/spec/fixtures/notes/new.json +5 -0
  178. data/spec/fixtures/notifications/mark_read.json +1 -0
  179. data/spec/fixtures/notifications/new.json +8 -0
  180. data/spec/fixtures/notifications/new_empty.json +7 -0
  181. data/spec/fixtures/notifications/notifications.json +43 -0
  182. data/spec/fixtures/notifications/post.json +10 -0
  183. data/spec/fixtures/notifications/unread.json +10 -0
  184. data/spec/fixtures/oauth2/access.json +3 -0
  185. data/spec/fixtures/oauth2/access_with_old_refresh.json +5 -0
  186. data/spec/fixtures/oauth2/access_with_refresh.json +5 -0
  187. data/spec/fixtures/oauth2/authorization_code_body.json +7 -0
  188. data/spec/fixtures/oauth2/error.json +3 -0
  189. data/spec/fixtures/oauth2/password_body.json +7 -0
  190. data/spec/fixtures/oauth2/refresh_body.json +7 -0
  191. data/spec/fixtures/oauth2_error.json +3 -0
  192. data/spec/fixtures/property_types/property_types.json +31 -0
  193. data/spec/fixtures/session.json +10 -0
  194. data/spec/fixtures/standardfields/city.json +1031 -0
  195. data/spec/fixtures/standardfields/nearby.json +53 -0
  196. data/spec/fixtures/standardfields/standardfields.json +188 -0
  197. data/spec/fixtures/standardfields/stateorprovince.json +36 -0
  198. data/spec/fixtures/success.json +5 -0
  199. data/spec/json_helper.rb +76 -0
  200. data/spec/mock_helper.rb +124 -0
  201. data/spec/oauth2_helper.rb +68 -0
  202. data/spec/spec_helper.rb +48 -0
  203. data/spec/unit/flexmls_api_spec.rb~ +23 -0
  204. data/spec/unit/spark_api/authentication/api_auth_spec.rb +169 -0
  205. data/spec/unit/spark_api/authentication/api_auth_spec.rb~ +169 -0
  206. data/spec/unit/spark_api/authentication/base_auth_spec.rb +10 -0
  207. data/spec/unit/spark_api/authentication/base_auth_spec.rb~ +10 -0
  208. data/spec/unit/spark_api/authentication/oauth2_impl/grant_type_base_spec.rb +10 -0
  209. data/spec/unit/spark_api/authentication/oauth2_impl/grant_type_base_spec.rb~ +10 -0
  210. data/spec/unit/spark_api/authentication/oauth2_spec.rb +205 -0
  211. data/spec/unit/spark_api/authentication/oauth2_spec.rb~ +205 -0
  212. data/spec/unit/spark_api/authentication_spec.rb +38 -0
  213. data/spec/unit/spark_api/authentication_spec.rb~ +38 -0
  214. data/spec/unit/spark_api/configuration/yaml_spec.rb +72 -0
  215. data/spec/unit/spark_api/configuration/yaml_spec.rb~ +72 -0
  216. data/spec/unit/spark_api/configuration_spec.rb +122 -0
  217. data/spec/unit/spark_api/configuration_spec.rb~ +122 -0
  218. data/spec/unit/spark_api/faraday_spec.rb +90 -0
  219. data/spec/unit/spark_api/faraday_spec.rb~ +90 -0
  220. data/spec/unit/spark_api/models/account_spec.rb +176 -0
  221. data/spec/unit/spark_api/models/base_spec.rb +106 -0
  222. data/spec/unit/spark_api/models/connect_prefs_spec.rb +9 -0
  223. data/spec/unit/spark_api/models/constraint_spec.rb +19 -0
  224. data/spec/unit/spark_api/models/contact_spec.rb +108 -0
  225. data/spec/unit/spark_api/models/contact_spec.rb~ +108 -0
  226. data/spec/unit/spark_api/models/document_spec.rb +32 -0
  227. data/spec/unit/spark_api/models/listing_cart_spec.rb +127 -0
  228. data/spec/unit/spark_api/models/listing_cart_spec.rb~ +127 -0
  229. data/spec/unit/spark_api/models/listing_spec.rb +320 -0
  230. data/spec/unit/spark_api/models/listing_spec.rb~ +320 -0
  231. data/spec/unit/spark_api/models/message_spec.rb +47 -0
  232. data/spec/unit/spark_api/models/message_spec.rb~ +47 -0
  233. data/spec/unit/spark_api/models/note_spec.rb +63 -0
  234. data/spec/unit/spark_api/models/note_spec.rb~ +63 -0
  235. data/spec/unit/spark_api/models/notification_spec.rb +62 -0
  236. data/spec/unit/spark_api/models/notification_spec.rb~ +62 -0
  237. data/spec/unit/spark_api/models/open_house_spec.rb +39 -0
  238. data/spec/unit/spark_api/models/photo_spec.rb +92 -0
  239. data/spec/unit/spark_api/models/property_types_spec.rb +33 -0
  240. data/spec/unit/spark_api/models/saved_search_spec.rb +40 -0
  241. data/spec/unit/spark_api/models/shared_listing_spec.rb +45 -0
  242. data/spec/unit/spark_api/models/shared_listing_spec.rb~ +45 -0
  243. data/spec/unit/spark_api/models/standard_fields_spec.rb +60 -0
  244. data/spec/unit/spark_api/models/system_info_spec.rb +83 -0
  245. data/spec/unit/spark_api/models/tour_of_home_spec.rb +44 -0
  246. data/spec/unit/spark_api/models/video_spec.rb +36 -0
  247. data/spec/unit/spark_api/models/virtual_tour_spec.rb +44 -0
  248. data/spec/unit/spark_api/multi_client_spec.rb +56 -0
  249. data/spec/unit/spark_api/multi_client_spec.rb~ +56 -0
  250. data/spec/unit/spark_api/paginate_spec.rb +224 -0
  251. data/spec/unit/spark_api/paginate_spec.rb~ +224 -0
  252. data/spec/unit/spark_api/primary_array_spec.rb +41 -0
  253. data/spec/unit/spark_api/primary_array_spec.rb~ +41 -0
  254. data/spec/unit/spark_api/request_spec.rb +344 -0
  255. data/spec/unit/spark_api/request_spec.rb~ +344 -0
  256. data/spec/unit/spark_api_spec.rb +23 -0
  257. metadata +725 -0
@@ -0,0 +1,56 @@
1
+ require './spec/spec_helper'
2
+
3
+ # Test client implemenations for multi client switching
4
+ module FlexmlsApi
5
+ def self.test_client_a
6
+ Thread.current[:test_client_a] ||= Client.new(:api_key => "a")
7
+ end
8
+ def self.test_client_b
9
+ Client.new(:api_key => "b")
10
+ end
11
+ def self.test_client_c
12
+ Client.new(:api_key => "c")
13
+ end
14
+ end
15
+
16
+ describe FlexmlsApi::MultiClient do
17
+ it "should activate a client implemenation when activate()" do
18
+ FlexmlsApi.activate(:test_client_a)
19
+ FlexmlsApi.client.api_key.should eq('a')
20
+ FlexmlsApi.activate(:test_client_b)
21
+ FlexmlsApi.client.api_key.should eq('b')
22
+ FlexmlsApi.activate(:test_client_c)
23
+ FlexmlsApi.client.api_key.should eq('c')
24
+ FlexmlsApi.activate(:test_client_a)
25
+ FlexmlsApi.client.api_key.should eq('a')
26
+ end
27
+ it "should fail to activate symbols that do not have implementations" do
28
+ expect { FlexmlsApi.activate(:test_client_d) }.to raise_error(ArgumentError)
29
+ end
30
+
31
+ it "should temporarily activate a client implemenation when activate() block" do
32
+ FlexmlsApi.activate(:test_client_a)
33
+ FlexmlsApi.client.api_key.should eq('a')
34
+ FlexmlsApi.activate(:test_client_b) do
35
+ FlexmlsApi.client.api_key.should eq('b')
36
+ end
37
+ FlexmlsApi.client.api_key.should eq('a')
38
+ expect do
39
+ FlexmlsApi.activate(:test_client_c) do
40
+ FlexmlsApi.client.api_key.should eq('c')
41
+ raise "OH MY GOODNESS I BLEW UP!!!"
42
+ end
43
+ end.to raise_error
44
+ FlexmlsApi.client.api_key.should eq('a')
45
+ end
46
+
47
+ context "yaml" do
48
+ it "should activate a client implemenation when activate()" do
49
+ FlexmlsApi::Configuration::YamlConfig.stub(:config_path) { "spec/config/flexmls_api" }
50
+ FlexmlsApi.activate(:test_key)
51
+ FlexmlsApi.client.api_key.should eq('demo_key')
52
+ end
53
+ end
54
+
55
+ end
56
+
@@ -0,0 +1,224 @@
1
+ require './spec/spec_helper'
2
+
3
+ class PaginateResponseTester
4
+ include SparkApi::PaginateHelper
5
+ end
6
+
7
+ class PaginateModelTester < SparkApi::Models::Base
8
+ @tester = PaginateResponseTester.new
9
+ attr_accessor :val
10
+ def initialize (val)
11
+ @val = val
12
+ end
13
+ class << self
14
+ attr_accessor :options
15
+ def get(options)
16
+ @options = options
17
+ json = "{#{paginate_json}}"
18
+ hash = JSON.parse(json)
19
+ results = @tester.paginate_response([1,2,3,4,5,6,7,8,9,10], hash["Pagination"])
20
+ collect(results)
21
+ end
22
+ end
23
+ end
24
+
25
+ class PaginateModelTester50 < PaginateModelTester
26
+ @tester = PaginateResponseTester.new
27
+ def self.per_page
28
+ 50
29
+ end
30
+ end
31
+
32
+
33
+ describe SparkApi::PaginateResponse do
34
+
35
+ describe "paginate_response" do
36
+ subject { PaginateResponseTester.new }
37
+ it "should give me the first page" do
38
+ json = "{#{paginate_json}}"
39
+ hash = JSON.parse(json)
40
+ results = subject.paginate_response([1,2,3,4,5,6,7,8,9,10], hash["Pagination"])
41
+ results.offset.should eq(0)
42
+ results.next_page.should eq(2)
43
+ results.previous_page.should eq(nil)
44
+ results.current_page.should eq(1)
45
+ results.per_page.should eq(10)
46
+ results.total_pages.should eq(4)
47
+ results.total_entries.should eq(38)
48
+ results[0].should eq(1)
49
+ end
50
+ it "should give me the second page" do
51
+ json = "{#{paginate_json(2)}}"
52
+ hash = JSON.parse(json)
53
+ results = subject.paginate_response([11,12,13,14,15,16,17,18,19,20], hash["Pagination"])
54
+ results.offset.should eq(10)
55
+ results.next_page.should eq(3)
56
+ results.previous_page.should eq(1)
57
+ results.current_page.should eq(2)
58
+ results.per_page.should eq(10)
59
+ results.total_pages.should eq(4)
60
+ results.total_entries.should eq(38)
61
+ results[0].should eq(11)
62
+ end
63
+ it "should give me the third page" do
64
+ json = "{#{paginate_json(3)}}"
65
+ hash = JSON.parse(json)
66
+ results = subject.paginate_response([21,22,23,24,25,26,27,28,29,30], hash["Pagination"])
67
+ results.offset.should eq(20)
68
+ results.next_page.should eq(4)
69
+ results.previous_page.should eq(2)
70
+ results.current_page.should eq(3)
71
+ results.per_page.should eq(10)
72
+ results.total_pages.should eq(4)
73
+ results.total_entries.should eq(38)
74
+ results[0].should eq(21)
75
+ end
76
+ it "should give me the last page" do
77
+ json = "{#{paginate_json(4)}}"
78
+ hash = JSON.parse(json)
79
+ results = subject.paginate_response([31,32,33,34,35,36,37,38], hash["Pagination"])
80
+ results.offset.should eq(30)
81
+ results.next_page.should eq(nil)
82
+ results.previous_page.should eq(3)
83
+ results.current_page.should eq(4)
84
+ results.per_page.should eq(10)
85
+ results.total_pages.should eq(4)
86
+ results.total_entries.should eq(38)
87
+ results[0].should eq(31)
88
+ results[-1].should eq(38)
89
+ end
90
+ end
91
+ end
92
+
93
+ describe SparkApi::Paginate do
94
+ describe "paginate" do
95
+ it "should give me a will paginate collection" do
96
+ results = PaginateModelTester.paginate(:page => 1)
97
+ results.should be_a(WillPaginate::Collection)
98
+ results.offset.should eq(0)
99
+ results.next_page.should eq(2)
100
+ results.previous_page.should eq(nil)
101
+ results.current_page.should eq(1)
102
+ results.per_page.should eq(10)
103
+ results.total_pages.should eq(4)
104
+ results.total_entries.should eq(38)
105
+
106
+ results[0].should be_a(PaginateModelTester)
107
+ results[0].val.should eq(1)
108
+
109
+ end
110
+
111
+ it "should give me pagination options" do
112
+ PaginateModelTester.paginate(:page => 1)
113
+ opts = PaginateModelTester.options
114
+ opts["_pagination"].should eq(1)
115
+ opts["_limit"].should eq(25)
116
+ opts["_page"].should eq(1)
117
+ opts.has_key?(:page).should eq(false)
118
+ end
119
+ end
120
+
121
+ describe "per_page" do
122
+ it "should set the default model max results per page" do
123
+ results = PaginateModelTester50.paginate(:page => 1)
124
+ opts = PaginateModelTester50.options
125
+ opts["_limit"].should eq(50)
126
+ end
127
+ end
128
+
129
+ # non unit-y real world test of paginations with listings
130
+ context "paginating listings" do
131
+ before do
132
+ stub_auth_request
133
+ end
134
+
135
+ subject { SparkApi::Models::Listing }
136
+ it "gives me page one of listings" do
137
+ json = <<-JSON
138
+ {"D": {
139
+ "Success": true,
140
+ "Results": [#{ListingJson.create_all(10)}],
141
+ #{paginate_json}
142
+ }
143
+ JSON
144
+ stub_request(:get, "#{SparkApi.endpoint}/#{SparkApi.version}/listings").
145
+ with(:query => {
146
+ :ApiSig => "4a00ca0e657c824d85a5fe1007d9c52d",
147
+ :ApiUser => "foobar",
148
+ :AuthToken => "c401736bf3d3f754f07c04e460e09573",
149
+ :_limit => '10',
150
+ :_page => '1',
151
+ :_pagination => '1'
152
+ }).
153
+ to_return(:body => json)
154
+ results = subject.paginate(:page=>1, :per_page=>10)
155
+ results.should be_a(WillPaginate::Collection)
156
+ results.offset.should eq(0)
157
+ results.next_page.should eq(2)
158
+ results.previous_page.should eq(nil)
159
+ results.current_page.should eq(1)
160
+ results.per_page.should eq(10)
161
+ results.total_pages.should eq(4)
162
+ results.total_entries.should eq(38)
163
+ results.length.should eq(10)
164
+
165
+ results[0].should be_a(subject)
166
+ results[0].ListPrice.should eq(50000.0)
167
+
168
+ end
169
+ it "gives me page two of listings" do
170
+ json = <<-JSON
171
+ {"D": {
172
+ "Success": true,
173
+ "Results": [#{ListingJson.create_all(10)}],
174
+ #{paginate_json(2)}
175
+ }
176
+ JSON
177
+ stub_request(:get, "#{SparkApi.endpoint}/#{SparkApi.version}/listings").
178
+ with(:query => {
179
+ :ApiSig => "094d3851a0c4c4563baf70ca45087e30",
180
+ :ApiUser => "foobar",
181
+ :AuthToken => "c401736bf3d3f754f07c04e460e09573",
182
+ :_limit => '10',
183
+ :_page => '2',
184
+ :_pagination => '1'
185
+ }).
186
+ to_return(:body => json)
187
+ results = subject.paginate(:page=>2, :per_page=>10)
188
+ results.next_page.should eq(3)
189
+ results.previous_page.should eq(1)
190
+ results.current_page.should eq(2)
191
+ end
192
+ it "gives me page four of listings" do
193
+ json = <<-JSON
194
+ {"D": {
195
+ "Success": true,
196
+ "Results": [#{ListingJson.create_all(8)}],
197
+ #{paginate_json(4)}
198
+ }
199
+ JSON
200
+ stub_request(:get, "#{SparkApi.endpoint}/#{SparkApi.version}/listings").
201
+ with(:query => {
202
+ :ApiSig => "dcfce1fe9289c905f8d2d01cbb850edc",
203
+ :ApiUser => "foobar",
204
+ :AuthToken => "c401736bf3d3f754f07c04e460e09573",
205
+ :_limit => '10',
206
+ :_page => '4',
207
+ :_pagination => '1'
208
+ }).
209
+ to_return(:body => json)
210
+ results = subject.paginate(:page=>4, :per_page=>10)
211
+ results.next_page.should eq(nil)
212
+ results.previous_page.should eq(3)
213
+ results.current_page.should eq(4)
214
+ results.per_page.should eq(10)
215
+ results.total_pages.should eq(4)
216
+ results.total_entries.should eq(38)
217
+
218
+ results.length.should eq(8)
219
+ results[0].should be_a(subject)
220
+ results[0].ListPrice.should eq(50000.0)
221
+ end
222
+
223
+ end
224
+ end
@@ -0,0 +1,224 @@
1
+ require './spec/spec_helper'
2
+
3
+ class PaginateResponseTester
4
+ include FlexmlsApi::PaginateHelper
5
+ end
6
+
7
+ class PaginateModelTester < FlexmlsApi::Models::Base
8
+ @tester = PaginateResponseTester.new
9
+ attr_accessor :val
10
+ def initialize (val)
11
+ @val = val
12
+ end
13
+ class << self
14
+ attr_accessor :options
15
+ def get(options)
16
+ @options = options
17
+ json = "{#{paginate_json}}"
18
+ hash = JSON.parse(json)
19
+ results = @tester.paginate_response([1,2,3,4,5,6,7,8,9,10], hash["Pagination"])
20
+ collect(results)
21
+ end
22
+ end
23
+ end
24
+
25
+ class PaginateModelTester50 < PaginateModelTester
26
+ @tester = PaginateResponseTester.new
27
+ def self.per_page
28
+ 50
29
+ end
30
+ end
31
+
32
+
33
+ describe FlexmlsApi::PaginateResponse do
34
+
35
+ describe "paginate_response" do
36
+ subject { PaginateResponseTester.new }
37
+ it "should give me the first page" do
38
+ json = "{#{paginate_json}}"
39
+ hash = JSON.parse(json)
40
+ results = subject.paginate_response([1,2,3,4,5,6,7,8,9,10], hash["Pagination"])
41
+ results.offset.should eq(0)
42
+ results.next_page.should eq(2)
43
+ results.previous_page.should eq(nil)
44
+ results.current_page.should eq(1)
45
+ results.per_page.should eq(10)
46
+ results.total_pages.should eq(4)
47
+ results.total_entries.should eq(38)
48
+ results[0].should eq(1)
49
+ end
50
+ it "should give me the second page" do
51
+ json = "{#{paginate_json(2)}}"
52
+ hash = JSON.parse(json)
53
+ results = subject.paginate_response([11,12,13,14,15,16,17,18,19,20], hash["Pagination"])
54
+ results.offset.should eq(10)
55
+ results.next_page.should eq(3)
56
+ results.previous_page.should eq(1)
57
+ results.current_page.should eq(2)
58
+ results.per_page.should eq(10)
59
+ results.total_pages.should eq(4)
60
+ results.total_entries.should eq(38)
61
+ results[0].should eq(11)
62
+ end
63
+ it "should give me the third page" do
64
+ json = "{#{paginate_json(3)}}"
65
+ hash = JSON.parse(json)
66
+ results = subject.paginate_response([21,22,23,24,25,26,27,28,29,30], hash["Pagination"])
67
+ results.offset.should eq(20)
68
+ results.next_page.should eq(4)
69
+ results.previous_page.should eq(2)
70
+ results.current_page.should eq(3)
71
+ results.per_page.should eq(10)
72
+ results.total_pages.should eq(4)
73
+ results.total_entries.should eq(38)
74
+ results[0].should eq(21)
75
+ end
76
+ it "should give me the last page" do
77
+ json = "{#{paginate_json(4)}}"
78
+ hash = JSON.parse(json)
79
+ results = subject.paginate_response([31,32,33,34,35,36,37,38], hash["Pagination"])
80
+ results.offset.should eq(30)
81
+ results.next_page.should eq(nil)
82
+ results.previous_page.should eq(3)
83
+ results.current_page.should eq(4)
84
+ results.per_page.should eq(10)
85
+ results.total_pages.should eq(4)
86
+ results.total_entries.should eq(38)
87
+ results[0].should eq(31)
88
+ results[-1].should eq(38)
89
+ end
90
+ end
91
+ end
92
+
93
+ describe FlexmlsApi::Paginate do
94
+ describe "paginate" do
95
+ it "should give me a will paginate collection" do
96
+ results = PaginateModelTester.paginate(:page => 1)
97
+ results.should be_a(WillPaginate::Collection)
98
+ results.offset.should eq(0)
99
+ results.next_page.should eq(2)
100
+ results.previous_page.should eq(nil)
101
+ results.current_page.should eq(1)
102
+ results.per_page.should eq(10)
103
+ results.total_pages.should eq(4)
104
+ results.total_entries.should eq(38)
105
+
106
+ results[0].should be_a(PaginateModelTester)
107
+ results[0].val.should eq(1)
108
+
109
+ end
110
+
111
+ it "should give me pagination options" do
112
+ PaginateModelTester.paginate(:page => 1)
113
+ opts = PaginateModelTester.options
114
+ opts["_pagination"].should eq(1)
115
+ opts["_limit"].should eq(25)
116
+ opts["_page"].should eq(1)
117
+ opts.has_key?(:page).should eq(false)
118
+ end
119
+ end
120
+
121
+ describe "per_page" do
122
+ it "should set the default model max results per page" do
123
+ results = PaginateModelTester50.paginate(:page => 1)
124
+ opts = PaginateModelTester50.options
125
+ opts["_limit"].should eq(50)
126
+ end
127
+ end
128
+
129
+ # non unit-y real world test of paginations with listings
130
+ context "paginating listings" do
131
+ before do
132
+ stub_auth_request
133
+ end
134
+
135
+ subject { FlexmlsApi::Models::Listing }
136
+ it "gives me page one of listings" do
137
+ json = <<-JSON
138
+ {"D": {
139
+ "Success": true,
140
+ "Results": [#{ListingJson.create_all(10)}],
141
+ #{paginate_json}
142
+ }
143
+ JSON
144
+ stub_request(:get, "#{FlexmlsApi.endpoint}/#{FlexmlsApi.version}/listings").
145
+ with(:query => {
146
+ :ApiSig => "4a00ca0e657c824d85a5fe1007d9c52d",
147
+ :ApiUser => "foobar",
148
+ :AuthToken => "c401736bf3d3f754f07c04e460e09573",
149
+ :_limit => '10',
150
+ :_page => '1',
151
+ :_pagination => '1'
152
+ }).
153
+ to_return(:body => json)
154
+ results = subject.paginate(:page=>1, :per_page=>10)
155
+ results.should be_a(WillPaginate::Collection)
156
+ results.offset.should eq(0)
157
+ results.next_page.should eq(2)
158
+ results.previous_page.should eq(nil)
159
+ results.current_page.should eq(1)
160
+ results.per_page.should eq(10)
161
+ results.total_pages.should eq(4)
162
+ results.total_entries.should eq(38)
163
+ results.length.should eq(10)
164
+
165
+ results[0].should be_a(subject)
166
+ results[0].ListPrice.should eq(50000.0)
167
+
168
+ end
169
+ it "gives me page two of listings" do
170
+ json = <<-JSON
171
+ {"D": {
172
+ "Success": true,
173
+ "Results": [#{ListingJson.create_all(10)}],
174
+ #{paginate_json(2)}
175
+ }
176
+ JSON
177
+ stub_request(:get, "#{FlexmlsApi.endpoint}/#{FlexmlsApi.version}/listings").
178
+ with(:query => {
179
+ :ApiSig => "094d3851a0c4c4563baf70ca45087e30",
180
+ :ApiUser => "foobar",
181
+ :AuthToken => "c401736bf3d3f754f07c04e460e09573",
182
+ :_limit => '10',
183
+ :_page => '2',
184
+ :_pagination => '1'
185
+ }).
186
+ to_return(:body => json)
187
+ results = subject.paginate(:page=>2, :per_page=>10)
188
+ results.next_page.should eq(3)
189
+ results.previous_page.should eq(1)
190
+ results.current_page.should eq(2)
191
+ end
192
+ it "gives me page four of listings" do
193
+ json = <<-JSON
194
+ {"D": {
195
+ "Success": true,
196
+ "Results": [#{ListingJson.create_all(8)}],
197
+ #{paginate_json(4)}
198
+ }
199
+ JSON
200
+ stub_request(:get, "#{FlexmlsApi.endpoint}/#{FlexmlsApi.version}/listings").
201
+ with(:query => {
202
+ :ApiSig => "dcfce1fe9289c905f8d2d01cbb850edc",
203
+ :ApiUser => "foobar",
204
+ :AuthToken => "c401736bf3d3f754f07c04e460e09573",
205
+ :_limit => '10',
206
+ :_page => '4',
207
+ :_pagination => '1'
208
+ }).
209
+ to_return(:body => json)
210
+ results = subject.paginate(:page=>4, :per_page=>10)
211
+ results.next_page.should eq(nil)
212
+ results.previous_page.should eq(3)
213
+ results.current_page.should eq(4)
214
+ results.per_page.should eq(10)
215
+ results.total_pages.should eq(4)
216
+ results.total_entries.should eq(38)
217
+
218
+ results.length.should eq(8)
219
+ results[0].should be_a(subject)
220
+ results[0].ListPrice.should eq(50000.0)
221
+ end
222
+
223
+ end
224
+ end