Class: GovDelivery::TMS::Connection
- Inherits:
-
Object
- Object
- GovDelivery::TMS::Connection
- Defined in:
- lib/govdelivery/tms/connection.rb
Instance Attribute Summary collapse
-
#api_root ⇒ Object
Returns the value of attribute api_root.
-
#auth_token ⇒ Object
Returns the value of attribute auth_token.
-
#connection ⇒ Object
Returns the value of attribute connection.
-
#logger ⇒ Object
Returns the value of attribute logger.
Instance Method Summary collapse
- #dump_headers(headers) ⇒ Object
- #get(href) ⇒ Object
-
#initialize(opts = {}) ⇒ Connection
constructor
A new instance of Connection.
- #setup_connection ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ Connection
Returns a new instance of Connection.
13 14 15 16 17 18 |
# File 'lib/govdelivery/tms/connection.rb', line 13 def initialize(opts = {}) self.auth_token = opts[:auth_token] self.api_root = opts[:api_root] self.logger = opts[:logger] setup_connection end |
Instance Attribute Details
#api_root ⇒ Object
Returns the value of attribute api_root.
2 3 4 |
# File 'lib/govdelivery/tms/connection.rb', line 2 def api_root @api_root end |
#auth_token ⇒ Object
Returns the value of attribute auth_token.
2 3 4 |
# File 'lib/govdelivery/tms/connection.rb', line 2 def auth_token @auth_token end |
#connection ⇒ Object
Returns the value of attribute connection.
2 3 4 |
# File 'lib/govdelivery/tms/connection.rb', line 2 def connection @connection end |
#logger ⇒ Object
Returns the value of attribute logger.
2 3 4 |
# File 'lib/govdelivery/tms/connection.rb', line 2 def logger @logger end |
Instance Method Details
#dump_headers(headers) ⇒ Object
31 32 33 |
# File 'lib/govdelivery/tms/connection.rb', line 31 def dump_headers(headers) headers.map { |k, v| "#{k}: #{v.inspect}" }.join("\n") end |
#get(href) ⇒ Object
4 5 6 7 8 9 10 11 |
# File 'lib/govdelivery/tms/connection.rb', line 4 def get(href) resp = connection.get("#{href}.json") if resp.status != 200 fail RecordNotFound.new("Could not find resource at #{href} (status #{resp.status})") else resp.body end end |
#setup_connection ⇒ Object
20 21 22 23 24 25 26 27 28 29 |
# File 'lib/govdelivery/tms/connection.rb', line 20 def setup_connection self.connection = Faraday.new(url: api_root) do |faraday| faraday.use GovDelivery::TMS::Logger, logger if logger faraday.request :json faraday.headers['X-AUTH-TOKEN'] = auth_token faraday.headers[:user_agent] = "GovDelivery Ruby GovDelivery::TMS::Client #{GovDelivery::TMS::VERSION}" faraday.response :json, content_type: /\bjson$/ faraday.adapter :net_http end end |