Class: Croesus::WebRequest

Inherits:
Object show all
Extended by:
Utils::ClassMethods
Includes:
Utils
Defined in:
lib/croesus/web_client/web_request.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils::ClassMethods

callable, caller_name, camelize, class_name, classify, demodulize, pluralize, request_id, singularize, twenty_four_hours_ago, underscore, utc_httpdate, verify_options

Methods included from Utils

#callable, #caller_name, #camelize, #class_name, #classify, #command_in_path?, #demodulize, #pluralize, #request_id, #retrier, #singularize, #terminal_dimensions, #twenty_four_hours_ago, #underscore, #utc_httpdate, #verify_options

Constructor Details

#initialize(method, url, headers = {}, body = nil) ⇒ WebRequest

Returns a new instance of WebRequest.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# 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

Instance Attribute Details

#allObject (readonly)

Returns the value of attribute all.



28
29
30
# File 'lib/croesus/web_client/web_request.rb', line 28

def all
  @all
end

#bodyObject (readonly)

Returns the value of attribute body.



28
29
30
# File 'lib/croesus/web_client/web_request.rb', line 28

def body
  @body
end

#headersObject (readonly)

Returns the value of attribute headers.



28
29
30
# File 'lib/croesus/web_client/web_request.rb', line 28

def headers
  @headers
end

#methodObject (readonly)

Returns the value of attribute method.



28
29
30
# File 'lib/croesus/web_client/web_request.rb', line 28

def method
  @method
end

#urlObject (readonly)

Returns the value of attribute url.



28
29
30
# File 'lib/croesus/web_client/web_request.rb', line 28

def url
  @url
end

Instance Method Details

#add_header(name, value) ⇒ Object



30
31
32
# File 'lib/croesus/web_client/web_request.rb', line 30

def add_header(name, value)
  @headers[name] = value
end