Class: Emojidex::Service::Transactor

Inherits:
Object
  • Object
show all
Defined in:
lib/emojidex/service/transactor.rb

Overview

API transaction utility

Constant Summary collapse

@@connection =
nil
@@retries =
10
@@settings =
{
  api: {
    host: 'www.emojidex.com',
    prefix: '/api/v1/',
    protocol: 'https'
  },
  cdn: {
    host: 'cdn.emojidex.com',
    prefix: '/emoji/',
    protocol: 'http'
  },
  closed_net: false
}

Class Method Summary collapse

Class Method Details

.api_urlObject



70
71
72
# File 'lib/emojidex/service/transactor.rb', line 70

def self.api_url()
  "#{@@settings[:api][:protocol]}://#{@@settings[:api][:host]}#{@@settings[:api][:prefix]}"
end

.cdn_urlObject



74
75
76
# File 'lib/emojidex/service/transactor.rb', line 74

def self.cdn_url()
  "#{@@settings[:cdn][:protocol]}://#{@@settings[:cdn][:host]}#{@@settings[:cdn][:prefix]}"
end

.connectObject



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/emojidex/service/transactor.rb', line 57

def self.connect
  return @@connection if @@connection
  @@connection = Faraday.new do |conn|
    conn.request :url_encoded
    conn.request :retry, max: @@retries, interval: 0.05, interval_randomness: 0.5,
                  backoff_factor: 2
    # conn.response :logger
    conn.adapter :typhoeus #Faraday.default_adapter
  end
  _kludge_windows if Gem.win_platform?
  @@connection
end

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



45
46
47
48
49
50
51
# File 'lib/emojidex/service/transactor.rb', line 45

def self.delete(endpoint, params = {})
  response = connect.delete(
    "#{api_url}#{endpoint}", params)

  _status_raiser(response)
  _datafy_json(response.body)
end

.download(file_subpath) ⇒ Object



53
54
55
# File 'lib/emojidex/service/transactor.rb', line 53

def self.download(file_subpath)
  connect.get(URI.escape("#{cdn_url}#{file_subpath.tr(' ', '_')}"))
end

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



29
30
31
32
33
34
35
# File 'lib/emojidex/service/transactor.rb', line 29

def self.get(endpoint, params = {})
  response = connect.get(
    "#{api_url}#{endpoint}", params)

  _status_raiser(response)
  _datafy_json(response.body)
end

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



37
38
39
40
41
42
43
# File 'lib/emojidex/service/transactor.rb', line 37

def self.post(endpoint, params = {})
  response = connect.post(
    "#{api_url}#{endpoint}", params)

  _status_raiser(response)
  _datafy_json(response.body)
end