class GHI::Client

Constants

CONTENT_TYPE
DEFAULT_HOST
HOST
METHODS
PORT
USER_AGENT

Attributes

password[R]
username[R]

Public Class Methods

new(username = nil, password = nil) click to toggle source
# File lib/ghi/client.rb, line 67
def initialize username = nil, password = nil
  @username, @password = username, password
end

Public Instance Methods

delete(path, options = {}) click to toggle source
# File lib/ghi/client.rb, line 91
def delete path, options = {}
  request :delete, path, options
end
get(path, params = {}) click to toggle source
# File lib/ghi/client.rb, line 75
def get path, params = {}, options = {}
  request :get, path, options.merge(:params => params)
end
head(path, options = {}) click to toggle source
# File lib/ghi/client.rb, line 71
def head path, options = {}
  request :head, path, options
end
patch(path, body = nil, options = {}) click to toggle source
# File lib/ghi/client.rb, line 87
def patch path, body = nil, options = {}
  request :patch, path, options.merge(:body => body)
end
post(path, body = nil, options = {}) click to toggle source
# File lib/ghi/client.rb, line 79
def post path, body = nil, options = {}
  request :post, path, options.merge(:body => body)
end
put(path, body = nil, options = {}) click to toggle source
# File lib/ghi/client.rb, line 83
def put path, body = nil, options = {}
  request :put, path, options.merge(:body => body)
end

Private Instance Methods

request(method, path, options) click to toggle source
# File lib/ghi/client.rb, line 97
def request method, path, options
  path = "/api/v3#{path}" if HOST != DEFAULT_HOST

  path = URI.escape path
  if params = options[:params] and !params.empty?
    q = params.map { |k, v| "#{CGI.escape k.to_s}=#{CGI.escape v.to_s}" }
    path += "?#{q.join '&'}"
  end

  headers = options.fetch :headers, {}
  headers.update 'Accept' => CONTENT_TYPE, 'User-Agent' => USER_AGENT
  req = METHODS[method].new path, headers
  if GHI::Authorization.token
    req['Authorization'] = "token #{GHI::Authorization.token}"
  end
  if options.key? :body
    req['Content-Type'] = CONTENT_TYPE
    req.body = options[:body] ? JSON.dump(options[:body]) : ''
  end
  req.basic_auth username, password if username && password

  proxy   = GHI.config 'https.proxy', :upcase => false
  proxy ||= GHI.config 'http.proxy',  :upcase => false
  if proxy
    proxy = URI.parse proxy
    http = Net::HTTP::Proxy(proxy.host, proxy.port).new HOST, PORT
  else
    http = Net::HTTP.new HOST, PORT
  end

  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE # FIXME 1.8.7

  GHI.v? and puts "\r===> #{method.to_s.upcase} #{path} #{req.body}"
  res = http.start { http.request req }
  GHI.v? and puts "\r<=== #{res.code}: #{res.body}"

  case res
  when Net::HTTPSuccess
    return Response.new(res)
  when Net::HTTPUnauthorized
    if password.nil?
      raise Authorization::Required, 'Authorization required'
    end
  when Net::HTTPMovedPermanently
    return Response.new(http.get(res['location']))
  end

  raise Error, res
end