Class: Webspicy::HttpClient::Api

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#last_responseObject (readonly)

Returns the value of attribute last_response.



32
33
34
# File 'lib/webspicy/client/http_client.rb', line 32

def last_response
  @last_response
end

Instance Method Details

#delete(url, params = {}, headers = nil, body = nil) ⇒ Object



89
90
91
92
93
94
95
96
97
98
# File 'lib/webspicy/client/http_client.rb', line 89

def delete(url, params = {}, headers = nil, body = nil)
  Webspicy.info("DELETE #{url} -- #{params.inspect}")

  @last_response = HTTP[headers || {}].delete(url, body: params.to_json)

  Webspicy.debug("Headers: #{@last_response.headers.to_hash}")
  Webspicy.debug("Response: #{@last_response.body}")

  @last_response
end

#get(url, params = {}, headers = nil, body = nil) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/webspicy/client/http_client.rb', line 34

def get(url, params = {}, headers = nil, body = nil)
  Webspicy.info("GET #{url} -- #{params.inspect}")

  @last_response = HTTP[headers || {}].get(url, params: params)

  Webspicy.debug("Headers: #{@last_response.headers.to_hash}")
  Webspicy.debug("Response: #{@last_response.body}")

  @last_response
end

#patch(url, params = {}, headers = nil, body = nil) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/webspicy/client/http_client.rb', line 65

def patch(url, params = {}, headers = nil, body = nil)
  Webspicy.info("PATCH #{url} -- #{params.inspect}")

  headers ||= {}
  headers['Content-Type'] ||= 'application/json'
  @last_response = HTTP[headers].patch(url, body: params.to_json)

  Webspicy.debug("Headers: #{@last_response.headers.to_hash}")
  Webspicy.debug("Response: #{@last_response.body}")

  @last_response
end

#post(url, params = {}, headers = nil, body = nil) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/webspicy/client/http_client.rb', line 45

def post(url, params = {}, headers = nil, body = nil)
  Webspicy.info("POST #{url} -- #{params.inspect}")

  url = url + "?" + Rack::Utils.build_query(params) if body && !params.empty?

  headers ||= {}
  headers['Content-Type'] ||= 'application/json'

  if body
    @last_response = HTTP[headers].post(url, body: body)
  else
    @last_response = HTTP[headers].post(url, body: params.to_json)
  end

  Webspicy.debug("Headers: #{@last_response.headers.to_hash}")
  Webspicy.debug("Response: #{@last_response.body}")

  @last_response
end

#post_form(url, params = {}, headers = nil, body = nil) ⇒ Object



78
79
80
81
82
83
84
85
86
87
# File 'lib/webspicy/client/http_client.rb', line 78

def post_form(url, params = {}, headers = nil, body = nil)
  Webspicy.info("POST #{url} -- #{params.inspect}")

  @last_response = HTTP[headers || {}].post(url, form: params)

  Webspicy.debug("Headers: #{@last_response.headers.to_hash}")
  Webspicy.debug("Response: #{@last_response.body}")

  @last_response
end