Class: NinjaVan::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/ninja_van/request.rb

Class Method Summary collapse

Class Method Details

.delete(endpoint, params = {}) ⇒ Object



38
39
40
41
42
43
44
45
46
47
# File 'lib/ninja_van/request.rb', line 38

def self.delete(endpoint, params={})
  full_endpoint = "#{NinjaVan.setup.require!(:domain)}#{endpoint}"
  response = HTTP.headers(content_type: 'application/json', accept: 'application/json')
    .auth("Bearer #{NinjaVan.setup.get_token_from_cache}")
    .delete(full_endpoint, params: params)
  raise NinjaVan::ForbiddenError.new(full_endpoint) if response.code == 403
  raise NinjaVan::NotFoundError.new(response.parse) if response.code == 404
  raise NinjaVan::ServerError.new(full_endpoint) if response.code == 500
  response.parse
end

.get(endpoint, params = {}) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/ninja_van/request.rb', line 27

def self.get(endpoint, params={})
  full_endpoint = "#{NinjaVan.setup.require!(:domain)}#{endpoint}"
  response = HTTP.headers(content_type: 'application/json', accept: 'application/json')
    .auth("Bearer #{NinjaVan.setup.get_token_from_cache}")
    .get(full_endpoint, params: params)
  raise NinjaVan::ForbiddenError.new(full_endpoint) if response.code == 403
  raise NinjaVan::NotFoundError.new(response.parse) if response.code == 404
  raise NinjaVan::ServerError.new(full_endpoint) if response.code == 500
  response.parse
end

.post(endpoint, params = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
# File 'lib/ninja_van/request.rb', line 6

def self.post(endpoint, params={})
  full_endpoint = "#{NinjaVan.setup.require!(:domain)}#{endpoint}"
  response = HTTP.headers(content_type: 'application/json', accept: 'application/json')
    .auth("Bearer #{NinjaVan.setup.get_token_from_cache}")
    .post(full_endpoint, json: params)
  raise NinjaVan::ForbiddenError.new(full_endpoint) if response.code == 403
  raise NinjaVan::ServerError.new(full_endpoint) if response.code == 500
  raise NinjaVan::RequestError.new(response.parse) if response.parse.is_a?(Hash) && !response.parse['error'].nil?
  response.parse
end

.post_without_token(endpoint, params = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/ninja_van/request.rb', line 17

def self.post_without_token(endpoint, params={})
  full_endpoint = "#{NinjaVan.setup.require!(:domain)}#{endpoint}"
  response = HTTP.headers(content_type: 'application/json', accept: 'application/json')
    .post(full_endpoint, json: params)
  raise NinjaVan::ForbiddenError.new(full_endpoint) if response.code == 403
  raise NinjaVan::ServerError.new(full_endpoint) if response.code == 500
  raise NinjaVan::RequestError.new(response.parse) unless response.parse['error'].nil?
  response.parse
end