Class: FE::Api

Inherits:
Object
  • Object
show all
Defined in:
lib/facturacr/api.rb,
lib/facturacr/api/document_status.rb

Defined Under Namespace

Classes: DocumentStatus

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration = nil) ⇒ Api

Returns a new instance of Api.



12
13
14
15
16
17
18
19
# File 'lib/facturacr/api.rb', line 12

def initialize(configuration = nil)
  @authentication_endpoint = (configuration || FE.configuration).authentication_endpoint
  @document_endpoint = (configuration || FE.configuration).documents_endpoint
  @username = (configuration || FE.configuration).api_username
  @password = (configuration || FE.configuration).api_password
  @client_id = (configuration || FE.configuration).api_client_id
  @errors = {}
end

Instance Attribute Details

#authentication_endpointObject

Returns the value of attribute authentication_endpoint.



10
11
12
# File 'lib/facturacr/api.rb', line 10

def authentication_endpoint
  @authentication_endpoint
end

#check_locationObject

Returns the value of attribute check_location.



10
11
12
# File 'lib/facturacr/api.rb', line 10

def check_location
  @check_location
end

#client_idObject

Returns the value of attribute client_id.



10
11
12
# File 'lib/facturacr/api.rb', line 10

def client_id
  @client_id
end

#document_endpointObject

Returns the value of attribute document_endpoint.



10
11
12
# File 'lib/facturacr/api.rb', line 10

def document_endpoint
  @document_endpoint
end

#errorsObject

Returns the value of attribute errors.



10
11
12
# File 'lib/facturacr/api.rb', line 10

def errors
  @errors
end

#passwordObject

Returns the value of attribute password.



10
11
12
# File 'lib/facturacr/api.rb', line 10

def password
  @password
end

#refresh_tokenObject

Returns the value of attribute refresh_token.



10
11
12
# File 'lib/facturacr/api.rb', line 10

def refresh_token
  @refresh_token
end

#usernameObject

Returns the value of attribute username.



10
11
12
# File 'lib/facturacr/api.rb', line 10

def username
  @username
end

Instance Method Details

#authenticateObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/facturacr/api.rb', line 21

def authenticate
  # Backwards compantibility with configurations that still use contain the token operation in the url.
  url = @authentication_endpoint
  if !@authentication_endpoint.end_with?('token')
    url += "/token"
  end
  response = RestClient.post url, auth_data
  json = JSON.parse(response)
  @token = json["access_token"]
  @refresh_token = json["refresh_token"]
  
  @token
rescue => e
  puts "AUTH ERROR: #{e.message}".red
  raise FE::Error.new("authentication error: #{e.message}",class: self.class)
end

#get_document(key) ⇒ Object



71
72
73
74
75
# File 'lib/facturacr/api.rb', line 71

def get_document(key)
  authenticate
  response = RestClient.get "#{@document_endpoint}/comprobantes/#{key}", {:Authorization=> "bearer #{@token}", content_type: :json}
  JSON.parse(response)
end

#get_document_status(key) ⇒ Object



65
66
67
68
69
# File 'lib/facturacr/api.rb', line 65

def get_document_status(key)
  authenticate
  response = RestClient.get "#{@document_endpoint}/recepcion/#{key}", {:Authorization=> "bearer #{@token}", content_type: :json}
  return FE::Api::DocumentStatus.new(response)
end

#get_documentsObject



77
78
79
80
81
# File 'lib/facturacr/api.rb', line 77

def get_documents
  authenticate
  response = RescClient.get "#{@document_endpoint}/comprobantes", {:Authorization => "bearer #{@token}", content_type: :json }
  JSON.parse(response)
end

#logoutObject



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/facturacr/api.rb', line 38

def logout
  url = @authentication_endpoint
  if @authentication_endpoint.end_with?('token')
    url = url.gsub("token","logout")
  else
    url += "/logout"
  end
  response = RestClient.post url, logout_data
rescue => e
  puts "LOGOUT ERROR: #{e.message}".red
end

#send_document(payload) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/facturacr/api.rb', line 51

def send_document(payload)
  authenticate    
  response = RestClient.post "#{@document_endpoint}/recepcion", payload.to_json, {:Authorization=> "bearer #{@token}", content_type: :json}
  if response.code.eql?(200) || response.code.eql?(202)
    @check_location = response.headers[:location]
    puts "CheckLocation: #{@check_location}"
    return true 
  end
rescue => e
  @errors[:request] = {message: e.message}
  @errors[:response] = e.response if e.respond_to?(:response) 
  return false      
end