Class: MagicAdmin::Http::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/magic-admin/http/request.rb

Overview

Http Request and its methods are accessible on the Magic instance by the http_client.http_request attribute. It provides methods to interact with the http_request.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.request(method, url, options) ⇒ Object

Description:

Method configure request object and provides request object
based on method argument.

Arguments:

method: http method
url: get request url
options: a hash contains params and headers for request

Returns:

A request object.


24
25
26
27
28
29
30
31
# File 'lib/magic-admin/http/request.rb', line 24

def request(method, url, options)
  case method
  when :get, "get" then new.get(url, options)
  when :post, "post" then new.post(url, options)
  else
    raise APIError.new("Request method not supported.", { http_method: method })
  end
end

Instance Method Details

#get(url, options) ⇒ Object

Description:

Method configure request object and provides you get request object.

Arguments:

url: get request url
options: a hash contains params and headers for request

Returns:

A get request object.


43
44
45
46
47
48
49
# File 'lib/magic-admin/http/request.rb', line 43

def get(url, options)
  headers = options[:headers] || {}
  params = options[:params] || {}
  url = url_with_params(url, params)
  req = Net::HTTP::Get.new(url)
  request_with_headers(req, headers)
end

#post(url, options) ⇒ Object

Description:

Method configure request object and provides you post request object.

Arguments:

url: post request url
options: a hash contains params and headers for request

Returns:

A post request object.


60
61
62
63
64
65
66
# File 'lib/magic-admin/http/request.rb', line 60

def post(url, options)
  headers = options[:headers] || {}
  params = options[:params] || {}
  req = Net::HTTP::Post.new(url)
  req = request_with_headers(req, headers)
  request_with_params(req, params)
end