class Plangrade::HttpAdapter

Attributes

connection_options[R]
site_url[R]

Public Class Methods

log=(output) click to toggle source
# File lib/plangrade/http_adapter.rb, line 8
def self.log=(output)
  RestClient.log = output
end
new(site_url, opts={}) click to toggle source
# File lib/plangrade/http_adapter.rb, line 14
def initialize(site_url, opts={})
  unless site_url =~ /^https?/
    raise ArgumentError, "site_url must include either http or https scheme"
  end
  @site_url = site_url
  @connection_options = opts
end

Public Instance Methods

absolute_url(path='') click to toggle source
# File lib/plangrade/http_adapter.rb, line 38
def absolute_url(path='')
  "#{@site_url}#{path}"
end
connection_options=(opts) click to toggle source
# File lib/plangrade/http_adapter.rb, line 42
def connection_options=(opts)
  raise ArgumentError, 'expected Hash' unless opts.is_a?(Hash)
  @connection_options = opts
end
host() click to toggle source
# File lib/plangrade/http_adapter.rb, line 30
def host
  @host ||= parsed_url.host
end
scheme() click to toggle source
# File lib/plangrade/http_adapter.rb, line 34
def scheme
  @scheme ||= parsed_url.scheme
end
send_request(method, path, opts={}) click to toggle source
# File lib/plangrade/http_adapter.rb, line 47
def send_request(method, path, opts={})
  begin
    params  = opts.fetch(:params, {})

    req_opts = self.connection_options.merge({
      :method  => method,
      :headers => opts.fetch(:headers, {})
    })

    case method
    when :get, :delete
      query = Addressable::URI.form_encode(params)
      normalized_path = query.empty? ? path : [path, query].join("?")
      req_opts[:url]  = absolute_url(normalized_path)
    when :post, :put
      req_opts[:payload] = params
      req_opts[:url]     = absolute_url(path)
    else
      raise "Unsupported HTTP method, #{method}"
    end

    resp = RestClient::Request.execute(req_opts)
  
    result = Plangrade::ApiResponse.new(resp.headers, resp.body, resp.code)
  rescue => e
    if e.is_a?(RestClient::ExceptionWithResponse)
      e.response
    else
      raise e
    end
  end
end
site_url=(url) click to toggle source

set the url to be used for creating an http connection @param url [string]

# File lib/plangrade/http_adapter.rb, line 24
def site_url=(url)
  @site_url = url
  @host     = nil
  @scheme   = nil
end

Private Instance Methods

parsed_url() click to toggle source
# File lib/plangrade/http_adapter.rb, line 81
def parsed_url
  Addressable::URI.parse(@site_url)
end