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.
- data/README.md +12 -5
- data/lib/faraday/api_key.rb +14 -0
- data/lib/faraday/errors.rb +14 -0
- data/lib/reviewed/article.rb +3 -1
- data/lib/reviewed/award.rb +4 -0
- data/lib/reviewed/base.rb +23 -11
- data/lib/reviewed/client.rb +47 -26
- data/lib/reviewed/collection.rb +8 -8
- data/lib/reviewed/product.rb +2 -0
- data/lib/reviewed/request.rb +43 -0
- data/lib/reviewed/version.rb +1 -1
- data/lib/reviewed.rb +13 -20
- data/reviewed.gemspec +1 -0
- data/spec/article_spec.rb +21 -21
- data/spec/award_spec.rb +4 -0
- data/spec/base_spec.rb +23 -49
- data/spec/client_spec.rb +97 -27
- data/spec/collection_spec.rb +6 -4
- data/spec/faraday/api_key_spec.rb +42 -0
- data/spec/faraday/errors_spec.rb +73 -0
- data/spec/fixtures/vcr/Reviewed_Article/associations/attachments/does_not_have_any_matching_attachments.yml +601 -0
- data/spec/fixtures/vcr/Reviewed_Article/associations/attachments/finds_attachments_by_tag.yml +601 -0
- data/spec/fixtures/vcr/Reviewed_Article/associations/attachments/has_many_attachments.yml +601 -0
- data/spec/fixtures/vcr/Reviewed_Article/associations/attachments/returns_all_attachments.yml +601 -0
- data/spec/fixtures/vcr/Reviewed_Article/associations/attachments/returns_attachments_of_the_correct_class.yml +601 -0
- data/spec/fixtures/vcr/Reviewed_Article/associations/pages/has_many_pages.yml +601 -0
- data/spec/fixtures/vcr/Reviewed_Article/associations/products/has_many_products.yml +582 -0
- data/spec/fixtures/vcr/Reviewed_Article/associations/products/returns_products_of_the_correct_class.yml +601 -0
- data/spec/fixtures/vcr/Reviewed_Article/find_page/finds_a_page_with_a_matching_slug.yml +784 -0
- data/spec/fixtures/vcr/Reviewed_Article/primary_product/returns_a_product_of_the_correct_class.yml +1199 -0
- data/spec/fixtures/vcr/Reviewed_Article/primary_product/returns_nil_if_does_not_respond_to_products.yml +1199 -0
- data/spec/fixtures/vcr/Reviewed_Article/primary_product/returns_the_primary_product.yml +1199 -0
- data/spec/fixtures/vcr/Reviewed_Client/_perform/request_params/not_set/has_nil_query_params.yml +254 -0
- data/spec/fixtures/vcr/Reviewed_Client/_perform/request_params/set/merges_quest_params.yml +98 -0
- data/spec/fixtures/vcr/Reviewed_Collection/collection_data/fetches_the_first_page_by_default.yml +183 -0
- data/spec/fixtures/vcr/Reviewed_Collection/collection_data/is_enumerable.yml +183 -0
- data/spec/fixtures/vcr/Reviewed_Collection/next_page/fetches_the_next_page_of_results.yml +310 -0
- data/spec/fixtures/vcr/Reviewed_Collection/page_attributes_pagination_/indicates_if_the_page_number_is_out_of_bounds.yml +183 -0
- data/spec/fixtures/vcr/Reviewed_Collection/page_attributes_pagination_/indicates_whether_this_is_the_first_or_last_page.yml +183 -0
- data/spec/fixtures/vcr/Reviewed_Collection/page_attributes_pagination_/returns_the_limit_value_max_per_page_.yml +183 -0
- data/spec/fixtures/vcr/Reviewed_Collection/page_attributes_pagination_/returns_the_number_of_entries_on_the_current_page.yml +183 -0
- data/spec/fixtures/vcr/Reviewed_Collection/page_attributes_pagination_/returns_the_offset.yml +183 -0
- data/spec/fixtures/vcr/Reviewed_Collection/page_attributes_pagination_/returns_the_total_item_count.yml +183 -0
- data/spec/fixtures/vcr/Reviewed_Collection/page_attributes_pagination_/returns_the_total_number_of_pages.yml +183 -0
- data/spec/fixtures/vcr/Reviewed_Collection/previous_page/fetches_the_previous_page_of_results.yml +490 -0
- data/spec/fixtures/vcr/Reviewed_Collection/previous_page/returns_nil_if_there_is_no_previous_page.yml +183 -0
- data/spec/fixtures/vcr/Reviewed_Product/associations/attachments/does_not_have_any_matching_attachments.yml +116 -0
- data/spec/fixtures/vcr/Reviewed_Product/associations/attachments/finds_attachments_by_tag.yml +116 -0
- data/spec/fixtures/vcr/Reviewed_Product/associations/attachments/has_many_attachments.yml +119 -0
- data/spec/fixtures/vcr/Reviewed_Product/associations/attachments/returns_all_attachments.yml +116 -0
- data/spec/fixtures/vcr/Reviewed_Product/associations/attachments/returns_attachments_of_the_correct_class.yml +119 -0
- data/spec/fixtures/vcr/Reviewed_Product/manufacturer_specs/has_many_manufacturer_specs.yml +116 -0
- data/spec/fixtures/vcr/Reviewed_Product/manufacturer_specs/returns_attachments_of_the_correct_class.yml +116 -0
- data/spec/fixtures/vcr/Reviewed_Request/_where/filters_collections_using_other_supported_options.yml +110 -0
- data/spec/fixtures/vcr/Reviewed_Request/_where/returns_a_collection.yml +254 -0
- data/spec/fixtures/vcr/Reviewed_Request/_where/returns_an_empty_set_if_no_matching_data_was_found.yml +86 -0
- data/spec/fixtures/vcr/Reviewed_Request/_where/returns_the_appropriate_page_of_results.yml +288 -0
- data/spec/fixtures/vcr/Reviewed_Request/collection_from_response/returns_a_collection_object.yml +254 -0
- data/spec/fixtures/vcr/Reviewed_Request/collection_from_response/returns_objects_of_the_correct_class.yml +254 -0
- data/spec/fixtures/vcr/Reviewed_Request/object_from_response/returns_an_object_of_the_correct_class.yml +601 -0
- data/spec/product_spec.rb +8 -6
- data/spec/request_spec.rb +117 -0
- data/spec/reviewed_spec.rb +5 -31
- data/spec/spec_helper.rb +6 -5
- data/spec/website_spec.rb +1 -2
- metadata +111 -40
- data/lib/faraday/global_params.rb +0 -21
- data/lib/reviewed/configurable.rb +0 -26
- data/lib/reviewed/utils.rb +0 -21
- data/spec/configurable_spec.rb +0 -44
- data/spec/faraday/global_params_spec.rb +0 -33
- data/spec/fixtures/vcr/article/attachments.yml +0 -1065
- data/spec/fixtures/vcr/article/find_page.yml +0 -383
- data/spec/fixtures/vcr/article/products.yml +0 -1065
- data/spec/fixtures/vcr/base/article/find.yml +0 -14825
- data/spec/fixtures/vcr/base/where_collection.yml +0 -1203
- data/spec/fixtures/vcr/collection/products.yml +0 -922
- data/spec/fixtures/vcr/faraday/global_params/request.yml +0 -109
- data/spec/fixtures/vcr/faraday/global_params/request_no_params.yml +0 -885
- data/spec/fixtures/vcr/faraday/global_params/request_params.yml +0 -405
- data/spec/fixtures/vcr/product/attachments.yml +0 -221
- data/spec/fixtures/vcr/product/manufacturer_specs.yml +0 -194
- data/spec/fixtures/vcr/utils/collection.yml +0 -644
- data/spec/fixtures/vcr/utils/object.yml +0 -7658
- data/spec/fixtures/vcr/utils.yml +0 -7961
- data/spec/utils_spec.rb +0 -33
data/README.md
CHANGED
@@ -18,14 +18,21 @@ Or install it yourself as:
|
|
18
18
|
|
19
19
|
## Usage
|
20
20
|
|
21
|
-
|
21
|
+
Create a client to use in your application and configure as needed:
|
22
22
|
|
23
|
-
|
23
|
+
client = Reviewed::Client.new(
|
24
|
+
api_key: '0A0A1010202030', # required
|
25
|
+
base_uri: 'http://www.example.com/api/v1', # defaults to localhost:3000
|
26
|
+
request_params: { per_page: 1 } # query params applied to every request
|
27
|
+
)
|
24
28
|
|
25
|
-
|
29
|
+
Make a request via resources on your client:
|
26
30
|
|
27
|
-
|
28
|
-
|
31
|
+
client.articles.all
|
32
|
+
client.articles.find('123456')
|
33
|
+
|
34
|
+
client.products.all
|
35
|
+
client.products.find('123456')
|
29
36
|
|
30
37
|
## Contributing
|
31
38
|
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module Faraday
|
2
|
+
class ApiKey < Faraday::Middleware
|
3
|
+
|
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")
|
7
|
+
end
|
8
|
+
|
9
|
+
@app.call(env)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
Faraday.register_middleware :request, api_key: Faraday::ApiKey
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module Faraday
|
2
|
+
class Errors < Faraday::Middleware
|
3
|
+
|
4
|
+
def call(env)
|
5
|
+
@app.call(env).on_complete do
|
6
|
+
if env[:response].status == 404
|
7
|
+
raise Reviewed::ResourceNotFound.new(msg: 'Not Found', url: env[:url])
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
Faraday.register_middleware :response, errors: Faraday::Errors
|
data/lib/reviewed/article.rb
CHANGED
data/lib/reviewed/base.rb
CHANGED
@@ -3,7 +3,6 @@ require 'active_model'
|
|
3
3
|
module Reviewed
|
4
4
|
class Base
|
5
5
|
include ::Reviewed::Embeddable
|
6
|
-
include ::Reviewed::Utils
|
7
6
|
|
8
7
|
extend ::ActiveModel::Naming
|
9
8
|
|
@@ -13,25 +12,38 @@ module Reviewed
|
|
13
12
|
self.attributes = objectify(data)
|
14
13
|
end
|
15
14
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
15
|
+
def created_at
|
16
|
+
if @attributes.has_key?(:created_at)
|
17
|
+
Time.parse(@attributes[:created_at])
|
18
|
+
else
|
19
|
+
nil
|
20
20
|
end
|
21
|
+
end
|
21
22
|
|
22
|
-
|
23
|
-
|
23
|
+
def updated_at
|
24
|
+
if @attributes.has_key?(:updated_at)
|
25
|
+
Time.parse(@attributes[:updated_at])
|
26
|
+
else
|
27
|
+
nil
|
24
28
|
end
|
29
|
+
end
|
30
|
+
|
31
|
+
class << self
|
25
32
|
|
26
|
-
def
|
27
|
-
|
33
|
+
def path
|
34
|
+
@resource_name ||= association_name
|
28
35
|
end
|
29
36
|
|
30
|
-
def
|
31
|
-
|
37
|
+
def association_name
|
38
|
+
self.name.demodulize.downcase.pluralize
|
32
39
|
end
|
33
40
|
end
|
34
41
|
|
42
|
+
def respond_to?(sym, include_private=false)
|
43
|
+
return true if super
|
44
|
+
@attributes.has_key?(sym)
|
45
|
+
end
|
46
|
+
|
35
47
|
def method_missing(sym, *args, &block)
|
36
48
|
if @attributes.has_key?(sym)
|
37
49
|
@attributes[sym]
|
data/lib/reviewed/client.rb
CHANGED
@@ -1,57 +1,78 @@
|
|
1
1
|
module Reviewed
|
2
2
|
class Client
|
3
|
-
|
3
|
+
attr_accessor :api_key, :base_uri, :api_version, :request_params
|
4
4
|
|
5
|
-
|
5
|
+
BASE_URI = "http://localhost:3000/api/v1"
|
6
6
|
|
7
|
-
def initialize
|
8
|
-
|
9
|
-
|
10
|
-
|
7
|
+
def initialize(opts={})
|
8
|
+
@api_key = opts[:api_key] || ENV['REVIEWED_API_KEY']
|
9
|
+
@base_uri = opts[:base_uri] || BASE_URI
|
10
|
+
@request_params = opts[:request_params] || {}
|
11
11
|
end
|
12
12
|
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
def configure
|
14
|
+
yield self
|
15
|
+
self
|
16
16
|
end
|
17
17
|
|
18
18
|
# Perform an HTTP GET request
|
19
19
|
def get(path, params={})
|
20
|
-
|
21
|
-
end
|
22
|
-
|
23
|
-
# Perform an HTTP POST request
|
24
|
-
def post(path, params={})
|
25
|
-
request(:post, path, params)
|
20
|
+
perform(:get, path, params)
|
26
21
|
end
|
27
22
|
|
28
23
|
# Perform an HTTP PUT request
|
29
24
|
def put(path, params={})
|
30
|
-
|
25
|
+
perform(:put, path, params)
|
31
26
|
end
|
32
27
|
|
33
|
-
|
34
|
-
|
28
|
+
# Perform an HTTP DELETE request
|
29
|
+
def post(path, params={})
|
30
|
+
perform(:post, path, params)
|
35
31
|
end
|
36
32
|
|
37
|
-
|
33
|
+
# Perform an HTTP DELETE request
|
34
|
+
def delete(path, params={})
|
35
|
+
perform(:delete, path, params)
|
36
|
+
end
|
38
37
|
|
39
|
-
def
|
40
|
-
|
38
|
+
def resource(name)
|
39
|
+
klass_string = "Reviewed::#{name.to_s.singularize.classify}"
|
40
|
+
klass_string.constantize rescue name
|
41
|
+
end
|
41
42
|
|
42
|
-
|
43
|
-
|
44
|
-
end
|
43
|
+
def method_missing(method, *args, &block)
|
44
|
+
Reviewed::Request.new(resource: resource(method), client: self)
|
45
45
|
end
|
46
46
|
|
47
47
|
def connection
|
48
|
-
@connection ||= ::Faraday.new(url:
|
48
|
+
@connection ||= ::Faraday.new(url: base_uri) do |faraday|
|
49
49
|
faraday.response :mashify
|
50
|
+
faraday.response :errors
|
50
51
|
faraday.response :json
|
51
|
-
faraday.request :
|
52
|
+
faraday.request :api_key
|
52
53
|
faraday.request :url_encoded
|
53
54
|
faraday.adapter Faraday.default_adapter
|
54
55
|
end
|
55
56
|
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
|
56
77
|
end
|
57
78
|
end
|
data/lib/reviewed/collection.rb
CHANGED
@@ -1,22 +1,22 @@
|
|
1
1
|
module Reviewed
|
2
2
|
class Collection
|
3
|
-
include Reviewed::Utils
|
4
3
|
include Enumerable
|
5
4
|
|
6
5
|
extend Forwardable
|
7
6
|
|
8
7
|
def_delegators :@items, :<<, :[], :[]=, :each, :first, :last, :length, :concat, :map, :collect, :empty?
|
9
8
|
|
10
|
-
attr_accessor :raw_response, :page_attributes, :klass, :items, :params
|
9
|
+
attr_accessor :raw_response, :page_attributes, :klass, :items, :params, :client
|
11
10
|
|
12
|
-
def initialize(klass, response, params={})
|
11
|
+
def initialize(client, klass, response, params={})
|
13
12
|
body = response.body
|
14
13
|
data = body.data
|
15
14
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
15
|
+
@client = client
|
16
|
+
@klass = klass
|
17
|
+
@params = params
|
18
|
+
@items = []
|
19
|
+
@page_attributes = body.pagination
|
20
20
|
|
21
21
|
data.each do |obj|
|
22
22
|
self.items << klass.new(obj)
|
@@ -58,7 +58,7 @@ module Reviewed
|
|
58
58
|
|
59
59
|
def fetch_page(page=nil)
|
60
60
|
self.params[:page] = page
|
61
|
-
|
61
|
+
client.send(klass.association_name).where(self.params)
|
62
62
|
end
|
63
63
|
end
|
64
64
|
end
|
data/lib/reviewed/product.rb
CHANGED
@@ -0,0 +1,43 @@
|
|
1
|
+
module Reviewed
|
2
|
+
class Request
|
3
|
+
attr_accessor :client, :resource
|
4
|
+
attr_reader :path
|
5
|
+
|
6
|
+
def initialize(opts={})
|
7
|
+
if opts[:resource].kind_of?(Class)
|
8
|
+
@resource = opts[:resource]
|
9
|
+
@path = @resource.path
|
10
|
+
else
|
11
|
+
@path = opts[:resource].to_s
|
12
|
+
end
|
13
|
+
|
14
|
+
@client = opts[:client] || Reviewed::Client.new
|
15
|
+
end
|
16
|
+
|
17
|
+
# Get request on resource#show
|
18
|
+
def find(id, params={})
|
19
|
+
url_path = [path, CGI::escape(id.to_s)]
|
20
|
+
object_from_response(:get, url_path.join('/'), params)
|
21
|
+
end
|
22
|
+
|
23
|
+
# Get request on resource#index with query params
|
24
|
+
def where(params={})
|
25
|
+
collection_from_response(:get, path, params)
|
26
|
+
end
|
27
|
+
|
28
|
+
# Convenience Method
|
29
|
+
def all
|
30
|
+
where({})
|
31
|
+
end
|
32
|
+
|
33
|
+
def object_from_response(method, url, params={})
|
34
|
+
response = client.send(method, url, params)
|
35
|
+
resource.new(response.body)
|
36
|
+
end
|
37
|
+
|
38
|
+
def collection_from_response(method, url, params={})
|
39
|
+
response = client.send(method, url, params)
|
40
|
+
Reviewed::Collection.new(client, resource, response, params)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
data/lib/reviewed/version.rb
CHANGED
data/lib/reviewed.rb
CHANGED
@@ -2,48 +2,41 @@ $LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__)))
|
|
2
2
|
|
3
3
|
require 'faraday'
|
4
4
|
require 'faraday_middleware'
|
5
|
-
require 'faraday/
|
5
|
+
require 'faraday/api_key'
|
6
|
+
require 'faraday/errors'
|
6
7
|
require 'active_support/inflector'
|
7
8
|
require 'active_support/core_ext'
|
8
9
|
require 'active_model'
|
9
10
|
require 'hashie'
|
10
11
|
require 'rack'
|
11
12
|
|
12
|
-
require 'reviewed/configurable'
|
13
13
|
require 'reviewed/embeddable'
|
14
|
-
require 'reviewed/
|
14
|
+
require 'reviewed/request'
|
15
15
|
require 'reviewed/client'
|
16
16
|
require 'reviewed/collection'
|
17
17
|
require 'reviewed/base'
|
18
18
|
|
19
19
|
require 'reviewed/manufacturer_spec'
|
20
|
+
require 'reviewed/award'
|
21
|
+
require 'reviewed/brand'
|
20
22
|
require 'reviewed/product'
|
21
23
|
require 'reviewed/article'
|
22
24
|
require 'reviewed/author'
|
23
|
-
require 'reviewed/brand'
|
24
25
|
require 'reviewed/page'
|
25
26
|
require 'reviewed/website'
|
26
27
|
|
27
28
|
|
28
29
|
module Reviewed
|
29
|
-
class
|
30
|
-
|
31
|
-
class << self
|
32
|
-
|
33
|
-
attr_accessor :global_params
|
34
|
-
|
35
|
-
def global_params
|
36
|
-
@global_params ||= {}
|
37
|
-
end
|
38
|
-
|
39
|
-
def client
|
40
|
-
@client ||= Reviewed::Client.new
|
41
|
-
end
|
30
|
+
class BaseError < StandardError
|
31
|
+
attr_accessor :url
|
42
32
|
|
43
|
-
def
|
44
|
-
|
45
|
-
|
33
|
+
def initialize(opts={})
|
34
|
+
@url = opts[:url]
|
35
|
+
super(opts[:msg])
|
46
36
|
end
|
47
37
|
end
|
38
|
+
class ConfigurationError < Reviewed::BaseError; end
|
39
|
+
class ResourceNotFound < Reviewed::BaseError; end
|
40
|
+
class ApiError < Reviewed::BaseError; end
|
48
41
|
end
|
49
42
|
|
data/reviewed.gemspec
CHANGED
@@ -22,6 +22,7 @@ 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'
|
25
26
|
gem.add_development_dependency 'rspec', '>= 2.10'
|
26
27
|
gem.add_development_dependency 'webmock', '>= 1.8'
|
27
28
|
gem.add_development_dependency 'vcr', '>= 2.1'
|
data/spec/article_spec.rb
CHANGED
@@ -1,11 +1,18 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe Reviewed::Article do
|
3
|
+
describe Reviewed::Article, vcr: true do
|
4
4
|
|
5
|
-
|
5
|
+
let(:client) do
|
6
|
+
Reviewed::Client.new(api_key: TEST_KEY, base_uri: TEST_URL)
|
7
|
+
end
|
8
|
+
|
9
|
+
before(:each) do
|
10
|
+
@article = client.articles.find('big-green-egg-medium-charcoal-grill-review', { with_attachments: true })
|
11
|
+
end
|
12
|
+
|
13
|
+
describe 'associations', vcr: true do
|
6
14
|
|
7
15
|
describe 'pages' do
|
8
|
-
use_vcr_cassette 'article/pages'
|
9
16
|
|
10
17
|
it 'has_many :pages' do
|
11
18
|
Reviewed::Article._embedded_many.should include({"pages"=>Reviewed::Page})
|
@@ -13,11 +20,6 @@ describe Reviewed::Article do
|
|
13
20
|
end
|
14
21
|
|
15
22
|
describe 'products' do
|
16
|
-
use_vcr_cassette 'article/products'
|
17
|
-
|
18
|
-
before(:each) do
|
19
|
-
@article = Reviewed::Article.find('big-green-egg-medium-charcoal-grill-review')
|
20
|
-
end
|
21
23
|
|
22
24
|
it 'has_many :products' do
|
23
25
|
Reviewed::Article._embedded_many.should include({"products"=>Reviewed::Product})
|
@@ -31,11 +33,6 @@ describe Reviewed::Article do
|
|
31
33
|
end
|
32
34
|
|
33
35
|
describe 'attachments' do
|
34
|
-
use_vcr_cassette 'article/attachments'
|
35
|
-
|
36
|
-
before(:each) do
|
37
|
-
@article = Reviewed::Article.find('big-green-egg-medium-charcoal-grill-review')
|
38
|
-
end
|
39
36
|
|
40
37
|
it 'has_many :attachments' do
|
41
38
|
Reviewed::Article._embedded_many.should include({"attachments"=>Reviewed::Attachment})
|
@@ -52,7 +49,7 @@ describe Reviewed::Article do
|
|
52
49
|
end
|
53
50
|
|
54
51
|
it 'finds attachments by tag' do
|
55
|
-
attachments = @article.attachments('hero')
|
52
|
+
attachments = @article.attachments('old-hero')
|
56
53
|
attachments.length.should == 1
|
57
54
|
attachments.each do |attachment|
|
58
55
|
attachment.tags.join(',').should match(/hero/i)
|
@@ -66,12 +63,11 @@ describe Reviewed::Article do
|
|
66
63
|
end
|
67
64
|
end
|
68
65
|
|
69
|
-
describe 'find_page' do
|
70
|
-
use_vcr_cassette 'article/find_page'
|
66
|
+
describe 'find_page', vcr: true do
|
71
67
|
|
72
68
|
it 'finds a page with a matching slug' do
|
73
|
-
article =
|
74
|
-
article.pages.length.should ==
|
69
|
+
article = client.articles.find('minden-master-ii-grill-review')
|
70
|
+
article.pages.length.should == 4
|
75
71
|
page = article.find_page('performance')
|
76
72
|
page.should == article.pages[2]
|
77
73
|
page.name.should == 'Performance'
|
@@ -79,11 +75,10 @@ describe Reviewed::Article do
|
|
79
75
|
end
|
80
76
|
|
81
77
|
|
82
|
-
describe 'primary_product' do
|
83
|
-
use_vcr_cassette 'article/products'
|
78
|
+
describe 'primary_product', vcr: true do
|
84
79
|
|
85
80
|
before(:each) do
|
86
|
-
@article =
|
81
|
+
@article = client.articles.find('big-green-egg-medium-charcoal-grill-review')
|
87
82
|
@product = @article.primary_product
|
88
83
|
end
|
89
84
|
|
@@ -94,5 +89,10 @@ describe Reviewed::Article do
|
|
94
89
|
it "returns a product of the correct class" do
|
95
90
|
@product.class.should == Reviewed::Product
|
96
91
|
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
97
|
end
|
98
98
|
end
|
data/spec/award_spec.rb
ADDED
data/spec/base_spec.rb
CHANGED
@@ -26,76 +26,50 @@ describe Reviewed::Base do
|
|
26
26
|
end
|
27
27
|
end
|
28
28
|
|
29
|
-
describe '
|
30
|
-
use_vcr_cassette 'base/article/find'
|
29
|
+
describe 'path' do
|
31
30
|
|
32
|
-
it '
|
33
|
-
Reviewed::Article.
|
34
|
-
end
|
35
|
-
|
36
|
-
it 'calls object_from_response with correct params' do
|
37
|
-
Reviewed::Article.should_receive(:object_from_response).with(
|
38
|
-
:get, "articles/#{article_id}", {}
|
39
|
-
)
|
40
|
-
Reviewed::Article.find("#{article_id}")
|
31
|
+
it 'should the demodulized resource name' do
|
32
|
+
Reviewed::Article.path.should eql("articles")
|
41
33
|
end
|
42
34
|
end
|
43
35
|
|
44
|
-
describe '
|
36
|
+
describe 'association_name' do
|
45
37
|
|
46
|
-
it '
|
47
|
-
Reviewed::Article.
|
38
|
+
it 'returns the demodulized & pluralized version of itself' do
|
39
|
+
Reviewed::Article.association_name.should eql("articles")
|
48
40
|
end
|
49
41
|
end
|
50
42
|
|
51
|
-
describe '
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
collection = Reviewed::Article.where
|
56
|
-
collection.class.should == Reviewed::Collection
|
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)
|
57
47
|
end
|
58
48
|
|
59
|
-
it 'returns the
|
60
|
-
|
61
|
-
collection.total.should > 1
|
62
|
-
collection.current_page.should == 2
|
49
|
+
it 'returns the named attribute (via method missing)' do
|
50
|
+
@model.super_awesome.should == 'true'
|
63
51
|
end
|
64
52
|
|
65
|
-
it '
|
66
|
-
|
67
|
-
collection.total.should == 1
|
68
|
-
collection.last_page.should be_true
|
53
|
+
it 'has a created at timestamp' do
|
54
|
+
@model.created_at.should == @timestamp
|
69
55
|
end
|
70
56
|
|
71
|
-
it '
|
72
|
-
|
73
|
-
collection.should be_empty
|
74
|
-
collection.total.should == 0
|
75
|
-
collection.out_of_bounds.should be_true
|
57
|
+
it 'has an updated at timestamp' do
|
58
|
+
@model.updated_at.should == @timestamp
|
76
59
|
end
|
77
60
|
end
|
78
61
|
|
79
|
-
describe '
|
80
|
-
|
81
|
-
|
82
|
-
Reviewed::Article.should_receive(:where).with({})
|
83
|
-
Reviewed::Article.all
|
62
|
+
describe 'respond_to?' do
|
63
|
+
before(:each) do
|
64
|
+
@model = Example.new(id: 'id', super_awesome: 'true')
|
84
65
|
end
|
85
|
-
end
|
86
66
|
|
87
|
-
|
88
|
-
|
89
|
-
model = Example.new(:id => 'id', :super_awesome => 'true')
|
90
|
-
model.super_awesome.should == 'true'
|
67
|
+
it 'takes attributes into consideration' do
|
68
|
+
@model.respond_to?(:super_awesome).should be_true
|
91
69
|
end
|
92
|
-
end
|
93
|
-
|
94
|
-
describe 'resource_url' do
|
95
70
|
|
96
|
-
it '
|
97
|
-
|
98
|
-
Example.resource_url.should eql('examples')
|
71
|
+
it 'preserves the original behavior' do
|
72
|
+
@model.respond_to?(:fafafa).should be_false
|
99
73
|
end
|
100
74
|
end
|
101
75
|
end
|