reviewed 0.0.8 → 0.0.9

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,19 @@
1
+ module Faraday
2
+ class Branches < Faraday::Middleware
3
+
4
+ def call(env)
5
+ path = env[:url].path
6
+ branch = Reviewed::Article.branch
7
+
8
+ if path =~ /#{Reviewed::Article.resource_url}/ && branch
9
+ query = env[:url].query || {}
10
+ branch = Reviewed::Article.branch
11
+ env[:url].query = Faraday::Utils.build_query(query.merge(branch: branch))
12
+ end
13
+
14
+ @app.call(env)
15
+ end
16
+ end
17
+ end
18
+
19
+ Faraday.register_middleware :request, branches: Faraday::Branches
@@ -8,6 +8,10 @@ module Reviewed
8
8
  has_many :products
9
9
  has_many :attachments
10
10
 
11
+ class << self
12
+ attr_accessor :branch
13
+ end
14
+
11
15
  def find_page(slug)
12
16
  pages.find { |page| page.slug.match(/#{slug}/i) }
13
17
  end
@@ -48,6 +48,7 @@ module Reviewed
48
48
  @connection ||= ::Faraday.new(url: url) do |faraday|
49
49
  faraday.response :mashify
50
50
  faraday.response :json
51
+ faraday.request :branches
51
52
  faraday.request :url_encoded
52
53
  faraday.adapter Faraday.default_adapter
53
54
  end
@@ -19,7 +19,7 @@ module Reviewed
19
19
  self.items = []
20
20
 
21
21
  data.each do |obj|
22
- self.items << klass.from_response(obj)
22
+ self.items << klass.new(obj)
23
23
  end
24
24
  end
25
25
 
@@ -0,0 +1,4 @@
1
+ module Reviewed
2
+ class ManufacturerSpec < Base
3
+ end
4
+ end
@@ -3,6 +3,7 @@ require 'reviewed/attachment'
3
3
  module Reviewed
4
4
  class Product < Base
5
5
  has_many :attachments
6
+ has_many :manufacturer_specs
6
7
 
7
8
  def attachments(tag=nil)
8
9
  if tag.present?
@@ -9,17 +9,13 @@ module Reviewed
9
9
 
10
10
  def object_from_response(method, url, params={})
11
11
  response = Reviewed.send(method, url, params)
12
- self.from_response(response.body)
12
+ self.new(response.body)
13
13
  end
14
14
 
15
15
  def collection_from_response(method, url, params={})
16
16
  response = Reviewed.send(method, url, params)
17
17
  Reviewed::Collection.new(self, response, params)
18
18
  end
19
-
20
- def from_response(data)
21
- self.new(data)
22
- end
23
19
  end
24
20
  end
25
21
  end
@@ -1,4 +1,4 @@
1
1
  module Reviewed
2
- VERSION = "0.0.8"
2
+ VERSION = "0.0.9"
3
3
  API_VERSION = 'v1'
4
4
  end
data/lib/reviewed.rb CHANGED
@@ -2,6 +2,7 @@ $LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__)))
2
2
 
3
3
  require 'faraday'
4
4
  require 'faraday_middleware'
5
+ require 'faraday/branches'
5
6
  require 'active_support/inflector'
6
7
  require 'active_support/core_ext'
7
8
  require 'active_model'
@@ -14,6 +15,7 @@ require 'reviewed/client'
14
15
  require 'reviewed/collection'
15
16
  require 'reviewed/base'
16
17
 
18
+ require 'reviewed/manufacturer_spec'
17
19
  require 'reviewed/product'
18
20
  require 'reviewed/article'
19
21
  require 'reviewed/author'
data/spec/article_spec.rb CHANGED
@@ -2,6 +2,18 @@ require 'spec_helper'
2
2
 
3
3
  describe Reviewed::Article do
4
4
 
5
+ describe 'accessors' do
6
+
7
+ after(:each) do
8
+ Reviewed::Article.branch = nil
9
+ end
10
+
11
+ it 'global params' do
12
+ Reviewed::Article.branch = 'test'
13
+ Reviewed::Article.branch.should eql('test')
14
+ end
15
+ end
16
+
5
17
  describe 'associations' do
6
18
 
7
19
  describe 'pages' do
@@ -95,18 +95,32 @@ describe Reviewed::Embeddable do
95
95
 
96
96
  describe 'objectify_has_many' do
97
97
 
98
- let(:data) { { "articles" => [{},{}] } }
99
98
  let(:mocked) { Reviewed::MockEmbeddable.new }
100
99
 
101
100
  before(:each) do
102
101
  Reviewed::MockEmbeddable._embedded_many = [ { "articles" => Reviewed::Article } ]
103
102
  end
104
103
 
105
- it 'objectifies' do
106
- mocked.objectify_has_many(data)["articles"].each do |obj|
107
- obj.should be_an_instance_of Reviewed::Article
104
+ context 'relationship exists in json' do
105
+
106
+ let(:data) { { "articles" => [{},{}] } }
107
+
108
+ it 'objectifies' do
109
+ mocked.objectify_has_many(data)["articles"].each do |obj|
110
+ obj.should be_an_instance_of Reviewed::Article
111
+ end
112
+ end
113
+ end
114
+
115
+ context 'relationship does not have json' do
116
+
117
+ let(:data) { { foo: 'bar' } }
118
+
119
+ it 'does not error' do
120
+ mocked.objectify_has_many(data).should eql( { foo: 'bar' } )
108
121
  end
109
122
  end
123
+
110
124
  end
111
125
 
112
126
  describe 'objectify_has_one' do
@@ -0,0 +1,62 @@
1
+ require 'spec_helper.rb'
2
+
3
+ describe Faraday::Branches do
4
+
5
+ class FauxApp
6
+ def call(env)
7
+ return env
8
+ end
9
+ end
10
+
11
+ let(:faraday) { Faraday::Branches.new(FauxApp.new) }
12
+
13
+ context 'Articles' do
14
+
15
+ let(:env) { { url: URI.parse("http://www.the-guide-staging.com/api/v1/articles") } }
16
+
17
+ context 'branch present' do
18
+
19
+ before(:each) do
20
+ Reviewed::Article.stub!(:branch).and_return("test")
21
+ end
22
+
23
+ it 'adds the branch to the query params' do
24
+ out = faraday.call(env)
25
+ out[:url].query.should eql("branch=test")
26
+ end
27
+ end
28
+
29
+ context 'no branch present' do
30
+
31
+ it 'does not add the branch to the query params' do
32
+ out = faraday.call(env)
33
+ out[:url].query.should be_nil
34
+ end
35
+ end
36
+ end
37
+
38
+ context 'Others' do
39
+
40
+ let(:env) { { url: URI.parse("http://www.the-guide-staging.com/api/v1/products") } }
41
+
42
+ context 'branch present' do
43
+
44
+ before(:each) do
45
+ Reviewed::Article.stub!(:branch).and_return("test")
46
+ end
47
+
48
+ it 'adds the branch to the query params' do
49
+ out = faraday.call(env)
50
+ out[:url].query.should eql(nil)
51
+ end
52
+ end
53
+
54
+ context 'no branch present' do
55
+
56
+ it 'does not add the branch to the query params' do
57
+ out = faraday.call(env)
58
+ out[:url].query.should be_nil
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,109 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: get
5
+ uri: https://the-guide-staging.herokuapp.com/api/v1/products/minden-master-ii
6
+ body:
7
+ encoding: US-ASCII
8
+ string: ''
9
+ headers:
10
+ X-Reviewed-Authorization:
11
+ - '1234567890'
12
+ Accept-Encoding:
13
+ - gzip;q=1.0,deflate;q=0.6,identity;q=0.3
14
+ Accept:
15
+ - ! '*/*'
16
+ User-Agent:
17
+ - Ruby
18
+ response:
19
+ status:
20
+ code: 200
21
+ message: !binary |-
22
+ T0s=
23
+ headers:
24
+ !binary "QWNjZXNzLUNvbnRyb2wtQWxsb3ctQ3JlZGVudGlhbHM=":
25
+ - !binary |-
26
+ dHJ1ZQ==
27
+ !binary "QWNjZXNzLUNvbnRyb2wtQWxsb3ctSGVhZGVycw==":
28
+ - !binary |-
29
+ eC1wYWdpbmF0aW9uLCB4LXJlcXVlc3RlZC13aXRoLCB4LXJlcXVlc3RlZC1i
30
+ eSwgeC1yZXZpZXdlZC1hdXRob3JpemF0aW9uLCBDb250ZW50LVR5cGU=
31
+ !binary "QWNjZXNzLUNvbnRyb2wtQWxsb3ctTWV0aG9kcw==":
32
+ - !binary |-
33
+ T1BUSU9OUywgR0VULCBQT1NULCBQVVQsIERFTEVURQ==
34
+ !binary "QWNjZXNzLUNvbnRyb2wtQWxsb3ctT3JpZ2lu":
35
+ - !binary |-
36
+ Kg==
37
+ !binary "QWNjZXNzLUNvbnRyb2wtTWF4LUFnZQ==":
38
+ - !binary |-
39
+ MTAwMA==
40
+ !binary "Q2FjaGUtQ29udHJvbA==":
41
+ - !binary |-
42
+ bm8tY2FjaGUsIG5vLXN0b3Jl
43
+ !binary "Q29udGVudC1FbmNvZGluZw==":
44
+ - !binary |-
45
+ Z3ppcA==
46
+ !binary "Q29udGVudC1UeXBl":
47
+ - !binary |-
48
+ YXBwbGljYXRpb24vanNvbjsgY2hhcnNldD11dGYtOA==
49
+ !binary "RGF0ZQ==":
50
+ - !binary |-
51
+ TW9uLCAyNiBOb3YgMjAxMiAyMDozOTowMSBHTVQ=
52
+ !binary "TGFzdC1Nb2RpZmllZA==":
53
+ - !binary |-
54
+ RnJpLCAwOSBOb3YgMjAxMiAyMDoxNjo0MCBHTVQ=
55
+ !binary "U2VydmVy":
56
+ - !binary |-
57
+ dGhpbiAxLjUuMCBjb2RlbmFtZSBLbmlmZQ==
58
+ !binary "U3RyaWN0LVRyYW5zcG9ydC1TZWN1cml0eQ==":
59
+ - !binary |-
60
+ bWF4LWFnZT0zMTUzNjAwMA==
61
+ !binary "VmFyeQ==":
62
+ - !binary |-
63
+ QWNjZXB0LUVuY29kaW5n
64
+ !binary "WC1SYWNrLUNhY2hl":
65
+ - !binary |-
66
+ bWlzcw==
67
+ !binary "WC1SYXRlLUxpbWl0":
68
+ - !binary |-
69
+ MTAwMDAw
70
+ !binary "WC1SZXF1ZXN0LUlk":
71
+ - !binary |-
72
+ M2U2ZDkzNmYzM2MxODlhYmU5YzAxNWE4Y2YwY2ExZGE=
73
+ !binary "WC1SdW50aW1l":
74
+ - !binary |-
75
+ MC4yMDk1NTQ=
76
+ !binary "WC1VYS1Db21wYXRpYmxl":
77
+ - !binary |-
78
+ SUU9RWRnZSxjaHJvbWU9MQ==
79
+ !binary "VHJhbnNmZXItRW5jb2Rpbmc=":
80
+ - !binary |-
81
+ Y2h1bmtlZA==
82
+ !binary "Q29ubmVjdGlvbg==":
83
+ - !binary |-
84
+ a2VlcC1hbGl2ZQ==
85
+ body:
86
+ encoding: ASCII-8BIT
87
+ string: !binary |-
88
+ H4sIAKhknVAAA8RWS2/bOBD+K4au6wdJvSxd9rJAN4cAiyKnLRbC8CGHrV4g
89
+ qWTTIP+9Q8l25LZWUCBQfbA9nE+cb16aeQ60DPIgJpkIMwDFBCQqyeJYCJqJ
90
+ JFgHwihwShbgEMcIZRsSb2h8x2hOWU7jfxHTd/I7DKUbkt0xktMkj4jHNFAr
91
+ 1N7qRqpmdQvWKbO6uUGNrfoDaupBs6kHzUZr1BTuqfMP/d3WKs8/GF1VFn/B
92
+ os6rbJB/Cv4xbQeNCv5bB51pZ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