Skip to content

Commit

Permalink
[WEBSITE-878] Implementation to add headers to a request
Browse files Browse the repository at this point in the history
* Headers can be added on initialization of Request object.
* Default header set.
* Added documentation both in line (for yard) and in the readme.
  • Loading branch information
katylouise committed Mar 31, 2017
1 parent a713fd5 commit 98d988f
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 6 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,19 @@ parliament.parties('123').people.current
parliament.people('123').letters('456')
```

### Setting headers

#### Setting headers 'globally'
Within the code you can set global headers using the following snippet.
```
Parliament::Request.headers = { 'Accept' => 'Test' }
# The headers should be set for all new objects
Parliament::Request.new.headers #=> { 'Accept' => 'Test' }
# You can still override the headers on an instance by instance basis
Parliament::Request.new(headers: { 'Accept' => 'Test2' }).headers #=> { 'Accept' => 'Test2' }
```
### Methods
[parliament-ruby][parliament-ruby] comes with the following common methods:

Expand Down
40 changes: 35 additions & 5 deletions lib/parliament/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ module Parliament
# @since 0.1.0
#
# @attr_reader [String] base_url the base url of our api. (expected: http://example.com - without the trailing slash).
# @attr_reader [Hash] headers the headers being sent in the request.
class Request
attr_reader :base_url
attr_reader :base_url, :headers

# Creates a new instance of Parliament::Request.
#
# An interesting note for #initialize is that setting base_url on the class, or using the environment variable
# PARLIAMENT_BASE_URL means you don't need to pass in a base_url. You can pass one anyway to override the
# environment variable or class parameter.
# environment variable or class parameter. Similarly, headers can be set by either settings the headers on the class, or passing headers in.
#
# @example Setting the base_url on the class
# Parliament::Request.base_url = 'http://example.com'
Expand All @@ -35,10 +36,27 @@ class Request
#
# Parliament::Request.new(base_url: 'http://example.com').base_url #=> 'http://example.com'
#
# @example Setting the headers on the class
# Parliament::Request.headers = { 'Accept' => 'Test' }
#
# Parliament::Request.new.headers #=> '{ 'Accept' => 'Test' }
#
# @example Setting the headers via a parameter
# Parliament::Request.headers #=> nil
#
# Parliament::Request.new(headers: '{ 'Accept' => 'Test' }).headers #=> { 'Accept' => 'Test' }
#
# @example Overriding the headers via a parameter
# Parliament::Request.headers = { 'Accept' => 'Test' }
#
# Parliament::Request.new(headers: '{ 'Accept' => 'Test2' }).headers #=> { 'Accept' => 'Test2' }
#
# @param [String] base_url the base url of our api. (expected: http://example.com - without the trailing slash).
def initialize(base_url: nil)
# @param [Hash] headers the headers being sent in the request.
def initialize(base_url: nil, headers: nil)
@endpoint_parts = []
@base_url = base_url || self.class.base_url || ENV['PARLIAMENT_BASE_URL']
@headers = headers || self.class.headers || {}
end

# Overrides ruby's method_missing to allow creation of URLs through method calls.
Expand Down Expand Up @@ -113,7 +131,7 @@ def get(params: nil)

net_response = http.start do |h|
api_request = Net::HTTP::Get.new(endpoint_uri.request_uri)
api_request.add_field('Accept', 'application/n-triples')
add_headers(api_request)

h.request api_request
end
Expand All @@ -126,8 +144,20 @@ def get(params: nil)
private

# @attr [String] base_url the base url of our api. (expected: http://example.com - without the trailing slash).
# @attr [Hash] headers the headers being sent in the request.
class << self
attr_accessor :base_url
attr_accessor :base_url, :headers
end

def default_headers
{ 'Accept' => 'application/n-triples' }
end

def add_headers(request)
headers = default_headers.merge(@headers)
headers.each do |key, value|
request.add_field(key, value)
end
end

def api_endpoint
Expand Down
2 changes: 1 addition & 1 deletion lib/parliament/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Parliament
VERSION = '0.6.2'.freeze
VERSION = '0.6.3'.freeze
end

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions spec/parliament/request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,23 @@
expect(subject.size).to eq(1)
end
end

context 'it accepts headers' do
it 'sets the header correctly when passed in' do
Parliament::Request.headers = { 'Access-Token'=>'Test-Token' }
Parliament::Request.new(base_url: 'http://localhost:3030').people.get

expect(WebMock).to have_requested(:get, 'http://localhost:3030/people').
with(:headers => {'Accept'=>['*/*', 'application/n-triples'], 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Access-Token'=>'Test-Token', 'User-Agent'=>'Ruby'}).once
end

it 'sets the default headers only when no additional headers passed' do
Parliament::Request.new(base_url: 'http://localhost:3030').people.get

expect(WebMock).to have_requested(:get, 'http://localhost:3030/people').
with(:headers => {'Accept'=>['*/*', 'application/n-triples'], 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}).once
end
end
end

describe '#assign_decorator' do
Expand Down

0 comments on commit 98d988f

Please sign in to comment.