Class: Efatura::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/efatura/client.rb

Constant Summary collapse

LOGIN_URL =
'https://www.acesso.gov.pt/jsp/loginRedirectForm.jsp?path=painelAdquirente.action&partID=EFPF'
CONSUMIDOR_URL =
'https://faturas.portaldasfinancas.gov.pt/painelAdquirente.action'
FATURAS_URL =
'https://faturas.portaldasfinancas.gov.pt/json/obterDocumentosAdquirente.action'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(from_date, to_date) ⇒ Client

Returns a new instance of Client.



10
11
12
13
# File 'lib/efatura/client.rb', line 10

def initialize(from_date, to_date)
  @from_date = from_date
  @to_date = to_date
end

Instance Attribute Details

#cookiesObject

Returns the value of attribute cookies.



4
5
6
# File 'lib/efatura/client.rb', line 4

def cookies
  @cookies
end

#from_dateObject

Returns the value of attribute from_date.



3
4
5
# File 'lib/efatura/client.rb', line 3

def from_date
  @from_date
end

#to_dateObject

Returns the value of attribute to_date.



3
4
5
# File 'lib/efatura/client.rb', line 3

def to_date
  @to_date
end

Instance Method Details

#date_format_valid?(date) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
40
41
42
43
44
# File 'lib/efatura/client.rb', line 37

def date_format_valid?(date)
  # RECEIVE A DATE INSTANCE VARIABLE AND VERIFIES THE CORRECT FORMAT
  format = '%Y-%m-%d'
  DateTime.strptime(date, format)
  true
rescue ArgumentError
  false
end

#date_same_year?(from_date, to_date) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/efatura/client.rb', line 46

def date_same_year?(from_date, to_date)
  Date.parse(from_date).year == Date.parse(to_date).year
end

#date_valid?(from_date, to_date) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
35
# File 'lib/efatura/client.rb', line 32

def date_valid?(from_date, to_date)
  # FOR A DATE TO BE VALID IT NEEDS TO PASS THE THREE CONDITIONS
  date_format_valid?(from_date) && date_format_valid?(to_date) && date_same_year?(from_date, to_date)
end

#invoicesObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/efatura/client.rb', line 15

def invoices
  
  response = RestClient::Request.execute(
    method: :get,
    url: FATURAS_URL,
    cookies: cookies,
    headers: {
      params: {
        'dataInicioFilter' => from_date,
        'dataFimFilter' => to_date,
        'ambitoAquisicaoFilter' => 'TODOS'
      }
    }
  )
  JSON.parse(response)
end