Sha256: b75d3765fdaf350cb8d10e84f74d4986870d7b914f991c88d8adebee6455c45b

Contents?: true

Size: 978 Bytes

Versions: 7

Compression:

Stored size: 978 Bytes

Contents

class TMS::Connection
  attr_accessor :auth_token, :api_root, :connection, :logger

  def get(href)
    resp = connection.get("#{href}.json")
    if resp.status != 200
      raise RecordNotFound.new("Could not find resource at #{href} (status #{resp.status})")
    else
      resp.body
    end
  end

  def initialize(opts={})
    self.auth_token = opts[:auth_token]
    self.api_root = opts[:api_root]
    self.logger = opts[:logger]
    setup_connection
  end

  def setup_connection
    self.connection = Faraday.new(:url => self.api_root) do |faraday|
      faraday.use TMS::Logger, self.logger if self.logger
      faraday.request :json
      faraday.headers['X-AUTH-TOKEN'] = auth_token
      faraday.headers[:user_agent] = "GovDelivery Ruby TMS::Client #{TMS::VERSION}"
      faraday.response :json, :content_type => /\bjson$/
      faraday.adapter :net_http
    end
  end

  def dump_headers(headers)
    headers.map { |k, v| "#{k}: #{v.inspect}" }.join("\n")
  end
end

Version data entries

7 entries across 7 versions & 2 rubygems

Version Path
govdelivery-tms-0.8.0 lib/govdelivery-tms/connection.rb
tms_client-0.6.0 lib/tms_client/connection.rb
tms_client-0.5.4 lib/tms_client/connection.rb
tms_client-0.5.3 lib/tms_client/connection.rb
tms_client-0.5.2 lib/tms_client/connection.rb
tms_client-0.5.1 lib/tms_client/connection.rb
tms_client-0.4.1 lib/tms_client/connection.rb