Sha256: 4e7a1a2d6ac017a25630a11eb72d17aaf0da98bc49596829ce4dd36baf97e95a

Contents?: true

Size: 998 Bytes

Versions: 1

Compression:

Stored size: 998 Bytes

Contents

class GovDelivery::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 GovDelivery::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

1 entries across 1 versions & 1 rubygems

Version Path
govdelivery-tms-0.8.1 lib/govdelivery/tms/connection.rb