Class: Hugs::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/hugs/client.rb

Constant Summary collapse

MIME_TYPES =
{
  :binary => "application/octet-stream",
  :json   => "application/json",
  :xml    => "application/xml",
  :none   => "text/plain"
}.freeze
CLASSES =
[
  Net::HTTP::Delete,
  Net::HTTP::Get,
  Net::HTTP::Head,
  Net::HTTP::Post,
  Net::HTTP::Put
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Client

Required options:

+host+: A String with the host to connect.

Optional:

+user+: A String containing the username for use in HTTP Basic Authentication.
+pass+: A String containing the password for use in HTTP Basic Authentication.
+port+: An Integer containing the port to connect.
+scheme+: A String containing the HTTP scheme.
+type+: A Symbol containing (:json or :xml) for automatic content-type parsing
        and encoding.
+raises+: A boolean if HTTP 4xx and 5xx status codes should be raised.


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/hugs/client.rb', line 37

def initialize options
  @user    = options[:user]
  @pass    = options[:password]
  host     = options[:host]
  raises   = options[:raise_errors]
  port     = options[:port]   || 80
  scheme   = options[:scheme] || "http"
  @type    = options[:type]   || :json

  @http   = Net::HTTP::Persistent.new
  @errors = Errors::HTTP.new :raise_errors => raises
  @uri    = URI.parse "#{scheme}://#{options[:host]}:#{port}"

  @http.debug_output = $stdout if options[:debug]
end

Instance Method Details

#clazzObject

Perform an HTTP Delete, Head, Get, Post, or Put.



56
57
58
59
60
61
62
63
64
65
# File 'lib/hugs/client.rb', line 56

CLASSES.each do |clazz|
  verb = clazz.to_s.split("::").last.downcase

  define_method verb do |*args|
    path   = args[0]
    params = args[1] || {}

    response_for clazz, path, params
  end
end