Class: Soracom::ApiClient

Inherits:
Object
  • Object
show all
Defined in:
lib/soracom/api_client.rb

Overview

Class to handle API requests

Instance Method Summary collapse

Constructor Details

#initialize(auth, endpoint) ⇒ ApiClient

Returns a new instance of ApiClient.



4
5
6
7
8
9
# File 'lib/soracom/api_client.rb', line 4

def initialize(auth, endpoint)
  @log = Logger.new(STDERR)
  @log.level = ENV['SORACOM_DEBUG'] ? Logger::DEBUG : Logger::WARN
  @auth = auth
  @endpoint = (endpoint.nil?) ? API_BASE_URL : endpoint
end

Instance Method Details

#delete(path: '', params: {}) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/soracom/api_client.rb', line 40

def delete(path:'', params:{})
  res = RestClient.delete @endpoint + path, accept: 'application/json', params: params, 'X-Soracom-API-Key' => @auth[:apiKey], 'X-Soracom-Token' => @auth[:token]
  @log.debug res.body
  return parse(res.body) if res.body && res.body.length > 0
  return { result: (res.code =~ /2../) ? 'success' : 'failure' }
rescue => evar
  @log.debug evar
  return { result: "failure: #{evar}" }
end

#get(path: '', params: {}) ⇒ Object



11
12
13
14
15
16
17
18
# File 'lib/soracom/api_client.rb', line 11

def get(path:'', params:{})
  res = RestClient.get @endpoint + path, accept: 'application/json', params: params, 'X-Soracom-API-Key' => @auth[:apiKey], 'X-Soracom-Token' => @auth[:token]
  return parse(res.body) if res.body && res.body.length > 0
  return { result: (res.code =~ /2../) ? 'success' : 'failure' }
rescue => evar
  @log.debug evar
  return { result: "failure: #{evar}" }
end

#post(path: '', params: {}, payload: {}) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/soracom/api_client.rb', line 20

def post(path:'', params:{}, payload:{})
  res = RestClient.post @endpoint + path, payload,
                        content_type: 'application/json', accept: 'application/json', params: params, 'X-Soracom-API-Key' => @auth[:apiKey], 'X-Soracom-Token' => @auth[:token]
  return parse(res.body) if res.body && res.body.length > 0
  return { result: (res.code =~ /2../) ? 'success' : 'failure' }
rescue => evar
  @log.debug evar
  return { result: "failure: #{evar}" }
end

#put(path: '', params: {}, payload: {}) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/soracom/api_client.rb', line 30

def put(path:'', params:{}, payload:{})
  res = RestClient.put @endpoint + path, payload,
                        content_type: 'application/json', accept: 'application/json', params: params, 'X-Soracom-API-Key' => @auth[:apiKey], 'X-Soracom-Token' => @auth[:token]
  return parse(res.body) if res.body && res.body.length > 0
  return { result: (res.code =~ /2../) ? 'success' : 'failure' }
rescue => evar
  @log.debug evar
  return { result: "failure: #{evar}" }
end