Module: Gerry::Api::Request

Included in:
Client
Defined in:
lib/gerry/api/request.rb

Overview

:nodoc:

Defined Under Namespace

Classes: RequestError

Instance Method Summary collapse

Instance Method Details

#auth_url(url) ⇒ Object



37
38
39
# File 'lib/gerry/api/request.rb', line 37

def auth_url(url)
  self.class.default_options[:basic_auth] ? "/a#{url}" : url
end

#delete(url) ⇒ Object



51
52
53
54
# File 'lib/gerry/api/request.rb', line 51

def delete(url)
  self.class.delete(auth_url(url))
  parse(response)
end

#get(url) ⇒ Object



32
33
34
35
# File 'lib/gerry/api/request.rb', line 32

def get(url)
  response = self.class.get(auth_url(url))
  parse(response)
end

#map_options(options) ⇒ String

Get the mapped options.

Parameters:

  • or (Array)
    Hash

    options the query parameters.

Returns:

  • (String)

    the mapped options.



13
14
15
16
17
18
19
# File 'lib/gerry/api/request.rb', line 13

def map_options(options)
  if options.is_a?(Array)
    options.map { |v| "#{v}" }.join('&')
  elsif options.is_a?(Hash)
    options.map { |k,v| "#{k}=#{v.join(',')}" }.join('&')
  end
end

#options(body = nil, is_json = true) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/gerry/api/request.rb', line 21

def options(body = nil, is_json = true)
  return {} unless body
  default_options = {
    headers: {
      'Content-Type' => is_json ? 'application/json' : 'text/plain'
    }
  }
  default_options[:body] = is_json ? body.to_json : body
  default_options
end

#post(url, body, is_json = true) ⇒ Object



46
47
48
49
# File 'lib/gerry/api/request.rb', line 46

def post(url, body, is_json = true)
  response = self.class.post(auth_url(url), options(body, is_json))
  parse(response)
end

#put(url, body = nil, is_json = true) ⇒ Object



41
42
43
44
# File 'lib/gerry/api/request.rb', line 41

def put(url, body = nil, is_json = true)
  response = self.class.put(auth_url(url), options(body, is_json))
  parse(response)
end