class HttpCaller::Curb

Public Instance Methods

call(opts) click to toggle source
# File lib/http_caller/curb.rb, line 11
def call opts
  accept = HttpCaller::APPLICATION_TYPES[opts.fetch(:accept, :json)]
  content_type = HttpCaller::APPLICATION_TYPES[opts.fetch(:content_type, :json)]

  begin
    case opts[:method]
    when :post
      http = Curl.post(opts[:uri].to_s, opts[:payload]) do |http|
        http.headers['Content-Type'] = content_type
        http.headers['Accept'] = accept
      end
      response(http)
    when :get
      http = Curl.get(opts[:uri].to_s) do |http|
        http.headers['Accept'] = accept
      end
      response(http)
    when :put
      http = Curl.put(opts[:uri].to_s, opts[:payload]) do |http|
        http.headers['Content-Type'] = content_type
        http.headers['Accept'] = accept
      end
      response(http)
    else
      raise ArgumentError.new("Unknown call method: #{opts[:method]}")
    end
  rescue Curl::Err::ConnectionFailedError
    raise Errno::ECONNREFUSED
  rescue Curl::Err::TimeoutError
    raise Errno::ETIMEDOUT
  end
end
response(http) click to toggle source
# File lib/http_caller/curb.rb, line 7
def response http
  Response.new(http.response_code, http.body_str)
end