reviewed 0.0.9 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,18 @@
1
+ module Faraday
2
+ class GlobalParams < Faraday::Middleware
3
+
4
+ def call(env)
5
+ params = Reviewed.global_params
6
+
7
+ if params && params.is_a?(Hash)
8
+ path = env[:url].path
9
+ query = env[:url].query || {}
10
+ env[:url].query = Faraday::Utils.build_query(query.merge(params))
11
+ end
12
+
13
+ @app.call(env)
14
+ end
15
+ end
16
+ end
17
+
18
+ Faraday.register_middleware :request, global_params: Faraday::GlobalParams
@@ -8,10 +8,6 @@ module Reviewed
8
8
  has_many :products
9
9
  has_many :attachments
10
10
 
11
- class << self
12
- attr_accessor :branch
13
- end
14
-
15
11
  def find_page(slug)
16
12
  pages.find { |page| page.slug.match(/#{slug}/i) }
17
13
  end
@@ -48,7 +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
+ faraday.request :global_params
52
52
  faraday.request :url_encoded
53
53
  faraday.adapter Faraday.default_adapter
54
54
  end
@@ -1,4 +1,4 @@
1
1
  module Reviewed
2
- VERSION = "0.0.9"
2
+ VERSION = "0.1.0"
3
3
  API_VERSION = 'v1'
4
4
  end
data/lib/reviewed.rb CHANGED
@@ -2,7 +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
+ require 'faraday/global_params'
6
6
  require 'active_support/inflector'
7
7
  require 'active_support/core_ext'
8
8
  require 'active_model'
@@ -29,6 +29,8 @@ module Reviewed
29
29
 
30
30
  class << self
31
31
 
32
+ attr_accessor :global_params
33
+
32
34
  def client
33
35
  @client ||= Reviewed::Client.new
34
36
  end
data/spec/article_spec.rb CHANGED
@@ -2,18 +2,6 @@ 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
-
17
5
  describe 'associations' do
18
6
 
19
7
  describe 'pages' do
@@ -0,0 +1,33 @@
1
+ require 'spec_helper.rb'
2
+
3
+ describe Faraday::GlobalParams do
4
+
5
+ class FauxApp
6
+ def call(env)
7
+ return env
8
+ end
9
+ end
10
+
11
+ let(:faraday) { Faraday::GlobalParams.new(FauxApp.new) }
12
+ let(:env) { { url: URI.parse("http://www.the-guide-staging.com/api/v1/articles") } }
13
+
14
+ context 'branch present' do
15
+
16
+ before(:each) do
17
+ Reviewed.stub!(:global_params).and_return( foo: "bar" )
18
+ end
19
+
20
+ it 'adds the branch to the query params' do
21
+ out = faraday.call(env)
22
+ out[:url].query.should eql("foo=bar")
23
+ end
24
+ end
25
+
26
+ context 'no branch present' do
27
+
28
+ it 'does not add the branch to the query params' do
29
+ out = faraday.call(env)
30
+ out[:url].query.should be_nil
31
+ end
32
+ end
33
+ end
@@ -2,6 +2,19 @@ require 'spec_helper.rb'
2
2
 
3
3
  describe Reviewed do
4
4
 
5
+ describe 'accessors' do
6
+
7
+ after(:each) do
8
+ Reviewed.global_params = nil
9
+ end
10
+
11
+ it 'global params' do
12
+ Reviewed.global_params = { foo: 'bar' }
13
+ Reviewed.global_params.should eql( { foo: 'bar' } )
14
+ end
15
+ end
16
+
17
+
5
18
  describe '.client' do
6
19
 
7
20
  it 'returns a Reviewed::Client' do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: reviewed
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.9
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2012-11-26 00:00:00.000000000 Z
13
+ date: 2012-11-27 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activesupport
@@ -186,7 +186,7 @@ files:
186
186
  - LICENSE
187
187
  - README.md
188
188
  - Rakefile
189
- - lib/faraday/branches.rb
189
+ - lib/faraday/global_params.rb
190
190
  - lib/reviewed.rb
191
191
  - lib/reviewed/article.rb
192
192
  - lib/reviewed/attachment.rb
@@ -212,7 +212,7 @@ files:
212
212
  - spec/collection_spec.rb
213
213
  - spec/configurable_spec.rb
214
214
  - spec/embeddable_spec.rb
215
- - spec/faraday/branches_spec.rb
215
+ - spec/faraday/global_params_spec.rb
216
216
  - spec/fixtures/vcr/article/attachments.yml
217
217
  - spec/fixtures/vcr/article/find_page.yml
218
218
  - spec/fixtures/vcr/article/products.yml
@@ -266,7 +266,7 @@ test_files:
266
266
  - spec/collection_spec.rb
267
267
  - spec/configurable_spec.rb
268
268
  - spec/embeddable_spec.rb
269
- - spec/faraday/branches_spec.rb
269
+ - spec/faraday/global_params_spec.rb
270
270
  - spec/fixtures/vcr/article/attachments.yml
271
271
  - spec/fixtures/vcr/article/find_page.yml
272
272
  - spec/fixtures/vcr/article/products.yml
@@ -1,19 +0,0 @@
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
@@ -1,62 +0,0 @@
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