Module: Cloudpt::API::Connection::Requests

Included in:
Cloudpt::API::Connection
Defined in:
lib/cloudpt-api/connection/requests.rb

Instance Method Summary collapse

Instance Method Details

#get(endpoint, path, data = {}, headers = {}) ⇒ Object



41
42
43
44
45
46
# File 'lib/cloudpt-api/connection/requests.rb', line 41

def get(endpoint, path, data = {}, headers = {})
  query = Cloudpt::API::Util.query(data)
  request do
    token(endpoint).get "#{Cloudpt::API::Config.prefix}#{path}?#{URI.parse(URI.encode(query))}", headers
  end
end

#get_raw(endpoint, path, data = {}, headers = {}) ⇒ Object



34
35
36
37
38
39
# File 'lib/cloudpt-api/connection/requests.rb', line 34

def get_raw(endpoint, path, data = {}, headers = {})
  query = Cloudpt::API::Util.query(data)
  request(:raw => true) do
    token(endpoint).get "#{Cloudpt::API::Config.prefix}#{path}?#{URI.parse(URI.encode(query))}", headers
  end
end

#post(endpoint, path, data = {}, headers = {}) ⇒ Object



48
49
50
51
52
# File 'lib/cloudpt-api/connection/requests.rb', line 48

def post(endpoint, path, data = {}, headers = {})
  request do
    token(endpoint).post "#{Cloudpt::API::Config.prefix}#{path}", data, headers
  end
end

#put(endpoint, path, data = {}, headers = {}) ⇒ Object



54
55
56
57
58
# File 'lib/cloudpt-api/connection/requests.rb', line 54

def put(endpoint, path, data = {}, headers = {})
  request do
    token(endpoint).put "#{Cloudpt::API::Config.prefix}#{path}", data, headers
  end
end

#request(options = {}) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/cloudpt-api/connection/requests.rb', line 8

def request(options = {})
  response = yield
  raise Cloudpt::API::Error::ConnectionFailed if !response
  status = response.code.to_i
  case status
    when 401
      raise Cloudpt::API::Error::Unauthorized
    when 403
      parsed = MultiJson.decode(response.body)
      raise Cloudpt::API::Error::Forbidden.new(parsed["error"])
    when 404
      raise Cloudpt::API::Error::NotFound
    when 400, 406
      parsed = MultiJson.decode(response.body)
      raise Cloudpt::API::Error.new(parsed["error"])
    when 300..399
      raise Cloudpt::API::Error::Redirect
    when 500..599
      raise Cloudpt::API::Error
    else
      options[:raw] ? response.body : MultiJson.decode(response.body)
  end
end