Class: Webspicy::Web::HttpClient::Api

Inherits:
Object
  • Object
show all
Includes:
Client::Support
Defined in:
lib/webspicy/web/client/http_client.rb

Constant Summary

Constants included from Client::Support

Client::Support::NONE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Client::Support

#debug, #debug_response, #info_request, #json_pretty, #querystring_params, #request_body_to_s, #response_body_to_s, #status_to_s, #value_to_s

Methods included from Support::Colorize

colorize, colorize_error, colorize_highlight, colorize_section, colorize_success

Constructor Details

#initialize(scope) ⇒ Api

Returns a new instance of Api.



46
47
48
# File 'lib/webspicy/web/client/http_client.rb', line 46

def initialize(scope)
  @scope = scope
end

Instance Attribute Details

#last_responseObject (readonly)

Returns the value of attribute last_response.



49
50
51
# File 'lib/webspicy/web/client/http_client.rb', line 49

def last_response
  @last_response
end

Instance Method Details

#configObject



51
52
53
# File 'lib/webspicy/web/client/http_client.rb', line 51

def config
  @scope.config
end

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



151
152
153
154
155
156
157
158
159
160
161
# File 'lib/webspicy/web/client/http_client.rb', line 151

def delete(url, params = {}, headers = nil, body = nil)
  info_request("DELETE", url, params, headers, body)

  url = url + "?" + Rack::Utils.build_query(params) if body && !params.empty?
  http_opts = http_options(body: params.to_json)
  @last_response = HTTP[headers || {}].delete(url, http_opts)

  debug_response(@last_response)

  @last_response
end

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



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

def get(url, params = {}, headers = nil, body = nil)
  info_request("GET", url, params, headers, body)

  url = url + "?" + Rack::Utils.build_query(params) if !params.empty?
  http_opts = http_options
  @last_response = HTTP[headers || {}].get(url, http_opts)

  debug_response(@last_response)

  @last_response
end

#http_options(extra = {}) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
# File 'lib/webspicy/web/client/http_client.rb', line 163

def http_options(extra = {})
  if config.insecure
    ctx = OpenSSL::SSL::SSLContext.new
    ctx.verify_mode = OpenSSL::SSL::VERIFY_NONE
    {
      :ssl_context => ctx
    }.merge(extra)
  else
    extra
  end
end

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



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

def options(url, params = {}, headers = nil, body = nil)
  info_request("OPTIONS", url, params, headers, body)

  url = url + "?" + Rack::Utils.build_query(params) if !params.empty?
  http_opts = http_options
  @last_response = HTTP[headers || {}].options(url, http_opts)

  debug_response(@last_response)

  @last_response
end

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



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

def patch(url, params = {}, headers = nil, body = nil)
  info_request("PATCH", url, params, headers, body)

  url = url + "?" + Rack::Utils.build_query(params) if body && !params.empty?
  headers ||= {}
  headers['Content-Type'] ||= 'application/json'
  http_opts = http_options(body: params.to_json)
  @last_response = HTTP[headers].patch(url, http_opts)

  debug_response(@last_response)

  @last_response
end

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



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/webspicy/web/client/http_client.rb', line 79

def post(url, params = {}, headers = nil, body = nil)
  info_request("POST", url, params, headers, body)

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

  headers ||= {}

  case body
  when NilClass
    headers['Content-Type'] ||= 'application/json'
    http_opts = http_options(body: params.to_json)
    @last_response = HTTP[headers].post(url, http_opts)
  when FileUpload
    file = HTTP::FormData::File.new(body.path.to_s, {
      content_type: body.content_type,
      filename: body.path.basename.to_s
    })
    http_opts = http_options(form: {
      body.param_name.to_sym => file
    })
    @last_response = HTTP[headers].post(url, http_opts)
  else
    headers['Content-Type'] ||= 'application/json'
    http_opts = http_options(body: body)
    @last_response = HTTP[headers].post(url, http_opts)
  end

  debug_response(@last_response)

  @last_response
end

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



139
140
141
142
143
144
145
146
147
148
149
# File 'lib/webspicy/web/client/http_client.rb', line 139

def post_form(url, params = {}, headers = nil, body = nil)
  info_request("POST", url, params, headers, body)

  url = url + "?" + Rack::Utils.build_query(params) if body && !params.empty?
  http_opts = http_options(form: params)
  @last_response = HTTP[headers || {}].post(url, http_opts)

  debug_response(@last_response)

  @last_response
end

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



125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/webspicy/web/client/http_client.rb', line 125

def put(url, params = {}, headers = nil, body = nil)
  info_request("PUT", url, params, headers, body)

  url = url + "?" + Rack::Utils.build_query(params) if body && !params.empty?
  headers ||= {}
  headers['Content-Type'] ||= 'application/json'
  http_opts = http_options(body: params.to_json)
  @last_response = HTTP[headers].put(url, http_opts)

  debug_response(@last_response)

  @last_response
end