Class: CentrumFaktur::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/centrum_faktur/connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Connection

Returns a new instance of Connection.



11
12
13
# File 'lib/centrum_faktur/connection.rb', line 11

def initialize(client)
  @client = client
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



9
10
11
# File 'lib/centrum_faktur/connection.rb', line 9

def client
  @client
end

#pathObject (readonly)

Returns the value of attribute path.



9
10
11
# File 'lib/centrum_faktur/connection.rb', line 9

def path
  @path
end

#responseObject (readonly)

Returns the value of attribute response.



9
10
11
# File 'lib/centrum_faktur/connection.rb', line 9

def response
  @response
end

Instance Method Details

#delete(to) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/centrum_faktur/connection.rb', line 57

def delete(to)
  @path = URI.parse(to).to_s
  request = Net::HTTP::Delete.new(@path, headers)
  request.basic_auth(client., client.password)
  @response = http.request(request)
  self
end

#formatObject



25
26
27
# File 'lib/centrum_faktur/connection.rb', line 25

def format
  @format ||= :json
end

#get(to, params = {}) ⇒ Object

TODO: unify



30
31
32
33
34
35
36
37
# File 'lib/centrum_faktur/connection.rb', line 30

def get(to, params = {})
  @format = params.fetch(:format, :json)
  @path = CentrumFaktur::Utils.path_with_params(to, params)
  request = Net::HTTP::Get.new(@path, headers)
  request.basic_auth(client., client.password)
  @response = http.request(request)
  self
end

#handle_responseObject



78
79
80
81
82
83
84
85
86
87
# File 'lib/centrum_faktur/connection.rb', line 78

def handle_response
  case response.code.to_i
  when 200...300
    parse_response
  when 300...600
    raise CentrumFaktur::ResponseError.new(response)
  else
    raise StandardError.new("Unknown response code")
  end
end

#headersObject



89
90
91
# File 'lib/centrum_faktur/connection.rb', line 89

def headers
  {"Content-Type" => "application/json", "User-Agent" => "ruby-gem-v#{CentrumFaktur::VERSION}"}
end

#httpObject



15
16
17
18
19
# File 'lib/centrum_faktur/connection.rb', line 15

def http
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  http
end

#parse_responseObject



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/centrum_faktur/connection.rb', line 65

def parse_response
  case format.to_sym
  when :json
    response.body ? MultiJson.load(response.body) : nil
  when :yaml
    response.body ? YAML.load(response.body) : nil
  when :xml, :pickle, :pdf
    response.body
  else
    raise StandardError.new("Unknown format: #{@format}")
  end
end

#post(to, params = {}) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/centrum_faktur/connection.rb', line 39

def post(to, params = {})
  @path = URI.parse(to).to_s
  request = Net::HTTP::Post.new(@path, headers)
  request.basic_auth(client., client.password)
  request.body = MultiJson.dump(CentrumFaktur::Utils.normalize_params(params))
  @response = http.request(request)
  self
end

#put(to, params = {}) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/centrum_faktur/connection.rb', line 48

def put(to, params = {})
  @path = URI.parse(to).to_s
  request = Net::HTTP::Put.new(@path, headers)
  request.basic_auth(client., client.password)
  request.body = MultiJson.encode(CentrumFaktur::Utils.normalize_params(params))
  @response = http.request(request)
  self
end

#uriObject



21
22
23
# File 'lib/centrum_faktur/connection.rb', line 21

def uri
  @uri ||= URI.parse("https://#{client.subdomain}.centrumfaktur.pl")
end