Class: WebmaniabrNfeRuby::ApiInterface

Inherits:
Object
  • Object
show all
Defined in:
lib/webmaniabr_nfe_ruby/api_interface.rb

Direct Known Subclasses

SefazStatus, SubmitNfe, ValidateA1Cert

Instance Method Summary collapse

Constructor Details

#initialize(action) ⇒ ApiInterface

Returns a new instance of ApiInterface.



6
7
8
# File 'lib/webmaniabr_nfe_ruby/api_interface.rb', line 6

def initialize(action)
  @uri = URI(base_uri + action)
end

Instance Method Details

#process(options = {}) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/webmaniabr_nfe_ruby/api_interface.rb', line 10

def process(options = {})
  authenticate = WebmaniabrNfeRuby.configuration.authenticate

  res =  Net::HTTP.start(@uri.hostname, @uri.port, use_ssl: true) do |http|
    case options[:method]
    when 'GET'
      req = Net::HTTP::Get.new(@uri)
    when 'POST'
      req = Net::HTTP::Post.new(@uri)
    when 'PUT'
      req = Net::HTTP::Put.new(@uri)
    end

    req['X-Consumer-Key']        = authenticate.consumer_key
    req['X-Consumer-Secret']     = authenticate.consumer_secret
    req['X-Access-Token']        = authenticate.access_token
    req['X-Access-Token-Secret'] = authenticate.access_token_secret
    req['Content-Type']          = 'application/json'

    if options[:params]
      req.body = options[:params].to_json
    end

    http.request(req)
  end

  # converte o json para hash e converte as string keys para sym
  return JSON.parse(res.body).inject({}) { |memo, (k,v)| memo[k.to_sym] = v; memo }
end