Module: Veritable::Connection

Includes:
VeritableObject
Included in:
VeritableResource
Defined in:
lib/veritable/connection.rb

Overview

Encapsulates the HTTP logic for connecting to the Veritable API

Users should not include this module.

Instance Method Summary collapse

Instance Method Details

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

Wraps the HTTP DELETE logic

Silently allows DELETE of nonexistent resources



48
49
50
51
52
53
54
55
56
# File 'lib/veritable/connection.rb', line 48

def delete(url, headers={})
  begin
    request(:delete, url, nil, headers)
  rescue VeritableError => e
    if not e.respond_to? :http_code or not e.http_code == "404 Resource Not Found"
      raise e
    end
  end
end

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

Wraps the HTTP GET logic



20
21
22
23
24
25
26
27
28
29
# File 'lib/veritable/connection.rb', line 20

def get(url, params=nil, headers={})
  if params and params.count > 0
    params.keys.to_a.each {|k|
        params.delete(k) if params[k].nil?
    }
    query_string = Util.query_params(params)
    url += "?#{query_string}"
  end
  request(:get, url, nil, headers)
end

#initialize(opts = nil, doc = nil) ⇒ Object

Initalizes a new connection



13
14
15
16
17
# File 'lib/veritable/connection.rb', line 13

def initialize(opts=nil, doc=nil)
  super(opts, doc)
  require_opts :api_key, :api_base_url
  default_opts(:ssl_verify => true, :enable_gzip => true)
end

#post(url, payload, headers = {}) ⇒ Object

Wraps the HTTP POST logic



32
33
34
35
36
# File 'lib/veritable/connection.rb', line 32

def post(url, payload, headers={})
  payload = MultiJson.encode(payload)
  headers = headers.merge({:content_type => 'application/json'})
  request(:post, url, payload, headers)
end

#put(url, payload, headers = {}) ⇒ Object

Wraps the HTTP PUT logic



39
40
41
42
43
# File 'lib/veritable/connection.rb', line 39

def put(url, payload, headers={})
  payload = MultiJson.encode(payload)
  headers = headers.merge({:content_type => 'application/json'})
  request(:put, url, payload, headers)
end

#request(verb, url, payload = nil, headers = {}) ⇒ Object

Wraps the core HTTP request logic



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/veritable/connection.rb', line 59

def request(verb, url, payload=nil, headers={})
  url = api_base_url + "/" + url

  headers = {
    :user_agent => USER_AGENT,
    :accept => :json,
    :accept_encoding => enable_gzip ? :gzip : nil
  }.merge(headers)

  opts = {
    :method => verb.to_s,
    :url => url,
    :user => api_key,
    :password => "",
    :headers => headers,
    :payload => payload,
    :verify_ssl => ssl_verify,
  }
  begin
    response = RestClient::Request.execute(opts)
  rescue RestClient::Exception => e
    begin
      r = MultiJson.decode(e.response)
      msg = r['message']
      code = r['code']
    rescue
      raise e
    end
    raise VeritableError.new("HTTP Error #{e.message} -- #{code}: #{msg}", {'http_code' => e.message, 'api_code' => code, 'api_message' => msg})
  end
  return MultiJson.decode(response)
end