class Croesus::WebRequest

Attributes

all[R]
body[R]
headers[R]
method[R]
url[R]

Public Class Methods

new(method, url, headers = {}, body = nil) click to toggle source
# File lib/croesus/web_client/web_request.rb, line 34
def initialize(method, url, headers = {}, body = nil)
  @method = method

  if (method == :get)
    if body.is_a?(Hash) && body.length > 0
      if url.include? "?"
        url += "&"
      else
        url += "?"
      end

      uri = Addressable::URI.new
      uri.query_values = body
      url += uri.query
    end
  else
     @body = body
  end

  unless url =~ URI.regexp
    raise "Invalid URL: " + url
  end

  @url = url.gsub(/\s+/, '%20')

  @headers = {
    'Date'       => utc_httpdate,
    'Request-ID' => request_id,
  }

  headers.each_pair {|key, value| @headers[key.downcase] = value }

  Croesus.last_request = {
    headers: @headers,
    method: @method,
    url: @url
  }
  begin
    Croesus.last_request[:body] = JSON.parse(@body) if body.length > 2
  rescue Exception
  end
end

Public Instance Methods

add_header(name, value) click to toggle source
# File lib/croesus/web_client/web_request.rb, line 30
def add_header(name, value)
  @headers[name] = value
end