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

#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

#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
# File 'lib/facturacr/api.rb', line 21

def authenticate
  response = RestClient.post @authentication_endpoint, auth_data
  @token = JSON.parse(response)["access_token"]
  
  @token
rescue => e
  raise "AUTH ERROR"
end

#get_document(key) ⇒ Object



46
47
48
49
50
# File 'lib/facturacr/api.rb', line 46

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

#get_document_status(key) ⇒ Object



40
41
42
43
44
# File 'lib/facturacr/api.rb', line 40

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

#send_document(payload) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/facturacr/api.rb', line 31

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