reviewed 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. data/lib/reviewed/article.rb +10 -8
  2. data/lib/reviewed/attachment.rb +4 -0
  3. data/lib/reviewed/base.rb +25 -40
  4. data/lib/reviewed/client.rb +56 -0
  5. data/lib/reviewed/collection.rb +16 -19
  6. data/lib/reviewed/configurable.rb +26 -0
  7. data/lib/reviewed/embeddable.rb +74 -0
  8. data/lib/reviewed/page.rb +4 -0
  9. data/lib/reviewed/product.rb +6 -2
  10. data/lib/reviewed/utils.rb +25 -0
  11. data/lib/reviewed/version.rb +1 -1
  12. data/lib/reviewed.rb +20 -33
  13. data/reviewed.gemspec +2 -1
  14. data/spec/article_spec.rb +65 -39
  15. data/spec/base_spec.rb +39 -74
  16. data/spec/client_spec.rb +95 -0
  17. data/spec/collection_spec.rb +5 -6
  18. data/spec/configurable_spec.rb +44 -0
  19. data/spec/embeddable_spec.rb +176 -0
  20. data/spec/fixtures/vcr/article/attachments.yml +464 -457
  21. data/spec/fixtures/vcr/article/find_page.yml +115 -111
  22. data/spec/fixtures/vcr/article/products.yml +464 -457
  23. data/spec/fixtures/vcr/base/article/find.yml +7618 -0
  24. data/spec/fixtures/vcr/base/where_collection.yml +600 -7626
  25. data/spec/fixtures/vcr/collection/products.yml +501 -498
  26. data/spec/fixtures/vcr/product/attachments.yml +94 -68
  27. data/spec/fixtures/vcr/utils/collection.yml +346 -0
  28. data/spec/fixtures/vcr/utils/object.yml +7618 -0
  29. data/spec/fixtures/vcr/utils.yml +7961 -0
  30. data/spec/product_spec.rb +31 -17
  31. data/spec/reviewed_spec.rb +19 -0
  32. data/spec/utils_spec.rb +40 -0
  33. metadata +45 -22
  34. data/lib/reviewed/request.rb +0 -23
  35. data/lib/reviewed/response.rb +0 -12
  36. data/lib/reviewed/util.rb +0 -16
  37. data/spec/fixtures/vcr/base/find_error_key.yml +0 -172
  38. data/spec/fixtures/vcr/base/find_ok.yml +0 -1155
  39. data/spec/fixtures/vcr/request/authors.yml +0 -133
  40. data/spec/fixtures/vcr/response/authors.yml +0 -133
  41. data/spec/request_spec.rb +0 -15
  42. data/spec/response_spec.rb +0 -26
  43. data/spec/util_spec.rb +0 -33
data/spec/base_spec.rb CHANGED
@@ -1,87 +1,50 @@
1
1
  require 'spec_helper'
2
2
 
3
- module Reviewed
4
- class Example < Base
5
- end
6
- end
3
+ describe Reviewed::Base do
7
4
 
8
- module Reviewed
9
- class Article < Base
10
- end
11
- end
5
+ let(:article_id) { '509d166d60de7db97c05ce71' }
12
6
 
13
- describe Reviewed::Base do
14
- before(:each) do
15
- Reviewed.api_key = TEST_KEY
16
- end
7
+ class Example < Reviewed::Base; end
17
8
 
18
9
  describe 'Class' do
10
+
19
11
  it 'responds to model_name' do
20
- Reviewed::Example.model_name.should eql("Reviewed::Example")
12
+ Example.model_name.should eql("Example")
21
13
  end
22
14
  end
23
15
 
24
- describe 'initialization' do
25
- it 'raises an error if a resource ID is not present' do
26
- expect {
27
- Reviewed::Example.new
28
- }.to raise_error(Reviewed::ResourceError)
16
+ describe 'initialize' do
17
+
18
+ it 'objectifies data' do
19
+ Example.any_instance.should_receive(:objectify).with({})
20
+ obj = Example.new( {} )
29
21
  end
30
22
 
31
- it 'returns a value' do
32
- model = Reviewed::Example.new(id: 1234, name: 'Test')
33
- model.id.should == 1234
23
+ it 'sets the data in the @attributes var' do
24
+ obj = Example.new( { foo: 'bar' } )
25
+ obj.instance_variable_get(:@attributes).should eql( { foo: 'bar' } )
34
26
  end
35
27
  end
36
28
 
37
29
  describe 'find' do
38
- before(:each) do
39
- Reviewed::Example.resource_name = 'article' # test
40
- end
41
-
42
- it 'raises an error if the api key is not set' do
43
- Reviewed.api_key = nil
30
+ use_vcr_cassette 'base/article/find'
44
31
 
45
- expect {
46
- Reviewed::Example.find('test')
47
- }.to raise_error(Reviewed::ConfigurationError)
32
+ it 'returns an article' do
33
+ Reviewed::Article.find("#{article_id}").should be_an_instance_of(Reviewed::Article)
48
34
  end
49
35
 
50
- context 'with a valid request' do
51
- use_vcr_cassette 'base/find_ok'
52
-
53
- before(:each) do
54
- @article = Reviewed::Article.find('big-green-egg-medium-charcoal-grill-review')
55
- end
56
-
57
- it 'fetches content from the api' do
58
- model = Reviewed::Example.find(@article.id)
59
- model.raw_response.should_not be_nil
60
- end
61
-
62
- it 'parses response json and returns an object' do
63
- model = Reviewed::Example.find(@article.id)
64
- model.class.should == Reviewed::Example
65
- model.name.should == 'Big Green Egg Medium Charcoal Grill Review'
66
- end
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}")
67
41
  end
42
+ end
68
43
 
69
- context 'with an invalid request' do
70
- use_vcr_cassette 'base/find_error_key'
71
-
72
- it 'complains if the api key is unauthorized' do
73
- Reviewed.api_key = 'xxxxxxxxxxxxxxxx'
74
-
75
- expect {
76
- Reviewed::Example.find('50241b9c5da4ac8d38000001')
77
- }.to raise_error(Reviewed::ResourceError, /unauthorized/i)
78
- end
44
+ describe 'resource_url' do
79
45
 
80
- it 'complains if the requested resource is not found' do
81
- expect {
82
- Reviewed::Example.find('notfound')
83
- }.to raise_error(Reviewed::ResourceError, /not found/i)
84
- end
46
+ it 'should the demodulized resource name' do
47
+ Reviewed::Article.resource_url.should eql("articles")
85
48
  end
86
49
  end
87
50
 
@@ -89,7 +52,7 @@ describe Reviewed::Base do
89
52
  use_vcr_cassette 'base/where_collection'
90
53
 
91
54
  it 'returns a collection' do
92
- collection = Reviewed::Article.all
55
+ collection = Reviewed::Article.where
93
56
  collection.class.should == Reviewed::Collection
94
57
  end
95
58
 
@@ -113,24 +76,26 @@ describe Reviewed::Base do
113
76
  end
114
77
  end
115
78
 
79
+ describe 'all' do
80
+
81
+ it 'calls where with empty params' do
82
+ Reviewed::Article.should_receive(:where).with({})
83
+ Reviewed::Article.all
84
+ end
85
+ end
86
+
116
87
  describe 'attributes' do
117
88
  it 'returns the named attribute (via method missing)' do
118
- model = Reviewed::Example.new(:id => 'id', :super_awesome => 'true')
89
+ model = Example.new(:id => 'id', :super_awesome => 'true')
119
90
  model.super_awesome.should == 'true'
120
91
  end
121
92
  end
122
93
 
123
- describe 'resource url' do
124
- it 'returns an individual resource' do
125
- Reviewed::Example.resource_url('article-123').should == 'https://the-guide-staging.herokuapp.com/api/v1/articles/article-123'
126
- end
127
-
128
- it 'returns a collection of resources' do
129
- Reviewed::Example.resource_url(nil).should == 'https://the-guide-staging.herokuapp.com/api/v1/articles'
130
- end
94
+ describe 'resource_url' do
131
95
 
132
- it 'includes a query parameter' do
133
- Reviewed::Example.resource_url(nil, page: 2).should == 'https://the-guide-staging.herokuapp.com/api/v1/articles?page=2'
96
+ it 'returns the pluralized demodulized class name' do
97
+ Reviewed::Article.resource_url.should eql('articles')
98
+ Example.resource_url.should eql('examples')
134
99
  end
135
100
  end
136
101
  end
@@ -0,0 +1,95 @@
1
+ require 'spec_helper.rb'
2
+
3
+ describe Reviewed::Client do
4
+
5
+ let(:client) { Reviewed::Client.new }
6
+
7
+ describe 'variables' do
8
+
9
+ [:api_key, :base_uri, :api_version].each do |var|
10
+ describe "#{var}" do
11
+
12
+ it 'exists' do
13
+ client.instance_variables.should include(:"@#{var}")
14
+ end
15
+ end
16
+ end
17
+ end
18
+
19
+ describe '#delete' do
20
+
21
+ it 'delegates to request' do
22
+ client.should_receive(:request).with(:delete, "path", kind_of(Hash))
23
+ client.delete("path", {})
24
+ end
25
+ end
26
+
27
+ describe '#get' do
28
+
29
+ it 'delegates to request' do
30
+ client.should_receive(:request).with(:get, "path", kind_of(Hash))
31
+ client.get("path", {})
32
+ end
33
+ end
34
+
35
+ describe '#post' do
36
+
37
+ it 'delegates to request' do
38
+ client.should_receive(:request).with(:post, "path", kind_of(Hash))
39
+ client.post("path", {})
40
+ end
41
+ end
42
+
43
+ describe '#put' do
44
+
45
+ it 'delegates to request' do
46
+ client.should_receive(:request).with(:put, "path", kind_of(Hash))
47
+ client.put("path", {})
48
+ end
49
+ end
50
+
51
+ describe '#url' do
52
+
53
+ it 'returns a url' do
54
+ client.url.should eql('http://localhost:3000/api/v1')
55
+ end
56
+ end
57
+
58
+ describe '#request' do
59
+
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')
64
+ end
65
+ end
66
+
67
+ describe '#connnection' do
68
+
69
+ let(:conn) { client.send(:connection) }
70
+
71
+ it 'returns a Faraday object' do
72
+ conn.should be_an_instance_of(Faraday::Connection)
73
+ end
74
+
75
+ it 'uses the UrlEncoded middleware' do
76
+ conn.builder.handlers.should include(Faraday::Request::UrlEncoded)
77
+ end
78
+
79
+ it 'uses a Json middleware' do
80
+ conn.builder.handlers.should include(FaradayMiddleware::ParseJson)
81
+ end
82
+
83
+ it 'uses a Hashie middleware' do
84
+ conn.builder.handlers.should include(FaradayMiddleware::Mashify)
85
+ end
86
+
87
+ it 'uses the NetHttp adapter' do
88
+ conn.builder.handlers.should include(Faraday::Adapter::NetHttp)
89
+ end
90
+
91
+ it 'sets the url' do
92
+ conn.url_prefix.to_s.should eql('http://localhost:3000/api/v1')
93
+ end
94
+ end
95
+ end
@@ -14,6 +14,7 @@ describe Reviewed::Collection do
14
14
  end
15
15
 
16
16
  describe 'collection data' do
17
+
17
18
  it 'is enumerable' do
18
19
  @collection.each do |product|
19
20
  product.class.should == Reviewed::Product
@@ -21,16 +22,13 @@ describe Reviewed::Collection do
21
22
  end
22
23
  end
23
24
 
24
- it 'contains the raw response' do
25
- @collection.raw_response.should_not be_blank
26
- end
27
-
28
25
  it 'fetches the first page by default' do
29
26
  @collection.current_page.should == 1
30
27
  end
31
28
  end
32
29
 
33
30
  describe 'next page' do
31
+
34
32
  it 'fetches the next page of results' do
35
33
  page2 = @collection.next_page
36
34
  page2.current_page.should == 2
@@ -38,11 +36,11 @@ describe Reviewed::Collection do
38
36
  end
39
37
 
40
38
  describe 'previous page' do
39
+
41
40
  it 'fetches the previous page of results' do
42
41
  page2 = @collection.next_page
43
42
  page1 = page2.previous_page
44
- @collection.should == page1
45
- page1.current_page.should == 1
43
+ @collection.current_page == page1.current_page
46
44
  end
47
45
 
48
46
  it 'returns nil if there is no previous page' do
@@ -51,6 +49,7 @@ describe Reviewed::Collection do
51
49
  end
52
50
 
53
51
  describe 'page attributes (pagination)' do
52
+
54
53
  it 'returns the total item count' do
55
54
  @collection.total.should > 1
56
55
  end
@@ -0,0 +1,44 @@
1
+ require 'spec_helper.rb'
2
+
3
+ describe Reviewed::Configurable do
4
+
5
+ let(:client) { Reviewed::Client.new }
6
+
7
+ describe '.options' do
8
+
9
+ it 'returns a default set of options' do
10
+ Reviewed::Configurable.options.should be_an_instance_of(Hash)
11
+ end
12
+ end
13
+
14
+ describe '#configure' do
15
+
16
+ it 'returns self' do
17
+ client.configure{}.should be_an_instance_of(Reviewed::Client)
18
+ end
19
+
20
+ it 'yields self' do
21
+ client.configure do |config|
22
+ config.api_key = 'test_key'
23
+ end
24
+ client.api_key.should eql('test_key')
25
+ end
26
+ end
27
+
28
+ describe '#verify_key' do
29
+
30
+ after(:each) { Reviewed.api_key = TEST_KEY }
31
+
32
+ it 'allow when api_key is present' do
33
+ client.base_uri.should_not be_nil
34
+ client.verify_key!
35
+ end
36
+
37
+ it 'raises a ConfigurationError if api_key not present' do
38
+ Reviewed.api_key = nil
39
+ expect {
40
+ client.verify_key!
41
+ }.to raise_error(Reviewed::ConfigurationError)
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,176 @@
1
+ require 'spec_helper.rb'
2
+
3
+ describe Reviewed::Embeddable do
4
+
5
+ after(:each) do
6
+ Reviewed::MockEmbeddable._embedded_many = nil
7
+ Reviewed::MockEmbeddable._embedded_one = nil
8
+ end
9
+
10
+ module Reviewed
11
+ class MockEmbeddable
12
+ include Reviewed::Embeddable
13
+ end
14
+ end
15
+
16
+ describe 'accessors' do
17
+
18
+ it '_embedded_many' do
19
+ Reviewed::MockEmbeddable._embedded_many = "test"
20
+ Reviewed::MockEmbeddable._embedded_many.should eql("test")
21
+ end
22
+
23
+ it '_embedded_one' do
24
+ Reviewed::MockEmbeddable._embedded_one = "test"
25
+ Reviewed::MockEmbeddable._embedded_one.should eql("test")
26
+ end
27
+ end
28
+
29
+ describe '#has_many' do
30
+
31
+ context 'no options' do
32
+
33
+ context 'valid name' do
34
+
35
+ it 'stores the correct embedded relationship' do
36
+ Reviewed::MockEmbeddable._embedded_many.should be_empty
37
+ Reviewed::MockEmbeddable.has_many("mock_embeddables")
38
+ Reviewed::MockEmbeddable._embedded_many.should eql([{ "mock_embeddables" => Reviewed::MockEmbeddable }])
39
+ end
40
+ end
41
+
42
+ context 'invalid name' do
43
+
44
+ it 'errors' do
45
+ expect {
46
+ Reviewed::MockEmbeddable.has_many("bad_name")
47
+ }.to raise_error
48
+ end
49
+ end
50
+ end
51
+
52
+ context ":as" do
53
+
54
+ it 'stores the correct embedded relationship' do
55
+ Reviewed::MockEmbeddable._embedded_many.should be_empty
56
+ Reviewed::MockEmbeddable.has_many("mock_embeddables", as: "test")
57
+ Reviewed::MockEmbeddable._embedded_many.should eql([{ "test" => Reviewed::MockEmbeddable }])
58
+ end
59
+ end
60
+
61
+ context "class_name" do
62
+
63
+ it 'stores the correct embedded relationship' do
64
+ Reviewed::MockEmbeddable._embedded_many.should be_empty
65
+ Reviewed::MockEmbeddable.has_many("mock_embeddables", class_name: "Reviewed::Article")
66
+ Reviewed::MockEmbeddable._embedded_many.should eql([{ "mock_embeddables" => Reviewed::Article }])
67
+ end
68
+ end
69
+ end
70
+
71
+ describe '#has_one' do
72
+
73
+ it 'stores the correct embedded relationship' do
74
+ Reviewed::MockEmbeddable._embedded_one.should be_empty
75
+ Reviewed::MockEmbeddable.has_one("mock_embeddable")
76
+ Reviewed::MockEmbeddable._embedded_one.should eql([{ "mock_embeddable" => Reviewed::MockEmbeddable }])
77
+ end
78
+ end
79
+
80
+ describe 'objectify' do
81
+
82
+ let(:mocked) { Reviewed::MockEmbeddable.new }
83
+ let(:data) { { articles: [1,2,3] } }
84
+
85
+ it 'calls objectify_has_many' do
86
+ mocked.should_receive(:objectify_has_many)
87
+ mocked.objectify({})
88
+ end
89
+
90
+ it 'calls objectify_has_one' do
91
+ mocked.should_receive(:objectify_has_one)
92
+ mocked.objectify({})
93
+ end
94
+ end
95
+
96
+ describe 'objectify_has_many' do
97
+
98
+ let(:data) { { "articles" => [{},{}] } }
99
+ let(:mocked) { Reviewed::MockEmbeddable.new }
100
+
101
+ before(:each) do
102
+ Reviewed::MockEmbeddable._embedded_many = [ { "articles" => Reviewed::Article } ]
103
+ end
104
+
105
+ it 'objectifies' do
106
+ mocked.objectify_has_many(data)["articles"].each do |obj|
107
+ obj.should be_an_instance_of Reviewed::Article
108
+ end
109
+ end
110
+ end
111
+
112
+ describe 'objectify_has_one' do
113
+
114
+ let(:data) { { "article" => {} } }
115
+ let(:mocked) { Reviewed::MockEmbeddable.new }
116
+
117
+ before(:each) do
118
+ Reviewed::MockEmbeddable._embedded_one = [ { "article" => Reviewed::Article } ]
119
+ end
120
+
121
+ it 'objectifies' do
122
+ mocked.objectify_has_one(data)["article"].should be_an_instance_of(Reviewed::Article)
123
+ end
124
+ end
125
+
126
+ describe '.embedded_name' do
127
+
128
+ context 'plural' do
129
+
130
+ it 'returns a class name' do
131
+ Reviewed::Embeddable.embedded_name("mock_embeddables").should eql("Reviewed::MockEmbeddable")
132
+ end
133
+ end
134
+
135
+ context 'singular' do
136
+
137
+ it 'returns a class name' do
138
+ Reviewed::Embeddable.embedded_name("mock_embeddable").should eql("Reviewed::MockEmbeddable")
139
+ end
140
+ end
141
+ end
142
+
143
+ describe '.embedded_class' do
144
+
145
+ context 'opts_name' do
146
+
147
+ context 'valid' do
148
+
149
+ it 'returns a class_constant' do
150
+ Reviewed::Embeddable.embedded_class("test", "Reviewed::MockEmbeddable")
151
+ .should eql(Reviewed::MockEmbeddable)
152
+ end
153
+ end
154
+
155
+ context 'invalid' do
156
+
157
+ it 'errors' do
158
+ expect {
159
+ Reviewed::Embeddable.embedded_class("test", "Reviewed::MockBad")
160
+ }.to raise_error
161
+ end
162
+ end
163
+ end
164
+
165
+ context 'name' do
166
+
167
+ context 'valid' do
168
+
169
+ it 'returns a class_constant' do
170
+ Reviewed::Embeddable.embedded_class("mock_embeddables")
171
+ .should eql(Reviewed::MockEmbeddable)
172
+ end
173
+ end
174
+ end
175
+ end
176
+ end