Permalink
Browse files

Merge pull request #7 from shutl/add_optional_headers

add ability to add additonal headers
  • Loading branch information...
drouchy committed Nov 5, 2013
2 parents f472e59 + e4ee686 commit f88e25f68f1501b32a5f6e27ad7b8461cb7b21cb
View
@@ -1,15 +1,7 @@
language: ruby
-
rvm:
- - 1.9.2
- 1.9.3
- - jruby-19mode
-
+ - jruby-19mode # JRuby in 1.9 mode
jdk:
- openjdk7
- - oraclejdk7
-
-matrix:
- allow_failures:
- - rvm: jruby-19mode
-
+ - oraclejdk7
@@ -36,10 +36,10 @@ def update_attributes(attrs)
attrs.each { |a, v| instance_variable_set(:"@#{a}", v) }
end
- def update!(attrs)
+ def update!(attrs, headers = {})
new_attributes = attributes.merge attrs
update_attributes(self.class.add_resource_id_to new_attributes)
- save
+ save(headers)
end
def save options={}
@@ -19,17 +19,8 @@ def connection
end
end
- def headers
- {
- 'Accept' => 'application/json',
- 'Content-Type' => 'application/json',
- 'User-Agent' => "Shutl Resource Gem v#{Shutl::Resource::VERSION}"
- }
- end
-
def find(args = {}, params = {})
params = args if @singular_resource
- auth_options = { auth: params.delete(:auth), from: params.delete(:from) }
if @singular_resource
url = singular_member_url params
@@ -42,7 +33,7 @@ def find(args = {}, params = {})
end
response = connection.get(url) do |req|
- req.headers = headers_with_auth(auth_options)
+ req.headers = generate_request_header(header_options(params))
end
check_fail response, "Failed to find #{name}! args: #{args}, params: #{params}"
@@ -56,7 +47,7 @@ def create attributes = {}, options = {}
attributes.delete "response"
response = connection.post(url) do |req|
- req.headers = headers_with_auth(options)
+ req.headers = generate_request_header(header_options(options))
req.body = { @resource_name => attributes }.to_json
end
@@ -75,7 +66,7 @@ def destroy instance, options = {}
instance,
:delete,
{}.to_json,
- headers_with_auth(options),
+ generate_request_header(header_options(options)),
failure_message
).success?
end
@@ -88,7 +79,7 @@ def save instance, options = {}
response = perform_action(instance,
:put,
body,
- headers_with_auth(options),
+ generate_request_header(header_options(options)),
"Save failed")
response.success?
@@ -100,15 +91,14 @@ def update args, options = {}
def all(args = {})
- auth_options = { auth: args.delete(:auth), from: args.delete(:from) }
partition = args.partition { |key, value| !remote_collection_url.index(":#{key}").nil? }
url_args = partition.first.inject({}) { |h, pair| h[pair.first] = pair.last; h }
params = partition.last.inject({}) { |h, pair| h[pair.first] = pair.last; h }
url = generate_collection_url url_args, params
response = connection.get(url) do |req|
- req.headers = headers_with_auth(auth_options)
+ req.headers = generate_request_header(header_options(args))
end
check_fail response, "Failed to find all #{name.downcase.pluralize}"
@@ -229,10 +219,38 @@ def generate_collection_url *args
private
+
+ def headers
+ {
+ 'Accept' => 'application/json',
+ 'Content-Type' => 'application/json',
+ 'User-Agent' => "Shutl Resource Gem v#{Shutl::Resource::VERSION}"
+ }
+ end
+
+ def header_options params
+ header_opts = params[:headers] || {}
+ header_opts.merge!(authorization: params[:auth]) if params[:auth]
+ header_opts.merge!(from: params[:from]) if params[:from]
+ header_opts
+ end
+
+
+ def generate_request_header header_options = {}
+ header_options.inject(headers) do |h, (k,v)|
+ h[header_name(k.to_s)] = v if v
+ h
+ end
+ end
+
+ def header_name header_key
+ header_key.split(%r{\_|\-}).map {|e| e.capitalize }.join("-")
+ end
+
def headers_with_auth options = {}
headers.tap do |h|
h['Authorization'] = "Bearer #{options[:auth]}" if options[:auth]
- h['From'] = "#{options[:from]}" if options[:from]
+ h['From'] = "#{options[:from]}" if options[:from]
end
end
@@ -313,6 +331,7 @@ def generate_url!(url_pattern, args, params = {})
args, url = replace_args_from_pattern! args, url
url = URI.escape url
+ params = params.except(:headers, :auth, :from)
unless params.empty?
url += '?' + params.entries.map do |key, value|
URI::encode "#{key}=#{value}"
@@ -1,5 +1,5 @@
module Shutl
module Resource
- VERSION = '1.4.0'
+ VERSION = '1.4.1'
end
end
View
@@ -4,8 +4,8 @@ require File.expand_path('../lib/shutl/resource/version', __FILE__)
$platform ||= RUBY_PLATFORM[/java/] || 'ruby'
Gem::Specification.new do |gem|
- gem.authors = ["David Rouchy", "Volker Pacher", "Mark Burns"]
- gem.email = ["davidr@shutl.co.uk", "volker@shutl.com", "mark@shutl.com"]
+ gem.authors = ["David Rouchy", "Volker Pacher", "Mark Burns", "Cosimo Ranieri"]
+ gem.email = ["davidr@shutl.co.uk", "volker@shutl.com", "mark@shutl.com", "cosimo@shutl.com"]
gem.description = %q{Shutl Rest resource}
gem.summary = %q{Manage Shutl Rest resource. Parse/Serialize JSON}
gem.homepage = ""
@@ -0,0 +1,135 @@
+require 'spec_helper'
+
+describe Shutl::Resource::Rest do
+ let(:expected_headers) do
+ {
+ headers:
+ {
+ 'Accept' => 'application/json',
+ 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
+ 'Authorization' => 'some auth',
+ 'Content-Type' => 'application/json+consumer',
+ 'User-Agent' => "Shutl Resource Gem v#{Shutl::Resource::VERSION}",
+ 'Consumer-Access-Token' => 'ConsumerAccessToken'
+ }
+ }
+ end
+
+ let(:response_headers) do
+ { 'Accept' => 'application/json',
+ 'Content-Type' => 'application/json',
+ 'User-Agent' => "Shutl Resource Gem v#{Shutl::Resource::VERSION}" }
+ end
+
+ let(:resource) { TestRest.new(a: 'a', b: 2) }
+
+ let(:headers) {
+ {
+ authorization: "some auth",
+ content_type: "application/json+consumer",
+ consumer_access_token: "ConsumerAccessToken"
+ }
+ }
+
+ describe '#find' do
+ context "with a singular resource" do
+ let(:resource) { TestSingularResource.new }
+
+ before do
+ @request = stub_request(:get, 'http://host/test_singular_resource')
+ .with(expected_headers)
+ .to_return(:status => 200,
+ :body => '{"test_singular_resource": { "a": "a", "b": 2 }}',
+ :headers => response_headers)
+
+ end
+
+ it 'queries the endpoint' do
+ TestSingularResource.find(headers: headers)
+ @request.should have_been_requested
+ end
+ end
+ end
+
+ describe ".create" do
+
+ context "With the setting to not raise exceptions" do
+ let(:resource) { TestSingularResource.new }
+ let(:attributes) { { :a => 1 } }
+ let(:body) { { test_singular_resource: { a: 1 } }.to_json }
+ before do
+ post_headers = expected_headers[:headers].except("Accept-Encoding")
+ @request = stub_request(:post, 'http://host/test_singular_resources')
+ .with(post_headers.merge(body: body))
+ .to_return(:status => 200,
+ :headers => response_headers)
+ end
+
+ it 'queries the endpoint' do
+ TestSingularResource.create(attributes, headers: headers)
+ @request.should have_been_requested
+ end
+ end
+ end
+
+ describe "#destroy" do
+ let(:destroy_headers) { expected_headers[:headers].except("Accept-Encoding") }
+
+ before do
+ @request = stub_request(:delete, 'http://host/test_singular_resources/a')
+ .with(destroy_headers)
+ end
+
+ it 'queries the endpoint' do
+ TestSingularResource.destroy(id: 'a')
+ @request.should have_been_requested
+ end
+ end
+
+ describe "#save" do
+ let(:save_headers) { expected_headers[:headers].except("Accept-Encoding") }
+ before do
+ @request = stub_request(:put, 'http://host/test_rests/a')
+ .with(save_headers)
+ end
+
+ it 'queries the endpoint' do
+ resource.save
+ @request.should have_been_requested
+ end
+ end
+
+ describe '#update!' do
+ let(:update_headers) { expected_headers[:headers].except("Accept-Encoding") }
+ let(:attributes) { { a: 'a', b: 'b' } }
+ let(:body) { { test_rest: { a: "a", b: "b", id: "a" } }.to_json }
+
+ before do
+ @request = stub_request(:put, "http://host/test_rests/a").
+ with(:body => body,
+ :headers => update_headers).
+ to_return(:status => 200, :body => "", :headers => {})
+ end
+
+ it 'queries the endpoint' do
+ resource.update!(attributes, headers: headers)
+ @request.should have_been_requested
+ end
+ end
+
+ describe '#all' do
+ let(:body) { { test_rests: [{ a: "a", b: 2 }]}.to_json }
+
+ before do
+ @request = stub_request(:get, 'http://host/test_rests')
+ .with(expected_headers)
+ .to_return(:status => 200, body: body,:headers => headers)
+ end
+
+ it 'queries the endpoint' do
+ TestRest.all headers: headers
+ @request.should have_been_requested
+ end
+
+ end
+end
@@ -2,19 +2,16 @@
describe Shutl::Resource::Rest do
let(:headers) do
- { 'Accept' => 'application/json',
+ { 'Accept' => 'application/json',
'Content-Type' => 'application/json',
- 'User-Agent' => "Shutl Resource Gem v#{Shutl::Resource::VERSION}" }
+ 'User-Agent' => "Shutl Resource Gem v#{Shutl::Resource::VERSION}" }
end
let(:resource) { TestRest.new(a: 'a', b: 2) }
describe '#find' do
context "with a singular resource" do
let(:resource) { TestSingularResource.new }
- let(:headers_with_auth) do
- headers.merge("Authorization" => "Bearer some auth")
- end
before do
@request = stub_request(:get, 'http://host/test_singular_resource').
@@ -25,7 +22,6 @@
it 'queries the endpoint' do
TestSingularResource.find(auth: "some auth")
-
@request.should have_been_requested
end

0 comments on commit f88e25f

Please sign in to comment.