Class: GovDelivery::TMS::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/govdelivery/tms/connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_rootObject

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_tokenObject

Returns the value of attribute auth_token.



2
3
4
# File 'lib/govdelivery/tms/connection.rb', line 2

def auth_token
  @auth_token
end

#connectionObject

Returns the value of attribute connection.



2
3
4
# File 'lib/govdelivery/tms/connection.rb', line 2

def connection
  @connection
end

#loggerObject

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_connectionObject



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