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.



43
44
45
# File 'lib/webspicy/client/http_client.rb', line 43

def last_response
  @last_response
end

Instance Method Details

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



111
112
113
114
115
116
117
118
119
120
# File 'lib/webspicy/client/http_client.rb', line 111

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



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

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

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



45
46
47
48
49
50
51
52
53
54
# File 'lib/webspicy/client/http_client.rb', line 45

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

  @last_response = HTTP[headers || {}].options(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



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

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



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/webspicy/client/http_client.rb', line 67

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



100
101
102
103
104
105
106
107
108
109
# File 'lib/webspicy/client/http_client.rb', line 100

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