Module: EuPago::Mixins::Client

Included in:
Client, OAuthClient
Defined in:
lib/ruby-eupago/mixins/initializer.rb

Instance Method Summary collapse

Instance Method Details

#build_base_url(append_base_url = "") ⇒ Object



13
14
15
16
17
18
19
# File 'lib/ruby-eupago/mixins/initializer.rb', line 13

def build_base_url(append_base_url = "")
  if ENV["EUPAGO_PRODUCTION"].to_s.empty?
    "https://sandbox.eupago.pt#{append_base_url}"
  else
    "https://clientes.eupago.pt#{append_base_url}"
  end
end

#get(api_url, query: {}, headers: {}) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/ruby-eupago/mixins/initializer.rb', line 21

def get(api_url, query: {}, headers: {})
  result = HTTParty.get(
    api_url,
    base_uri: @base_url,
    format: :json,
    headers: build_headers(headers),
    query: query,
  )

  parse_result(result)
end

#initialize(config = {}) ⇒ Object



6
7
8
9
10
11
# File 'lib/ruby-eupago/mixins/initializer.rb', line 6

def initialize(config = {})
  options = Hash[config.map { |(k, v)| [k.to_sym, v] }]
  @base_url = build_base_url(options.fetch(:prefix_base_url, ""))
  @include_api_key = options.fetch(:include_api_key, false)
  @include_api_key_in_body = options.fetch(:include_api_key_in_body, false)
end

#post(api_url, body: {}, headers: {}) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/ruby-eupago/mixins/initializer.rb', line 33

def post(api_url, body: {}, headers: {})
  kheader = build_headers(headers)
  body[:chave] = ENV.fetch("EUPAGO_API_KEY", "") if @include_api_key_in_body && body[:chave].nil?
  kbody = kheader["Content-Type"] == "application/json" ? body.to_json : body

  result = HTTParty.post(
    api_url,
    base_uri: @base_url,
    format: :json,
    headers: kheader,
    body: kbody,
  )

  parse_result(result)
end