reviewed 0.1.2 → 0.1.4

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 (86) hide show
  1. data/README.md +12 -5
  2. data/lib/faraday/api_key.rb +14 -0
  3. data/lib/faraday/errors.rb +14 -0
  4. data/lib/reviewed/article.rb +3 -1
  5. data/lib/reviewed/award.rb +4 -0
  6. data/lib/reviewed/base.rb +23 -11
  7. data/lib/reviewed/client.rb +47 -26
  8. data/lib/reviewed/collection.rb +8 -8
  9. data/lib/reviewed/product.rb +2 -0
  10. data/lib/reviewed/request.rb +43 -0
  11. data/lib/reviewed/version.rb +1 -1
  12. data/lib/reviewed.rb +13 -20
  13. data/reviewed.gemspec +1 -0
  14. data/spec/article_spec.rb +21 -21
  15. data/spec/award_spec.rb +4 -0
  16. data/spec/base_spec.rb +23 -49
  17. data/spec/client_spec.rb +97 -27
  18. data/spec/collection_spec.rb +6 -4
  19. data/spec/faraday/api_key_spec.rb +42 -0
  20. data/spec/faraday/errors_spec.rb +73 -0
  21. data/spec/fixtures/vcr/Reviewed_Article/associations/attachments/does_not_have_any_matching_attachments.yml +601 -0
  22. data/spec/fixtures/vcr/Reviewed_Article/associations/attachments/finds_attachments_by_tag.yml +601 -0
  23. data/spec/fixtures/vcr/Reviewed_Article/associations/attachments/has_many_attachments.yml +601 -0
  24. data/spec/fixtures/vcr/Reviewed_Article/associations/attachments/returns_all_attachments.yml +601 -0
  25. data/spec/fixtures/vcr/Reviewed_Article/associations/attachments/returns_attachments_of_the_correct_class.yml +601 -0
  26. data/spec/fixtures/vcr/Reviewed_Article/associations/pages/has_many_pages.yml +601 -0
  27. data/spec/fixtures/vcr/Reviewed_Article/associations/products/has_many_products.yml +582 -0
  28. data/spec/fixtures/vcr/Reviewed_Article/associations/products/returns_products_of_the_correct_class.yml +601 -0
  29. data/spec/fixtures/vcr/Reviewed_Article/find_page/finds_a_page_with_a_matching_slug.yml +784 -0
  30. data/spec/fixtures/vcr/Reviewed_Article/primary_product/returns_a_product_of_the_correct_class.yml +1199 -0
  31. data/spec/fixtures/vcr/Reviewed_Article/primary_product/returns_nil_if_does_not_respond_to_products.yml +1199 -0
  32. data/spec/fixtures/vcr/Reviewed_Article/primary_product/returns_the_primary_product.yml +1199 -0
  33. data/spec/fixtures/vcr/Reviewed_Client/_perform/request_params/not_set/has_nil_query_params.yml +254 -0
  34. data/spec/fixtures/vcr/Reviewed_Client/_perform/request_params/set/merges_quest_params.yml +98 -0
  35. data/spec/fixtures/vcr/Reviewed_Collection/collection_data/fetches_the_first_page_by_default.yml +183 -0
  36. data/spec/fixtures/vcr/Reviewed_Collection/collection_data/is_enumerable.yml +183 -0
  37. data/spec/fixtures/vcr/Reviewed_Collection/next_page/fetches_the_next_page_of_results.yml +310 -0
  38. data/spec/fixtures/vcr/Reviewed_Collection/page_attributes_pagination_/indicates_if_the_page_number_is_out_of_bounds.yml +183 -0
  39. data/spec/fixtures/vcr/Reviewed_Collection/page_attributes_pagination_/indicates_whether_this_is_the_first_or_last_page.yml +183 -0
  40. data/spec/fixtures/vcr/Reviewed_Collection/page_attributes_pagination_/returns_the_limit_value_max_per_page_.yml +183 -0
  41. data/spec/fixtures/vcr/Reviewed_Collection/page_attributes_pagination_/returns_the_number_of_entries_on_the_current_page.yml +183 -0
  42. data/spec/fixtures/vcr/Reviewed_Collection/page_attributes_pagination_/returns_the_offset.yml +183 -0
  43. data/spec/fixtures/vcr/Reviewed_Collection/page_attributes_pagination_/returns_the_total_item_count.yml +183 -0
  44. data/spec/fixtures/vcr/Reviewed_Collection/page_attributes_pagination_/returns_the_total_number_of_pages.yml +183 -0
  45. data/spec/fixtures/vcr/Reviewed_Collection/previous_page/fetches_the_previous_page_of_results.yml +490 -0
  46. data/spec/fixtures/vcr/Reviewed_Collection/previous_page/returns_nil_if_there_is_no_previous_page.yml +183 -0
  47. data/spec/fixtures/vcr/Reviewed_Product/associations/attachments/does_not_have_any_matching_attachments.yml +116 -0
  48. data/spec/fixtures/vcr/Reviewed_Product/associations/attachments/finds_attachments_by_tag.yml +116 -0
  49. data/spec/fixtures/vcr/Reviewed_Product/associations/attachments/has_many_attachments.yml +119 -0
  50. data/spec/fixtures/vcr/Reviewed_Product/associations/attachments/returns_all_attachments.yml +116 -0
  51. data/spec/fixtures/vcr/Reviewed_Product/associations/attachments/returns_attachments_of_the_correct_class.yml +119 -0
  52. data/spec/fixtures/vcr/Reviewed_Product/manufacturer_specs/has_many_manufacturer_specs.yml +116 -0
  53. data/spec/fixtures/vcr/Reviewed_Product/manufacturer_specs/returns_attachments_of_the_correct_class.yml +116 -0
  54. data/spec/fixtures/vcr/Reviewed_Request/_where/filters_collections_using_other_supported_options.yml +110 -0
  55. data/spec/fixtures/vcr/Reviewed_Request/_where/returns_a_collection.yml +254 -0
  56. data/spec/fixtures/vcr/Reviewed_Request/_where/returns_an_empty_set_if_no_matching_data_was_found.yml +86 -0
  57. data/spec/fixtures/vcr/Reviewed_Request/_where/returns_the_appropriate_page_of_results.yml +288 -0
  58. data/spec/fixtures/vcr/Reviewed_Request/collection_from_response/returns_a_collection_object.yml +254 -0
  59. data/spec/fixtures/vcr/Reviewed_Request/collection_from_response/returns_objects_of_the_correct_class.yml +254 -0
  60. data/spec/fixtures/vcr/Reviewed_Request/object_from_response/returns_an_object_of_the_correct_class.yml +601 -0
  61. data/spec/product_spec.rb +8 -6
  62. data/spec/request_spec.rb +117 -0
  63. data/spec/reviewed_spec.rb +5 -31
  64. data/spec/spec_helper.rb +6 -5
  65. data/spec/website_spec.rb +1 -2
  66. metadata +111 -40
  67. data/lib/faraday/global_params.rb +0 -21
  68. data/lib/reviewed/configurable.rb +0 -26
  69. data/lib/reviewed/utils.rb +0 -21
  70. data/spec/configurable_spec.rb +0 -44
  71. data/spec/faraday/global_params_spec.rb +0 -33
  72. data/spec/fixtures/vcr/article/attachments.yml +0 -1065
  73. data/spec/fixtures/vcr/article/find_page.yml +0 -383
  74. data/spec/fixtures/vcr/article/products.yml +0 -1065
  75. data/spec/fixtures/vcr/base/article/find.yml +0 -14825
  76. data/spec/fixtures/vcr/base/where_collection.yml +0 -1203
  77. data/spec/fixtures/vcr/collection/products.yml +0 -922
  78. data/spec/fixtures/vcr/faraday/global_params/request.yml +0 -109
  79. data/spec/fixtures/vcr/faraday/global_params/request_no_params.yml +0 -885
  80. data/spec/fixtures/vcr/faraday/global_params/request_params.yml +0 -405
  81. data/spec/fixtures/vcr/product/attachments.yml +0 -221
  82. data/spec/fixtures/vcr/product/manufacturer_specs.yml +0 -194
  83. data/spec/fixtures/vcr/utils/collection.yml +0 -644
  84. data/spec/fixtures/vcr/utils/object.yml +0 -7658
  85. data/spec/fixtures/vcr/utils.yml +0 -7961
  86. data/spec/utils_spec.rb +0 -33
data/spec/client_spec.rb CHANGED
@@ -2,11 +2,13 @@ require 'spec_helper.rb'
2
2
 
3
3
  describe Reviewed::Client do
4
4
 
5
- let(:client) { Reviewed::Client.new }
5
+ let(:client) do
6
+ Reviewed::Client.new(api_key: TEST_KEY, base_uri: TEST_URL)
7
+ end
6
8
 
7
- describe 'variables' do
9
+ describe 'accessors' do
8
10
 
9
- [:api_key, :base_uri, :api_version].each do |var|
11
+ [:api_key, :base_uri, :request_params].each do |var|
10
12
  describe "#{var}" do
11
13
 
12
14
  it 'exists' do
@@ -16,54 +18,86 @@ describe Reviewed::Client do
16
18
  end
17
19
  end
18
20
 
19
- describe '#delete' do
21
+ describe '#configure' do
20
22
 
21
- it 'delegates to request' do
22
- client.should_receive(:request).with(:delete, "path", kind_of(Hash))
23
- client.delete("path", {})
23
+ it 'returns self' do
24
+ client.configure{}.should be_an_instance_of(Reviewed::Client)
25
+ end
26
+
27
+ it 'yields self' do
28
+ client.configure do |config|
29
+ config.api_key = 'test_key'
30
+ end
31
+ client.api_key.should eql('test_key')
24
32
  end
25
33
  end
26
34
 
27
- describe '#get' do
35
+ describe '#resource' do
28
36
 
29
- it 'delegates to request' do
30
- client.should_receive(:request).with(:get, "path", kind_of(Hash))
31
- client.get("path", {})
37
+ context 'constant exists' do
38
+
39
+ it 'returns the appropriate constant' do
40
+ client.resource("articles").should eql(Reviewed::Article)
41
+ end
42
+ end
43
+
44
+ context 'constant does not exist' do
45
+
46
+ it 'returns the string' do
47
+ client.resource("foobar").should eql("foobar")
48
+ end
32
49
  end
33
50
  end
34
51
 
35
- describe '#post' do
52
+ describe '#method_missing' do
36
53
 
37
- it 'delegates to request' do
38
- client.should_receive(:request).with(:post, "path", kind_of(Hash))
39
- client.post("path", {})
54
+ before(:each) do
55
+ @request = client.articles
56
+ end
57
+
58
+ it 'returns a Reviewed::Request instance' do
59
+ @request.should be_an_instance_of(Reviewed::Request)
60
+ end
61
+
62
+ it 'sets the correct instance variables' do
63
+ @request.resource.should eql(Reviewed::Article)
64
+ @request.client.should eql(client)
65
+ end
66
+ end
67
+
68
+ describe '#get' do
69
+
70
+ it 'delegates to perform' do
71
+ client.should_receive(:perform).with(:get, "path", kind_of(Hash))
72
+ client.get("path", {})
40
73
  end
41
74
  end
42
75
 
43
76
  describe '#put' do
44
77
 
45
- it 'delegates to request' do
46
- client.should_receive(:request).with(:put, "path", kind_of(Hash))
78
+ it 'delegates to client' do
79
+ client.should_receive(:perform).with(:put, "path", kind_of(Hash))
47
80
  client.put("path", {})
48
81
  end
49
82
  end
50
83
 
51
- describe '#url' do
84
+ describe '#post' do
52
85
 
53
- it 'returns a url' do
54
- client.url.should eql('http://localhost:3000/api/v1')
86
+ it 'delegates to client' do
87
+ client.should_receive(:perform).with(:post, "path", kind_of(Hash))
88
+ client.post("path", {})
55
89
  end
56
90
  end
57
91
 
58
- describe '#request' do
92
+ describe '#delete' do
59
93
 
60
- it 'verifies the key' do
61
- client.stub_chain(:connection, :send).and_return(nil)
62
- client.should_receive(:verify_key!)
63
- client.send(:request, :get, 'test')
94
+ it 'delegates to request' do
95
+ client.should_receive(:perform).with(:delete, "path", kind_of(Hash))
96
+ client.delete("path", {})
64
97
  end
65
98
  end
66
99
 
100
+
67
101
  describe '#connnection' do
68
102
 
69
103
  let(:conn) { client.send(:connection) }
@@ -76,7 +110,7 @@ describe Reviewed::Client do
76
110
  conn.builder.handlers.should include(Faraday::Request::UrlEncoded)
77
111
  end
78
112
 
79
- it 'uses a Json middleware' do
113
+ it 'uses a JSON middleware' do
80
114
  conn.builder.handlers.should include(FaradayMiddleware::ParseJson)
81
115
  end
82
116
 
@@ -89,7 +123,43 @@ describe Reviewed::Client do
89
123
  end
90
124
 
91
125
  it 'sets the url' do
92
- conn.url_prefix.to_s.should eql('http://localhost:3000/api/v1')
126
+ conn.url_prefix.to_s.should match('https://')
127
+ end
128
+ end
129
+
130
+ describe '#perform' do
131
+
132
+ describe 'request_params', vcr: true do
133
+
134
+ context 'set' do
135
+
136
+ it 'merges quest params' do
137
+ client.request_params = { per_page: 1 }
138
+ collection = client.articles.where({})
139
+ collection.count.should eql(1)
140
+ end
141
+ end
142
+
143
+ context 'not set' do
144
+
145
+ it 'has nil query params' do
146
+ collection = client.articles.where({})
147
+ collection.count.should eql(20)
148
+ end
149
+ end
150
+
151
+
93
152
  end
94
153
  end
154
+
155
+ context 'with error' do
156
+
157
+ context 'bad response' do
158
+ it 'should raise a Reviewed::ApiError' do
159
+ client.connection.stub(:send).and_raise Faraday::Error::ClientError.new(true)
160
+ lambda { client.get('something that times out') }.should raise_error(Reviewed::ApiError)
161
+ end
162
+ end
163
+
164
+ end
95
165
  end
@@ -5,12 +5,14 @@ module Reviewed
5
5
  end
6
6
  end
7
7
 
8
- describe Reviewed::Collection do
9
- use_vcr_cassette 'collection/products'
8
+ describe Reviewed::Collection, vcr: true do
9
+
10
+ let(:client) do
11
+ Reviewed::Client.new(api_key: TEST_KEY, base_uri: TEST_URL)
12
+ end
10
13
 
11
14
  before(:each) do
12
- Reviewed.api_key = TEST_KEY
13
- @collection = Reviewed::Product.all # creates a collection
15
+ @collection = client.products.all # creates a collection
14
16
  end
15
17
 
16
18
  describe 'collection data' do
@@ -0,0 +1,42 @@
1
+ require 'spec_helper.rb'
2
+
3
+ describe Faraday::ApiKey do
4
+
5
+ describe 'error' do
6
+
7
+ stubs = Faraday::Adapter::Test::Stubs.new do |stub|
8
+ stub.get('/products/123') {[ 200, {foo: 'hi'}, '']}
9
+ end
10
+
11
+ test = Faraday.new do |builder|
12
+ builder.request :api_key
13
+ builder.adapter :test, stubs
14
+ end
15
+
16
+ it 'should raise a configuration error' do
17
+ expect {
18
+ test.get('/products/123')
19
+ }.to raise_error(Reviewed::ConfigurationError)
20
+ end
21
+ end
22
+
23
+ describe 'no error' do
24
+
25
+ stubs = Faraday::Adapter::Test::Stubs.new do |stub|
26
+ stub.get('/products/123') {[ 200, {foo: 'hi'}, '']}
27
+ end
28
+
29
+ test = Faraday.new do |builder|
30
+ builder.request :api_key
31
+ builder.adapter :test, stubs
32
+ end
33
+
34
+ test.headers = { "X-Reviewed-Authorization" => '123' }
35
+
36
+ it 'should not raise an error' do
37
+ expect {
38
+ test.get('/products/123')
39
+ }.to_not raise_error
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,73 @@
1
+ require 'spec_helper.rb'
2
+
3
+ describe Faraday::Errors do
4
+
5
+ describe 'error' do
6
+ stubs = Faraday::Adapter::Test::Stubs.new do |stub|
7
+ stub.get('/products/123') {[ 404, {}, { message: 'Record Not Found' } ]}
8
+ end
9
+
10
+ stubs2 = Faraday::Adapter::Test::Stubs.new do |stub|
11
+ stub.get('/products/123') {[ 500, {}, '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1"> <style type="text/css"> html, body, iframe { margin: 0; padding: 0; height: 100%; } iframe { display: block; width: 100%; border: none; } </style> <title>Application Error</title></head> </head> <body> <iframe src="//s3.amazonaws.com/heroku_pages/error.html"> <p>Application Error</p> </iframe> </body> </html>' ]}
12
+ end
13
+
14
+ test = Faraday.new do |builder|
15
+ builder.response :errors
16
+ builder.response :json
17
+ builder.adapter :test, stubs
18
+ end
19
+
20
+ test2 = Faraday.new do |builder|
21
+ builder.response :errors
22
+ builder.response :json
23
+ builder.adapter :test, stubs2
24
+ end
25
+
26
+ it 'should raise a ResourceNotFound error' do
27
+ expect {
28
+ test.get('/products/123')
29
+ }.to raise_error(Reviewed::ResourceNotFound) { |e|
30
+ e.url.should be_an_instance_of(URI::HTTP)
31
+ e.url.to_s.should eql('http:/products/123')
32
+ e.message.should eql('Not Found')
33
+ }
34
+ end
35
+
36
+ context 'faraday catch-all' do
37
+
38
+ it 'raises an ApiError error' do
39
+ client = Reviewed::Client.new
40
+ client.stub!(:connection).and_return(test2)
41
+ expect {
42
+ client.send(:perform, :get, '/products/123')
43
+ }.to raise_error(Reviewed::ApiError)
44
+ end
45
+
46
+ it 'passes other Reviewed errors through' do
47
+ client = Reviewed::Client.new
48
+ client.stub!(:connection).and_raise(Reviewed::ConfigurationError.new)
49
+ expect {
50
+ client.send(:perform, :get, '/products/123')
51
+ }.to raise_error(Reviewed::ConfigurationError)
52
+ end
53
+ end
54
+ end
55
+
56
+ describe 'no error' do
57
+ stubs = Faraday::Adapter::Test::Stubs.new do |stub|
58
+ stub.get('/products/123') {[ 200, {}, { message: 'Record Not Found' } ]}
59
+ end
60
+
61
+ test = Faraday.new do |builder|
62
+ builder.response :errors
63
+ builder.response :json
64
+ builder.adapter :test, stubs
65
+ end
66
+
67
+ it 'should not raise an error' do
68
+ expect {
69
+ test.get('/products/123')
70
+ }.to_not raise_error
71
+ end
72
+ end
73
+ end