class Fakturownia::Connection
Attributes
client[R]
format[R]
Public Class Methods
new(client)
click to toggle source
# File lib/fakturownia/connection.rb, line 8 def initialize(client) @client = client end
Public Instance Methods
api_url()
click to toggle source
# File lib/fakturownia/connection.rb, line 75 def api_url "https://#{client.subdomain}.fakturownia.pl" end
delete(path, options = {})
click to toggle source
# File lib/fakturownia/connection.rb, line 25 def delete(path, options = {}) request(:delete, path, options) end
get(path, options = {})
click to toggle source
# File lib/fakturownia/connection.rb, line 12 def get(path, options = {}) @format = options.fetch(:format, :json) request(:get, path, options) end
headers()
click to toggle source
# File lib/fakturownia/connection.rb, line 79 def headers {accept: :json, content_type: :json, params: {api_token: client.api_token} } end
parse(response)
click to toggle source
# File lib/fakturownia/connection.rb, line 47 def parse(response) case format when :json JSON.parse(response.body) rescue JSON::ParserError && {} when :pdf response.body else raise StandardError.new("Unknown format #{format}") end end
post(path, options = {})
click to toggle source
# File lib/fakturownia/connection.rb, line 17 def post(path, options = {}) request(:post, path, options) end
process_response(response)
click to toggle source
# File lib/fakturownia/connection.rb, line 36 def process_response(response) case response.code.to_i when 200...300 body = parse(response) else raise Fakturownia::APIException.new(response.body, response.code) end response.return! body end
put(path, options = {})
click to toggle source
# File lib/fakturownia/connection.rb, line 21 def put(path, options = {}) request(:put, path, options) end
request(method, path, options = {})
click to toggle source
# File lib/fakturownia/connection.rb, line 29 def request(method, path, options = {}) options = request_parameters(method, path, options) RestClient::Request.execute(options) do |response, request| process_response(response) end end
request_parameters(method, path, options = {})
click to toggle source
# File lib/fakturownia/connection.rb, line 58 def request_parameters(method, path, options = {}) parameters = { method: method, url: "#{api_url}#{path}.#{format}", headers: headers } unless [:get, :head].include?(method) parameters = parameters.merge(payload: options.to_json) end parameters end