Class: FE::Api
- Inherits:
-
Object
- Object
- FE::Api
- Defined in:
- lib/facturacr/api.rb,
lib/facturacr/api/document_status.rb
Defined Under Namespace
Classes: DocumentStatus
Instance Attribute Summary collapse
-
#authentication_endpoint ⇒ Object
Returns the value of attribute authentication_endpoint.
-
#check_location ⇒ Object
Returns the value of attribute check_location.
-
#client_id ⇒ Object
Returns the value of attribute client_id.
-
#document_endpoint ⇒ Object
Returns the value of attribute document_endpoint.
-
#errors ⇒ Object
Returns the value of attribute errors.
-
#password ⇒ Object
Returns the value of attribute password.
-
#refresh_token ⇒ Object
Returns the value of attribute refresh_token.
-
#username ⇒ Object
Returns the value of attribute username.
Instance Method Summary collapse
- #authenticate ⇒ Object
- #get_document(key) ⇒ Object
- #get_document_status(key) ⇒ Object
- #get_documents ⇒ Object
-
#initialize(configuration = nil) ⇒ Api
constructor
A new instance of Api.
- #logout ⇒ Object
- #send_document(payload) ⇒ Object
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_endpoint ⇒ Object
Returns the value of attribute authentication_endpoint.
10 11 12 |
# File 'lib/facturacr/api.rb', line 10 def authentication_endpoint @authentication_endpoint end |
#check_location ⇒ Object
Returns the value of attribute check_location.
10 11 12 |
# File 'lib/facturacr/api.rb', line 10 def check_location @check_location end |
#client_id ⇒ Object
Returns the value of attribute client_id.
10 11 12 |
# File 'lib/facturacr/api.rb', line 10 def client_id @client_id end |
#document_endpoint ⇒ Object
Returns the value of attribute document_endpoint.
10 11 12 |
# File 'lib/facturacr/api.rb', line 10 def document_endpoint @document_endpoint end |
#errors ⇒ Object
Returns the value of attribute errors.
10 11 12 |
# File 'lib/facturacr/api.rb', line 10 def errors @errors end |
#password ⇒ Object
Returns the value of attribute password.
10 11 12 |
# File 'lib/facturacr/api.rb', line 10 def password @password end |
#refresh_token ⇒ Object
Returns the value of attribute refresh_token.
10 11 12 |
# File 'lib/facturacr/api.rb', line 10 def refresh_token @refresh_token end |
#username ⇒ Object
Returns the value of attribute username.
10 11 12 |
# File 'lib/facturacr/api.rb', line 10 def username @username end |
Instance Method Details
#authenticate ⇒ Object
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_documents ⇒ Object
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 |
#logout ⇒ Object
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 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.} @errors[:response] = e.response if e.respond_to?(:response) return false end |