Module: NCore::Client::ClassMethods

Defined in:
lib/ncore/client.rb

Instance Method Summary collapse

Instance Method Details

#request(method, url, opts = {}) ⇒ Object

opts - {, headers: {}, credentials: {}, cache: {}}

unknown keys assumed to be :params if :params is missing


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/ncore/client.rb', line 11

def request(method, url, opts={})
  opts = opts.with_indifferent_access
  request_credentials = opts.delete 'credentials'
  cache_opts = opts.delete 'cache'
  headers = opts.delete('headers') || {}
  params = opts['params'] || opts

  request_credentials ||= retrieve_credentials
  request_credentials = parse_credentials(request_credentials)

  base_url = request_credentials[:url] || retrieve_default_url
  base_url += '/' unless base_url.ends_with?('/')
  url = base_url + url

  headers = build_headers(headers, request_credentials.except(:url))

  path = URI.parse(url).path
  if [:get, :head, :delete].include? method
    qs = build_query_string params.as_json
    url += qs
    path += qs
    payload = nil
  else
    if defined? MultiJson
      payload = MultiJson.encode params.as_json
    else
      payload = params.to_json
    end
  end

  rest_opts = {
    body: payload,
    # connect_timeout: 10,
    headers: headers,
    method: method,
    path: path,
    read_timeout: 50,
    url: url,
    write_timeout: 50,
  }

  response = execute_request(rest_opts, cache_opts)
  parsed = parse_response(response)
  [parsed, request_credentials]
end