parliament-ruby 0.6.2 → 0.6.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1b45e08c7fa2e28c6b5f0c7b9d754cb7cc51449a
4
- data.tar.gz: 3e76b4cb267bbd1c5a8bea9e2488dc96e93bd513
3
+ metadata.gz: 581a69e9850b75eef39d684fd40f5347ebcf8bb1
4
+ data.tar.gz: f28f46585c4dc4cfd9f14735918804e184367d4f
5
5
  SHA512:
6
- metadata.gz: 7c4f920226cdb58e1f9931fe1768d119762268f89238808491e2c2cfc014ac776e6477c21062a5b44d6beed69b384cd4cd985c8da57f6c8faf338bb592253ca2
7
- data.tar.gz: 8cfac53fe11fe6306a5dbe1750d2f89ddd535e1d7b01b644fe600ece78b5adc800335bb3c712f5c7687ae89e88306171b9f265355a596c50c940df3753065ac5
6
+ metadata.gz: 5e1f64b23a425ee4e1b9c4a816eeb1f583a7c84a382010b0da35c6ef2dfcb5fc5ea0fc5a4bb23764b8b50650edcb5985b718e918bca9b3192137543930d9217c
7
+ data.tar.gz: '08882fbca6ac37527cd83420cb0a6d1a8bbf120db74e73ebdc8114548b40110d988948fdeaf37c97e803f5d791adf15d81d0cf8a7813080462cfe6b9d9c35cc8'
data/README.md CHANGED
@@ -98,7 +98,19 @@ parliament.parties('123').people.current
98
98
  parliament.people('123').letters('456')
99
99
  ```
100
100
 
101
+ ### Setting headers
101
102
 
103
+ #### Setting headers 'globally'
104
+ Within the code you can set global headers using the following snippet.
105
+ ```
106
+ Parliament::Request.headers = { 'Accept' => 'Test' }
107
+
108
+ # The headers should be set for all new objects
109
+ Parliament::Request.new.headers #=> { 'Accept' => 'Test' }
110
+
111
+ # You can still override the headers on an instance by instance basis
112
+ Parliament::Request.new(headers: { 'Accept' => 'Test2' }).headers #=> { 'Accept' => 'Test2' }
113
+ ```
102
114
  ### Methods
103
115
  [parliament-ruby][parliament-ruby] comes with the following common methods:
104
116
 
@@ -5,14 +5,15 @@ module Parliament
5
5
  # @since 0.1.0
6
6
  #
7
7
  # @attr_reader [String] base_url the base url of our api. (expected: http://example.com - without the trailing slash).
8
+ # @attr_reader [Hash] headers the headers being sent in the request.
8
9
  class Request
9
- attr_reader :base_url
10
+ attr_reader :base_url, :headers
10
11
 
11
12
  # Creates a new instance of Parliament::Request.
12
13
  #
13
14
  # An interesting note for #initialize is that setting base_url on the class, or using the environment variable
14
15
  # PARLIAMENT_BASE_URL means you don't need to pass in a base_url. You can pass one anyway to override the
15
- # environment variable or class parameter.
16
+ # environment variable or class parameter. Similarly, headers can be set by either settings the headers on the class, or passing headers in.
16
17
  #
17
18
  # @example Setting the base_url on the class
18
19
  # Parliament::Request.base_url = 'http://example.com'
@@ -35,10 +36,27 @@ module Parliament
35
36
  #
36
37
  # Parliament::Request.new(base_url: 'http://example.com').base_url #=> 'http://example.com'
37
38
  #
39
+ # @example Setting the headers on the class
40
+ # Parliament::Request.headers = { 'Accept' => 'Test' }
41
+ #
42
+ # Parliament::Request.new.headers #=> '{ 'Accept' => 'Test' }
43
+ #
44
+ # @example Setting the headers via a parameter
45
+ # Parliament::Request.headers #=> nil
46
+ #
47
+ # Parliament::Request.new(headers: '{ 'Accept' => 'Test' }).headers #=> { 'Accept' => 'Test' }
48
+ #
49
+ # @example Overriding the headers via a parameter
50
+ # Parliament::Request.headers = { 'Accept' => 'Test' }
51
+ #
52
+ # Parliament::Request.new(headers: '{ 'Accept' => 'Test2' }).headers #=> { 'Accept' => 'Test2' }
53
+ #
38
54
  # @param [String] base_url the base url of our api. (expected: http://example.com - without the trailing slash).
39
- def initialize(base_url: nil)
55
+ # @param [Hash] headers the headers being sent in the request.
56
+ def initialize(base_url: nil, headers: nil)
40
57
  @endpoint_parts = []
41
58
  @base_url = base_url || self.class.base_url || ENV['PARLIAMENT_BASE_URL']
59
+ @headers = headers || self.class.headers || {}
42
60
  end
43
61
 
44
62
  # Overrides ruby's method_missing to allow creation of URLs through method calls.
@@ -113,7 +131,7 @@ module Parliament
113
131
 
114
132
  net_response = http.start do |h|
115
133
  api_request = Net::HTTP::Get.new(endpoint_uri.request_uri)
116
- api_request.add_field('Accept', 'application/n-triples')
134
+ add_headers(api_request)
117
135
 
118
136
  h.request api_request
119
137
  end
@@ -126,8 +144,20 @@ module Parliament
126
144
  private
127
145
 
128
146
  # @attr [String] base_url the base url of our api. (expected: http://example.com - without the trailing slash).
147
+ # @attr [Hash] headers the headers being sent in the request.
129
148
  class << self
130
- attr_accessor :base_url
149
+ attr_accessor :base_url, :headers
150
+ end
151
+
152
+ def default_headers
153
+ { 'Accept' => 'application/n-triples' }
154
+ end
155
+
156
+ def add_headers(request)
157
+ headers = default_headers.merge(@headers)
158
+ headers.each do |key, value|
159
+ request.add_field(key, value)
160
+ end
131
161
  end
132
162
 
133
163
  def api_endpoint
@@ -1,3 +1,3 @@
1
1
  module Parliament
2
- VERSION = '0.6.2'.freeze
2
+ VERSION = '0.6.3'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: parliament-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.2
4
+ version: 0.6.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matt Rayner
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-03-29 00:00:00.000000000 Z
11
+ date: 2017-03-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: grom