activeresource_headers 0.0.1

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/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.swp
data/.travis.yml ADDED
@@ -0,0 +1,10 @@
1
+ language: ruby
2
+
3
+ rvm:
4
+ - 1.9.3
5
+
6
+ script: bundle exec rspec
7
+
8
+ branches:
9
+ only:
10
+ - master
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in activeresource_headers.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Denis Yagofarov
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,53 @@
1
+ [![Build Status](https://secure.travis-ci.org/denyago/activeresource_headers.png)](http://travis-ci.org/denyago/activeresource_headers)
2
+ [![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/denyago/activeresource_headers)
3
+
4
+ # Activeresource Headers
5
+
6
+ Add ability to send custom headers on each http request by an ActiveResource model.
7
+
8
+ There are two ways of setting headers.
9
+ * class method ```custom_headers do ... end``` in a model, that is an ActiveResource::Base kind
10
+ * chainable method ```with_headers```, that can be putten between model class and ```find``` method
11
+
12
+ ## Examples
13
+
14
+ ```ruby
15
+
16
+ class Person < ActiveResource::Base
17
+ include ActiveresourceHeaders::CustomHeaders
18
+
19
+ self.site = 'http://example.com'
20
+
21
+ custom_headers do
22
+ {"My-Time" => Time.now.to_s}
23
+ end
24
+ end
25
+
26
+ Person.find(:all) #=> will add header "My-Time: 2012-10-02 18:56:18 +0300"
27
+
28
+ Person.with_headers("My-Time" => "to drink!", "Age" => "25+").find(:all)
29
+ #=> will add headers "My-Time: to drink!" and "Age: 25+"
30
+ ```
31
+
32
+
33
+ ## Installation
34
+
35
+ Add this line to your application's Gemfile:
36
+
37
+ gem 'activeresource_headers'
38
+
39
+ And then execute:
40
+
41
+ $ bundle
42
+
43
+ Or install it yourself as:
44
+
45
+ $ gem install activeresource_headers
46
+
47
+ ## Contributing
48
+
49
+ 1. Fork it
50
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
51
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
52
+ 4. Push to the branch (`git push origin my-new-feature`)
53
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,28 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'activeresource_headers/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "activeresource_headers"
8
+ gem.version = ActiveresourceHeaders::VERSION
9
+ gem.authors = ["Denis Yagofarov"]
10
+ gem.email = ["denyago@gmail.com"]
11
+ gem.description = %q{Send custom headers with ActiveResource requests}
12
+ gem.summary = %q{Adds custom_headers { ... } method to ActiveResource model. Those headers merged to deafult ones, each time AR requests api.}
13
+ gem.homepage = ""
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ rails_version = "~> 3.2"
21
+
22
+ gem.add_dependency 'activeresource', rails_version
23
+
24
+ gem.add_development_dependency 'rspec', '~> 2.11'
25
+ gem.add_development_dependency 'fakeweb'
26
+ gem.add_development_dependency 'activesupport', rails_version
27
+
28
+ end
@@ -0,0 +1,5 @@
1
+ require "activeresource_headers/version"
2
+ require "activeresource_headers/custom_headers"
3
+
4
+ module ActiveresourceHeaders
5
+ end
@@ -0,0 +1,62 @@
1
+ require 'active_support/concern'
2
+ require 'active_support/core_ext/hash/deep_merge'
3
+
4
+ module ActiveresourceHeaders
5
+ ##
6
+ # When included to #ActiveResource::Base model,
7
+ # adds mthods to set additional headers for http
8
+ # requests.
9
+ module CustomHeaders
10
+ extend ActiveSupport::Concern
11
+
12
+ module ClassMethods
13
+
14
+ # Sets block to execute each time. Block must
15
+ # respond to #call and return #Hash each time.
16
+ def custom_headers(&block)
17
+ @custom_headers = block
18
+ end
19
+
20
+ # Saves headers #Hash for use in <i>current</i> request.
21
+ def with_headers(headers={})
22
+ @with_headers = headers
23
+ self
24
+ end
25
+
26
+ protected
27
+
28
+ # This kind of ActiveResource::Base.connection gets connection
29
+ # by <tt>super</tt> and restores it's default header, then it
30
+ # sets all the custom headers and clears @with_headers.
31
+ #
32
+ # Returns #ActiveResource::Connection
33
+ def connection
34
+ @custom_connection = super
35
+ toggle_default_header
36
+ set_headers
37
+ @with_headers = nil
38
+ @custom_connection
39
+ end
40
+
41
+ private
42
+ # Saves connection default header for first time, and restores it
43
+ # in connection every next time.
44
+ def toggle_default_header
45
+ if @last_default_header
46
+ @custom_connection.send(:default_header).clear
47
+ @custom_connection.send(:default_header).update(@last_default_header)
48
+ else
49
+ @last_default_header = @custom_connection.send(:default_header)
50
+ end
51
+ end
52
+
53
+ # Sets all the custom headers, defined by #custom_headers and #with_headers
54
+ def set_headers
55
+ headers = @custom_headers ? @custom_headers.call : {}
56
+ headers = headers.deep_merge((@with_headers || {}))
57
+ @custom_connection.send(:default_header).update(headers)
58
+ end
59
+ end
60
+
61
+ end
62
+ end
@@ -0,0 +1,3 @@
1
+ module ActiveresourceHeaders
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,63 @@
1
+ require 'spec_helper'
2
+
3
+ describe ActiveresourceHeaders::CustomHeaders do
4
+
5
+ require File.expand_path('spec/spec_helper')
6
+
7
+ describe "while using #custom_headers class method" do
8
+ before(:each) do
9
+ unset_const(:Profile)
10
+ class Profile < ActiveResource::Base
11
+ include ActiveresourceHeaders::CustomHeaders
12
+
13
+ self.site = REMOTE_HOST
14
+
15
+ custom_headers do
16
+ {time: self.time}
17
+ end
18
+ end
19
+ FakeWeb.register_uri(:get, "#{REMOTE_HOST}/profiles.json", body: [].to_json)
20
+ Profile.stub(:time).and_return("yesterday")
21
+ end
22
+
23
+ it "runs it's block each time" do
24
+ Profile.find(:all)
25
+ FakeWeb.last_request[:time].should eq('yesterday')
26
+
27
+ Profile.stub(:time).and_return("tomorrow")
28
+
29
+ Profile.find(:all)
30
+ FakeWeb.last_request[:time].should eq('tomorrow')
31
+ end
32
+
33
+ it "deeply merges it's headers from #with_headers method" do
34
+ Profile.with_headers(time: 'Soon!').find(:all)
35
+ FakeWeb.last_request[:time].should eq('Soon!')
36
+ end
37
+ end
38
+
39
+ describe "#with_headers" do
40
+ before(:each) do
41
+ unset_const(:Profile)
42
+ class Profile < ActiveResource::Base
43
+ include ActiveresourceHeaders::CustomHeaders
44
+ self.site = REMOTE_HOST
45
+ end
46
+ FakeWeb.register_uri(:get, "#{REMOTE_HOST}/profiles.json", body: [].to_json)
47
+ Profile.with_headers('Authorization' => 'OAuth2 token', 'Client-Id' => 'xyz').find(:all)
48
+ end
49
+ it "is chainable method for adding custom headers for the request" do
50
+ FakeWeb.last_request["authorization"].should eq('OAuth2 token')
51
+ FakeWeb.last_request["client-id"].should eq('xyz')
52
+ end
53
+
54
+ it "don't remembers it's custom headers between requests" do
55
+ FakeWeb.register_uri(:get, "#{REMOTE_HOST}/profiles.json?hello=world", body: [].to_json)
56
+ Profile.find(:all, params: {hello: 'world'})
57
+ FakeWeb.last_request["authorization"].should be_nil
58
+ FakeWeb.last_request["client-id"].should be_nil
59
+ end
60
+
61
+ end
62
+
63
+ end
@@ -0,0 +1,27 @@
1
+ require 'rspec'
2
+ require 'fake_web'
3
+
4
+ require 'activeresource_headers'
5
+
6
+ require 'active_resource'
7
+ require 'active_support/inflector'
8
+
9
+ RSpec.configure do |config|
10
+ config.color_enabled = true
11
+
12
+ config.before(:suite) do
13
+ FakeWeb.allow_net_connect = false
14
+ end
15
+
16
+ config.after(:each) do
17
+ FakeWeb.clean_registry
18
+ end
19
+ end
20
+
21
+ REMOTE_HOST = "http://127.0.0.1:3000"
22
+
23
+ def unset_const(const_name)
24
+ const_name = const_name.to_sym
25
+ const_owner = Module.constants.select { |c| c.to_s.constantize.constants(false).include?(const_name) rescue false }.first.to_s.constantize
26
+ !const_owner.send(:remove_const, const_name) rescue true
27
+ end
metadata ADDED
@@ -0,0 +1,124 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activeresource_headers
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Denis Yagofarov
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-02 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activeresource
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '3.2'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '3.2'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '2.11'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '2.11'
46
+ - !ruby/object:Gem::Dependency
47
+ name: fakeweb
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: activesupport
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: '3.2'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: '3.2'
78
+ description: Send custom headers with ActiveResource requests
79
+ email:
80
+ - denyago@gmail.com
81
+ executables: []
82
+ extensions: []
83
+ extra_rdoc_files: []
84
+ files:
85
+ - .gitignore
86
+ - .travis.yml
87
+ - Gemfile
88
+ - LICENSE.txt
89
+ - README.md
90
+ - Rakefile
91
+ - activeresource_headers.gemspec
92
+ - lib/activeresource_headers.rb
93
+ - lib/activeresource_headers/custom_headers.rb
94
+ - lib/activeresource_headers/version.rb
95
+ - spec/activeresource_headers/custom_headers_spec.rb
96
+ - spec/spec_helper.rb
97
+ homepage: ''
98
+ licenses: []
99
+ post_install_message:
100
+ rdoc_options: []
101
+ require_paths:
102
+ - lib
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ none: false
105
+ requirements:
106
+ - - ! '>='
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ required_rubygems_version: !ruby/object:Gem::Requirement
110
+ none: false
111
+ requirements:
112
+ - - ! '>='
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ requirements: []
116
+ rubyforge_project:
117
+ rubygems_version: 1.8.24
118
+ signing_key:
119
+ specification_version: 3
120
+ summary: Adds custom_headers { ... } method to ActiveResource model. Those headers
121
+ merged to deafult ones, each time AR requests api.
122
+ test_files:
123
+ - spec/activeresource_headers/custom_headers_spec.rb
124
+ - spec/spec_helper.rb