class Webspicy::Web::HttpClient::Api

Attributes

last_response[R]

Public Class Methods

new(scope) click to toggle source
# File lib/webspicy/web/client/http_client.rb, line 46
def initialize(scope)
  @scope = scope
end

Public Instance Methods

config() click to toggle source
# File lib/webspicy/web/client/http_client.rb, line 51
def config
  @scope.config
end
delete(url, params = {}, headers = nil, body = nil) click to toggle source
# File lib/webspicy/web/client/http_client.rb, line 148
def delete(url, params = {}, headers = nil, body = nil)
  info_request("DELETE", url, params, headers, body)

  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) click to toggle source
# 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)

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

  debug_response(@last_response)

  @last_response
end
http_options(extra) click to toggle source
# File lib/webspicy/web/client/http_client.rb, line 159
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) click to toggle source
# 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)

  params = querystring_params(params)
  http_opts = http_options(params: params)
  @last_response = HTTP[headers || {}].options(url, http_opts)

  debug_response(@last_response)

  @last_response
end
patch(url, params = {}, headers = nil, body = nil) click to toggle source
# 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)

  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) click to toggle source
# 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) click to toggle source
# File lib/webspicy/web/client/http_client.rb, line 137
def post_form(url, params = {}, headers = nil, body = nil)
  info_request("POST", url, params, headers, body)

  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) click to toggle source
# File lib/webspicy/web/client/http_client.rb, line 124
def put(url, params = {}, headers = nil, body = nil)
  info_request("PUT", url, params, headers, body)

  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