Class: WassengerClient::Client

Inherits:
OpenStruct
  • Object
show all
Defined in:
app/models/wassenger_client/client.rb

Class Method Summary collapse

Class Method Details

.base_urlObject



58
59
60
# File 'app/models/wassenger_client/client.rb', line 58

def base_url
  'https://api.wassenger.com/v1/'
end

.clientObject



10
11
12
13
14
15
# File 'app/models/wassenger_client/client.rb', line 10

def client
  http = Net::HTTP.new(url_endpoint.host, url_endpoint.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  http
end

.delete(url = base_url, id = '', params = {}, endpoint = '', query = {}) ⇒ Object



52
53
54
55
56
# File 'app/models/wassenger_client/client.rb', line 52

def delete(url = base_url, id = '', params = {}, endpoint = '', query = {})
  request = request(url, :delete, id, params, endpoint, query)
  response = client.request(request).read_body
  JSON.parse(response) if !response.nil?
end

.get(url = base_url, id = '', params = {}, endpoint = '', query = {}) ⇒ Object



40
41
42
43
44
# File 'app/models/wassenger_client/client.rb', line 40

def get(url = base_url, id = '', params = {}, endpoint = '', query = {})
  request = request(url, :get, id, params, endpoint, query)
  response = client.request(request).read_body
  JSON.parse(response) if !response.nil?
end

.post(url = base_url, id = '', params = {}, endpoint = '', query = {}) ⇒ Object



34
35
36
37
38
# File 'app/models/wassenger_client/client.rb', line 34

def post(url = base_url, id = '', params = {}, endpoint = '', query = {})
  request = request(url, :post, id, params, endpoint, query)
  response = client.request(request).read_body
  JSON.parse(response) if !response.nil?
end

.request(url = base_url, type = :get, id = '', params = {}, endpoint = '', query = {}) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/models/wassenger_client/client.rb', line 21

def request(url = base_url, type = :get, id = '', params = {}, endpoint = '', query = {})
  uri = url_endpoint(url, id, endpoint)
  uri.query = URI.encode_www_form(query)
  request = Net::HTTP::Get.new(uri) if type == :get
  request = Net::HTTP::Post.new(uri) if type == :post
  request = Net::HTTP::Patch.new(uri) if type == :patch
  request = Net::HTTP::Delete.new(uri) if type == :delete
  request['content-type'] = 'application/json'
  request['token'] = ENV['WASSENGER_TOKEN']
  request.body = "#{params.to_json}" if !params.blank?
  request
end

.update(url = base_url, id = '', params = {}, endpoint = '', query = {}) ⇒ Object



46
47
48
49
50
# File 'app/models/wassenger_client/client.rb', line 46

def update(url = base_url, id = '', params = {}, endpoint = '', query = {})
  request = request(url, :patch, id, params, endpoint, query)
  response = client.request(request).read_body
  JSON.parse(response) if !response.nil?
end

.url_endpoint(url = base_url, id = '', endpoint = '') ⇒ Object



17
18
19
# File 'app/models/wassenger_client/client.rb', line 17

def url_endpoint(url = base_url, id = '', endpoint = '')
  id.blank? ? URI(url+endpoint) : URI(url+"/"+id+endpoint)
end