reviewed 0.1.4 → 0.1.21

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 (75) hide show
  1. data/lib/faraday/api_key.rb +2 -4
  2. data/lib/reviewed/article.rb +1 -3
  3. data/lib/reviewed/base.rb +2 -19
  4. data/lib/reviewed/client.rb +7 -44
  5. data/lib/reviewed/product.rb +0 -2
  6. data/lib/reviewed/request.rb +35 -7
  7. data/lib/reviewed/version.rb +1 -1
  8. data/lib/reviewed.rb +2 -14
  9. data/reviewed.gemspec +0 -1
  10. data/spec/article_spec.rb +13 -12
  11. data/spec/base_spec.rb +2 -28
  12. data/spec/client_spec.rb +5 -72
  13. data/spec/collection_spec.rb +2 -1
  14. data/spec/faraday/api_key_spec.rb +21 -26
  15. data/spec/fixtures/vcr/article/find_page.yml +179 -0
  16. data/spec/fixtures/vcr/article/grill.yml +500 -0
  17. data/spec/fixtures/vcr/article/products.yml +500 -0
  18. data/spec/fixtures/vcr/collection/products.yml +525 -0
  19. data/spec/fixtures/vcr/product/attachments.yml +98 -0
  20. data/spec/fixtures/vcr/product/manufacturer_specs.yml +98 -0
  21. data/spec/fixtures/vcr/request/collection.yml +290 -0
  22. data/spec/fixtures/vcr/request/object.yml +7135 -0
  23. data/spec/fixtures/vcr/request/perform/no_params.yml +290 -0
  24. data/spec/fixtures/vcr/request/perform/params.yml +112 -0
  25. data/spec/fixtures/vcr/request/where/collection.yml +754 -0
  26. data/spec/product_spec.rb +4 -2
  27. data/spec/request_spec.rb +64 -9
  28. data/spec/reviewed_spec.rb +0 -9
  29. data/spec/spec_helper.rb +5 -6
  30. data/spec/website_spec.rb +2 -1
  31. metadata +25 -105
  32. data/lib/faraday/errors.rb +0 -14
  33. data/lib/reviewed/award.rb +0 -4
  34. data/spec/award_spec.rb +0 -4
  35. data/spec/faraday/errors_spec.rb +0 -73
  36. data/spec/fixtures/vcr/Reviewed_Article/associations/attachments/does_not_have_any_matching_attachments.yml +0 -601
  37. data/spec/fixtures/vcr/Reviewed_Article/associations/attachments/finds_attachments_by_tag.yml +0 -601
  38. data/spec/fixtures/vcr/Reviewed_Article/associations/attachments/has_many_attachments.yml +0 -601
  39. data/spec/fixtures/vcr/Reviewed_Article/associations/attachments/returns_all_attachments.yml +0 -601
  40. data/spec/fixtures/vcr/Reviewed_Article/associations/attachments/returns_attachments_of_the_correct_class.yml +0 -601
  41. data/spec/fixtures/vcr/Reviewed_Article/associations/pages/has_many_pages.yml +0 -601
  42. data/spec/fixtures/vcr/Reviewed_Article/associations/products/has_many_products.yml +0 -582
  43. data/spec/fixtures/vcr/Reviewed_Article/associations/products/returns_products_of_the_correct_class.yml +0 -601
  44. data/spec/fixtures/vcr/Reviewed_Article/find_page/finds_a_page_with_a_matching_slug.yml +0 -784
  45. data/spec/fixtures/vcr/Reviewed_Article/primary_product/returns_a_product_of_the_correct_class.yml +0 -1199
  46. data/spec/fixtures/vcr/Reviewed_Article/primary_product/returns_nil_if_does_not_respond_to_products.yml +0 -1199
  47. data/spec/fixtures/vcr/Reviewed_Article/primary_product/returns_the_primary_product.yml +0 -1199
  48. data/spec/fixtures/vcr/Reviewed_Client/_perform/request_params/not_set/has_nil_query_params.yml +0 -254
  49. data/spec/fixtures/vcr/Reviewed_Client/_perform/request_params/set/merges_quest_params.yml +0 -98
  50. data/spec/fixtures/vcr/Reviewed_Collection/collection_data/fetches_the_first_page_by_default.yml +0 -183
  51. data/spec/fixtures/vcr/Reviewed_Collection/collection_data/is_enumerable.yml +0 -183
  52. data/spec/fixtures/vcr/Reviewed_Collection/next_page/fetches_the_next_page_of_results.yml +0 -310
  53. data/spec/fixtures/vcr/Reviewed_Collection/page_attributes_pagination_/indicates_if_the_page_number_is_out_of_bounds.yml +0 -183
  54. data/spec/fixtures/vcr/Reviewed_Collection/page_attributes_pagination_/indicates_whether_this_is_the_first_or_last_page.yml +0 -183
  55. data/spec/fixtures/vcr/Reviewed_Collection/page_attributes_pagination_/returns_the_limit_value_max_per_page_.yml +0 -183
  56. data/spec/fixtures/vcr/Reviewed_Collection/page_attributes_pagination_/returns_the_number_of_entries_on_the_current_page.yml +0 -183
  57. data/spec/fixtures/vcr/Reviewed_Collection/page_attributes_pagination_/returns_the_offset.yml +0 -183
  58. data/spec/fixtures/vcr/Reviewed_Collection/page_attributes_pagination_/returns_the_total_item_count.yml +0 -183
  59. data/spec/fixtures/vcr/Reviewed_Collection/page_attributes_pagination_/returns_the_total_number_of_pages.yml +0 -183
  60. data/spec/fixtures/vcr/Reviewed_Collection/previous_page/fetches_the_previous_page_of_results.yml +0 -490
  61. data/spec/fixtures/vcr/Reviewed_Collection/previous_page/returns_nil_if_there_is_no_previous_page.yml +0 -183
  62. data/spec/fixtures/vcr/Reviewed_Product/associations/attachments/does_not_have_any_matching_attachments.yml +0 -116
  63. data/spec/fixtures/vcr/Reviewed_Product/associations/attachments/finds_attachments_by_tag.yml +0 -116
  64. data/spec/fixtures/vcr/Reviewed_Product/associations/attachments/has_many_attachments.yml +0 -119
  65. data/spec/fixtures/vcr/Reviewed_Product/associations/attachments/returns_all_attachments.yml +0 -116
  66. data/spec/fixtures/vcr/Reviewed_Product/associations/attachments/returns_attachments_of_the_correct_class.yml +0 -119
  67. data/spec/fixtures/vcr/Reviewed_Product/manufacturer_specs/has_many_manufacturer_specs.yml +0 -116
  68. data/spec/fixtures/vcr/Reviewed_Product/manufacturer_specs/returns_attachments_of_the_correct_class.yml +0 -116
  69. data/spec/fixtures/vcr/Reviewed_Request/_where/filters_collections_using_other_supported_options.yml +0 -110
  70. data/spec/fixtures/vcr/Reviewed_Request/_where/returns_a_collection.yml +0 -254
  71. data/spec/fixtures/vcr/Reviewed_Request/_where/returns_an_empty_set_if_no_matching_data_was_found.yml +0 -86
  72. data/spec/fixtures/vcr/Reviewed_Request/_where/returns_the_appropriate_page_of_results.yml +0 -288
  73. data/spec/fixtures/vcr/Reviewed_Request/collection_from_response/returns_a_collection_object.yml +0 -254
  74. data/spec/fixtures/vcr/Reviewed_Request/collection_from_response/returns_objects_of_the_correct_class.yml +0 -254
  75. data/spec/fixtures/vcr/Reviewed_Request/object_from_response/returns_an_object_of_the_correct_class.yml +0 -601
@@ -2,11 +2,9 @@ module Faraday
2
2
  class ApiKey < Faraday::Middleware
3
3
 
4
4
  def call(env)
5
- unless env[:request_headers]['X-Reviewed-Authorization']
6
- raise ::Reviewed::ConfigurationError.new(msg: "Please set the API key for your Reviewed::Client instance before making a request")
5
+ unless env.request.headers['X-Reviewed-Authorization']
6
+ raise ::Reviewed::ConfigurationError.new("Please set the API key for your Reviewed::Client instance before making a request")
7
7
  end
8
-
9
- @app.call(env)
10
8
  end
11
9
  end
12
10
  end
@@ -13,9 +13,7 @@ module Reviewed
13
13
  end
14
14
 
15
15
  def primary_product
16
- if respond_to?(:products)
17
- products.select { |p| p.id == primary_product_id }.first
18
- end
16
+ products.select { |p| p.id == primary_product_id }.first
19
17
  end
20
18
 
21
19
  def attachments(tag=nil)
data/lib/reviewed/base.rb CHANGED
@@ -12,22 +12,6 @@ module Reviewed
12
12
  self.attributes = objectify(data)
13
13
  end
14
14
 
15
- def created_at
16
- if @attributes.has_key?(:created_at)
17
- Time.parse(@attributes[:created_at])
18
- else
19
- nil
20
- end
21
- end
22
-
23
- def updated_at
24
- if @attributes.has_key?(:updated_at)
25
- Time.parse(@attributes[:updated_at])
26
- else
27
- nil
28
- end
29
- end
30
-
31
15
  class << self
32
16
 
33
17
  def path
@@ -39,9 +23,8 @@ module Reviewed
39
23
  end
40
24
  end
41
25
 
42
- def respond_to?(sym, include_private=false)
43
- return true if super
44
- @attributes.has_key?(sym)
26
+ def initialize(data)
27
+ self.attributes = objectify(data)
45
28
  end
46
29
 
47
30
  def method_missing(sym, *args, &block)
@@ -15,29 +15,14 @@ module Reviewed
15
15
  self
16
16
  end
17
17
 
18
- # Perform an HTTP GET request
19
- def get(path, params={})
20
- perform(:get, path, params)
21
- end
22
-
23
- # Perform an HTTP PUT request
24
- def put(path, params={})
25
- perform(:put, path, params)
26
- end
27
-
28
- # Perform an HTTP DELETE request
29
- def post(path, params={})
30
- perform(:post, path, params)
31
- end
32
-
33
- # Perform an HTTP DELETE request
34
- def delete(path, params={})
35
- perform(:delete, path, params)
36
- end
37
-
38
18
  def resource(name)
39
19
  klass_string = "Reviewed::#{name.to_s.singularize.classify}"
40
- klass_string.constantize rescue name
20
+
21
+ begin
22
+ klass = klass_string.constantize
23
+ rescue
24
+ raise "Resource: #{klass_string} not found"
25
+ end
41
26
  end
42
27
 
43
28
  def method_missing(method, *args, &block)
@@ -45,34 +30,12 @@ module Reviewed
45
30
  end
46
31
 
47
32
  def connection
48
- @connection ||= ::Faraday.new(url: base_uri) do |faraday|
33
+ @connection ||= ::Faraday.new(url: BASE_URI) do |faraday|
49
34
  faraday.response :mashify
50
- faraday.response :errors
51
35
  faraday.response :json
52
- faraday.request :api_key
53
36
  faraday.request :url_encoded
54
37
  faraday.adapter Faraday.default_adapter
55
38
  end
56
39
  end
57
-
58
- private
59
-
60
- def perform(method, path, params={})
61
- begin
62
- self.connection.send(method.to_sym, path, params) do |request|
63
- request.params.merge!(self.request_params)
64
- request.headers['X-Reviewed-Authorization'] ||= self.api_key
65
- end
66
- rescue Faraday::Error::ClientError => e
67
- message = <<-EOS.gsub(/^[ ]*/, '')
68
- API Error. method: #{method}. path: #{path}. params: #{params.to_s}. api_key: #{self.api_key}
69
- Original exception message:
70
- #{e.message}
71
- EOS
72
- new_exception = Reviewed::ApiError.new(msg: message)
73
- new_exception.set_backtrace(e.backtrace) # TODO not seeing in Airbrake
74
- raise new_exception
75
- end
76
- end
77
40
  end
78
41
  end
@@ -4,8 +4,6 @@ module Reviewed
4
4
  class Product < Base
5
5
  has_many :attachments
6
6
  has_many :manufacturer_specs
7
- has_many :awards
8
- has_one :brand
9
7
 
10
8
  def attachments(tag=nil)
11
9
  if tag.present?
@@ -4,20 +4,39 @@ module Reviewed
4
4
  attr_reader :path
5
5
 
6
6
  def initialize(opts={})
7
- if opts[:resource].kind_of?(Class)
7
+ if opts[:resource].kind_of?(String)
8
+ @path = opts[:resource]
9
+ else
8
10
  @resource = opts[:resource]
9
11
  @path = @resource.path
10
- else
11
- @path = opts[:resource].to_s
12
12
  end
13
13
 
14
14
  @client = opts[:client] || Reviewed::Client.new
15
15
  end
16
16
 
17
+ # Perform an HTTP GET request
18
+ def get(path, params={})
19
+ perform(:get, path, params)
20
+ end
21
+
22
+ # Perform an HTTP PUT request
23
+ def put(path, params={})
24
+ perform(:put, path, params)
25
+ end
26
+
27
+ # Perform an HTTP DELETE request
28
+ def post(path, params={})
29
+ perform(:post, path, params)
30
+ end
31
+
32
+ # Perform an HTTP DELETE request
33
+ def delete(path, params={})
34
+ perform(:delete, path, params)
35
+ end
36
+
17
37
  # Get request on resource#show
18
38
  def find(id, params={})
19
- url_path = [path, CGI::escape(id.to_s)]
20
- object_from_response(:get, url_path.join('/'), params)
39
+ object_from_response(:get, "#{path}/#{id}", params)
21
40
  end
22
41
 
23
42
  # Get request on resource#index with query params
@@ -31,13 +50,22 @@ module Reviewed
31
50
  end
32
51
 
33
52
  def object_from_response(method, url, params={})
34
- response = client.send(method, url, params)
53
+ response = self.send(method, url, params)
35
54
  resource.new(response.body)
36
55
  end
37
56
 
38
57
  def collection_from_response(method, url, params={})
39
- response = client.send(method, url, params)
58
+ response = self.send(method, url, params)
40
59
  Reviewed::Collection.new(client, resource, response, params)
41
60
  end
61
+
62
+ private
63
+
64
+ def perform(method, path, params={})
65
+ client.connection.send(method.to_sym, path, params) do |request|
66
+ request.params.merge!(client.request_params)
67
+ request.headers['X-Reviewed-Authorization'] ||= client.api_key
68
+ end
69
+ end
42
70
  end
43
71
  end
@@ -1,4 +1,4 @@
1
1
  module Reviewed
2
- VERSION = "0.1.4"
2
+ VERSION = "0.1.21"
3
3
  API_VERSION = 'v1'
4
4
  end
data/lib/reviewed.rb CHANGED
@@ -3,7 +3,6 @@ $LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__)))
3
3
  require 'faraday'
4
4
  require 'faraday_middleware'
5
5
  require 'faraday/api_key'
6
- require 'faraday/errors'
7
6
  require 'active_support/inflector'
8
7
  require 'active_support/core_ext'
9
8
  require 'active_model'
@@ -17,26 +16,15 @@ require 'reviewed/collection'
17
16
  require 'reviewed/base'
18
17
 
19
18
  require 'reviewed/manufacturer_spec'
20
- require 'reviewed/award'
21
- require 'reviewed/brand'
22
19
  require 'reviewed/product'
23
20
  require 'reviewed/article'
24
21
  require 'reviewed/author'
22
+ require 'reviewed/brand'
25
23
  require 'reviewed/page'
26
24
  require 'reviewed/website'
27
25
 
28
26
 
29
27
  module Reviewed
30
- class BaseError < StandardError
31
- attr_accessor :url
32
-
33
- def initialize(opts={})
34
- @url = opts[:url]
35
- super(opts[:msg])
36
- end
37
- end
38
- class ConfigurationError < Reviewed::BaseError; end
39
- class ResourceNotFound < Reviewed::BaseError; end
40
- class ApiError < Reviewed::BaseError; end
28
+ class ConfigurationError < StandardError; end
41
29
  end
42
30
 
data/reviewed.gemspec CHANGED
@@ -22,7 +22,6 @@ Gem::Specification.new do |gem|
22
22
  gem.add_dependency 'hashie', '~> 1.2'
23
23
  gem.add_dependency 'rack'
24
24
 
25
- gem.add_development_dependency 'rake'
26
25
  gem.add_development_dependency 'rspec', '>= 2.10'
27
26
  gem.add_development_dependency 'webmock', '>= 1.8'
28
27
  gem.add_development_dependency 'vcr', '>= 2.1'
data/spec/article_spec.rb CHANGED
@@ -1,18 +1,20 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe Reviewed::Article, vcr: true do
3
+ describe Reviewed::Article do
4
+ use_vcr_cassette 'article/grill'
4
5
 
5
6
  let(:client) do
6
7
  Reviewed::Client.new(api_key: TEST_KEY, base_uri: TEST_URL)
7
8
  end
8
9
 
9
10
  before(:each) do
10
- @article = client.articles.find('big-green-egg-medium-charcoal-grill-review', { with_attachments: true })
11
+ @article = client.articles.find('big-green-egg-medium-charcoal-grill-review')
11
12
  end
12
13
 
13
- describe 'associations', vcr: true do
14
+ describe 'associations' do
14
15
 
15
16
  describe 'pages' do
17
+ use_vcr_cassette 'article/pages'
16
18
 
17
19
  it 'has_many :pages' do
18
20
  Reviewed::Article._embedded_many.should include({"pages"=>Reviewed::Page})
@@ -20,6 +22,7 @@ describe Reviewed::Article, vcr: true do
20
22
  end
21
23
 
22
24
  describe 'products' do
25
+ use_vcr_cassette 'article/products'
23
26
 
24
27
  it 'has_many :products' do
25
28
  Reviewed::Article._embedded_many.should include({"products"=>Reviewed::Product})
@@ -33,6 +36,7 @@ describe Reviewed::Article, vcr: true do
33
36
  end
34
37
 
35
38
  describe 'attachments' do
39
+ use_vcr_cassette 'article/attachments'
36
40
 
37
41
  it 'has_many :attachments' do
38
42
  Reviewed::Article._embedded_many.should include({"attachments"=>Reviewed::Attachment})
@@ -49,7 +53,7 @@ describe Reviewed::Article, vcr: true do
49
53
  end
50
54
 
51
55
  it 'finds attachments by tag' do
52
- attachments = @article.attachments('old-hero')
56
+ attachments = @article.attachments('hero')
53
57
  attachments.length.should == 1
54
58
  attachments.each do |attachment|
55
59
  attachment.tags.join(',').should match(/hero/i)
@@ -63,11 +67,12 @@ describe Reviewed::Article, vcr: true do
63
67
  end
64
68
  end
65
69
 
66
- describe 'find_page', vcr: true do
70
+ describe 'find_page' do
71
+ use_vcr_cassette 'article/find_page'
67
72
 
68
73
  it 'finds a page with a matching slug' do
69
74
  article = client.articles.find('minden-master-ii-grill-review')
70
- article.pages.length.should == 4
75
+ article.pages.length.should == 9
71
76
  page = article.find_page('performance')
72
77
  page.should == article.pages[2]
73
78
  page.name.should == 'Performance'
@@ -75,7 +80,8 @@ describe Reviewed::Article, vcr: true do
75
80
  end
76
81
 
77
82
 
78
- describe 'primary_product', vcr: true do
83
+ describe 'primary_product' do
84
+ use_vcr_cassette 'article/products'
79
85
 
80
86
  before(:each) do
81
87
  @article = client.articles.find('big-green-egg-medium-charcoal-grill-review')
@@ -89,10 +95,5 @@ describe Reviewed::Article, vcr: true do
89
95
  it "returns a product of the correct class" do
90
96
  @product.class.should == Reviewed::Product
91
97
  end
92
-
93
- it 'returns nil if does not respond to products' do
94
- @article.attributes.delete(:products)
95
- @article.primary_product.should be_nil
96
- end
97
98
  end
98
99
  end
data/spec/base_spec.rb CHANGED
@@ -41,35 +41,9 @@ describe Reviewed::Base do
41
41
  end
42
42
 
43
43
  describe 'attributes' do
44
- before(:each) do
45
- @timestamp = Time.parse("01/01/2013 10:00:00 GMT")
46
- @model = Example.new(id: 'id', super_awesome: 'true', created_at: @timestamp.to_s, updated_at: @timestamp.to_s)
47
- end
48
-
49
44
  it 'returns the named attribute (via method missing)' do
50
- @model.super_awesome.should == 'true'
51
- end
52
-
53
- it 'has a created at timestamp' do
54
- @model.created_at.should == @timestamp
55
- end
56
-
57
- it 'has an updated at timestamp' do
58
- @model.updated_at.should == @timestamp
59
- end
60
- end
61
-
62
- describe 'respond_to?' do
63
- before(:each) do
64
- @model = Example.new(id: 'id', super_awesome: 'true')
65
- end
66
-
67
- it 'takes attributes into consideration' do
68
- @model.respond_to?(:super_awesome).should be_true
69
- end
70
-
71
- it 'preserves the original behavior' do
72
- @model.respond_to?(:fafafa).should be_false
45
+ model = Example.new(:id => 'id', :super_awesome => 'true')
46
+ model.super_awesome.should == 'true'
73
47
  end
74
48
  end
75
49
  end
data/spec/client_spec.rb CHANGED
@@ -43,8 +43,10 @@ describe Reviewed::Client do
43
43
 
44
44
  context 'constant does not exist' do
45
45
 
46
- it 'returns the string' do
47
- client.resource("foobar").should eql("foobar")
46
+ it 'raises an error' do
47
+ expect {
48
+ client.resource("tester")
49
+ }.to raise_error
48
50
  end
49
51
  end
50
52
  end
@@ -65,39 +67,6 @@ describe Reviewed::Client do
65
67
  end
66
68
  end
67
69
 
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", {})
73
- end
74
- end
75
-
76
- describe '#put' do
77
-
78
- it 'delegates to client' do
79
- client.should_receive(:perform).with(:put, "path", kind_of(Hash))
80
- client.put("path", {})
81
- end
82
- end
83
-
84
- describe '#post' do
85
-
86
- it 'delegates to client' do
87
- client.should_receive(:perform).with(:post, "path", kind_of(Hash))
88
- client.post("path", {})
89
- end
90
- end
91
-
92
- describe '#delete' do
93
-
94
- it 'delegates to request' do
95
- client.should_receive(:perform).with(:delete, "path", kind_of(Hash))
96
- client.delete("path", {})
97
- end
98
- end
99
-
100
-
101
70
  describe '#connnection' do
102
71
 
103
72
  let(:conn) { client.send(:connection) }
@@ -123,43 +92,7 @@ describe Reviewed::Client do
123
92
  end
124
93
 
125
94
  it 'sets the url' do
126
- conn.url_prefix.to_s.should match('https://')
95
+ conn.url_prefix.to_s.should eql('http://localhost:3000/api/v1')
127
96
  end
128
97
  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
-
152
- end
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
165
98
  end
@@ -5,7 +5,8 @@ module Reviewed
5
5
  end
6
6
  end
7
7
 
8
- describe Reviewed::Collection, vcr: true do
8
+ describe Reviewed::Collection do
9
+ use_vcr_cassette 'collection/products'
9
10
 
10
11
  let(:client) do
11
12
  Reviewed::Client.new(api_key: TEST_KEY, base_uri: TEST_URL)
@@ -1,42 +1,37 @@
1
1
  require 'spec_helper.rb'
2
+ require 'ostruct'
2
3
 
3
4
  describe Faraday::ApiKey do
4
5
 
5
- describe 'error' do
6
-
7
- stubs = Faraday::Adapter::Test::Stubs.new do |stub|
8
- stub.get('/products/123') {[ 200, {foo: 'hi'}, '']}
6
+ class FauxApp
7
+ def call(env)
8
+ return env
9
9
  end
10
+ end
10
11
 
11
- test = Faraday.new do |builder|
12
- builder.request :api_key
13
- builder.adapter :test, stubs
14
- end
12
+ let(:faraday) { Faraday::ApiKey.new(FauxApp.new) }
13
+ let(:env) { {} }
15
14
 
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
15
+ describe 'call' do
22
16
 
23
- describe 'no error' do
17
+ context 'no auth token' do
24
18
 
25
- stubs = Faraday::Adapter::Test::Stubs.new do |stub|
26
- stub.get('/products/123') {[ 200, {foo: 'hi'}, '']}
19
+ it 'raises Reviewed::ConfigurationError' do
20
+ expect {
21
+ faraday.call(env)
22
+ }.to raise_error
23
+ end
27
24
  end
28
25
 
29
- test = Faraday.new do |builder|
30
- builder.request :api_key
31
- builder.adapter :test, stubs
32
- end
26
+ context 'auth token' do
33
27
 
34
- test.headers = { "X-Reviewed-Authorization" => '123' }
28
+ let(:env) { OpenStruct.new(request: OpenStruct.new({ headers: {'X-Reviewed-Authorization' => '123'} })) }
35
29
 
36
- it 'should not raise an error' do
37
- expect {
38
- test.get('/products/123')
39
- }.to_not raise_error
30
+ it 'does not error' do
31
+ expect {
32
+ faraday.call(env)
33
+ }.to_not raise_error
34
+ end
40
35
  end
41
36
  end
42
37
  end