Class: ModulePos::Fiscalization::JsonRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/module_pos/fiscalization/json_request.rb

Constant Summary collapse

InvalidResponse =
Class.new(StandardError)

Instance Method Summary collapse

Instance Method Details

#call(user = nil, pass = nil) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/module_pos/fiscalization/json_request.rb', line 7

def call(user = nil, pass = nil)
  @conn.basic_auth(user, pass) if user && pass
  response = yield @conn
  if response.status.to_s.match?(/2[0-9][0-9]/)
    JSON.parse!(response.body).compact unless response.body.empty?
  else
    raise ResponseError, "Status: #{response.status} Response: #{response.body}"
  end
rescue JSON::ParserError
  raise InvalidResponse
end