Class: Superfaktura::BaseApi

Inherits:
Object
  • Object
show all
Defined in:
lib/superfaktura/base_api.rb

Direct Known Subclasses

CheckConnection, CreateInvoice, Invoices

Constant Summary collapse

SUPERFAKTURA_URL =
'https://moja.superfaktura.sk'.freeze
SANDBOX_SUPERFAKTURA_URL =
'https://sandbox.superfaktura.sk'.freeze

Class Method Summary collapse

Class Method Details

.clientObject



6
7
8
9
10
11
12
13
14
# File 'lib/superfaktura/base_api.rb', line 6

def self.client
  Faraday.new(url: superfaktura_url) do |builder|
    builder.request :url_encoded
    builder.request :json
    builder.response :json

    builder.adapter Faraday.default_adapter
  end
end

.file(uri) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/superfaktura/base_api.rb', line 49

def self.file(uri)
  response = file_client.get(uri) do |request|
    request.headers['Authorization'] = "SFAPI email=#{Superfaktura.config.email}&apikey=#{Superfaktura.config.token}"
  end
  raise Superfaktura::Error, response.body['error_message'] unless response.status == 200

  response.body
end

.file_clientObject



16
17
18
19
20
21
22
23
# File 'lib/superfaktura/base_api.rb', line 16

def self.file_client
  Faraday.new(url: superfaktura_url) do |builder|
    builder.request :url_encoded
    builder.request :json

    builder.adapter Faraday.default_adapter
  end
end

.get(path, payload = nil) ⇒ Object



41
42
43
# File 'lib/superfaktura/base_api.rb', line 41

def self.get(path, payload = nil)
  request(path, 'GET', payload)
end

.post(path, payload) ⇒ Object



37
38
39
# File 'lib/superfaktura/base_api.rb', line 37

def self.post(path, payload)
  request(path, 'POST', payload)
end

.request(uri, method = 'POST', payload = nil) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/superfaktura/base_api.rb', line 25

def self.request(uri, method = 'POST', payload = nil)
  response = client.public_send(method.downcase.to_sym) do |request|
    request.headers['Authorization'] = "SFAPI email=#{Superfaktura.config.email}&apikey=#{Superfaktura.config.token}"

    request.url(uri)
    request.body = "data=#{payload.to_json}" if payload
  end
  raise Superfaktura::Error, response.body['error_message'] unless response.body['error'].zero?

  response.body
end

.superfaktura_urlObject



45
46
47
# File 'lib/superfaktura/base_api.rb', line 45

def self.superfaktura_url
  Superfaktura.config.sandbox ? SANDBOX_SUPERFAKTURA_URL : SUPERFAKTURA_URL
end