Class: Braintree::Query

Inherits:
Object
  • Object
show all
Includes:
LibXML
Defined in:
app/models/braintree/query.rb

Direct Known Subclasses

TransactionInfo

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(query_params) ⇒ Query

Returns a new instance of Query.



19
20
21
22
23
# File 'app/models/braintree/query.rb', line 19

def initialize(query_params)
  @query_params = query_params.merge({ 
                          'username' => BraintreeTransparentRedirectSlice.config[:username],
                          'password' => BraintreeTransparentRedirectSlice.config[:password]})
end

Class Method Details

.xml_attrs(*args) ⇒ Object



4
5
6
7
8
9
10
11
12
# File 'app/models/braintree/query.rb', line 4

def self.xml_attrs(*args)
  args.each do |xml_attr|
    class_eval <<-METHOD_DEF
      def #{xml_attr}
        @#{xml_attr} = xml_attribute('#{xml_attr}')
      end
    METHOD_DEF
  end
end

Instance Method Details

#runObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'app/models/braintree/query.rb', line 25

def run
  uri = Addressable::URI.parse(BraintreeTransparentRedirectSlice.config[:query_api_url])

  server = Net::HTTP.new(uri.host, 443)
  server.use_ssl = true
  server.read_timeout = 20
  server.verify_mode = OpenSSL::SSL::VERIFY_NONE

  resp = server.start do |http|
    req = Net::HTTP::Post.new(uri.path)
    req.set_form_data(@query_params)
    http.request(req)
  end
  case resp
  when Net::HTTPSuccess, Net::HTTPRedirection
    result = LibXML::XML::Document.string(resp.body)
  else
    resp.error!
  end
end

#xml_attribute(path) ⇒ Object



14
15
16
17
# File 'app/models/braintree/query.rb', line 14

def xml_attribute(path)
  result = @info.find("/nm_response/transaction/#{path}")
  (result.nil?||result.first.nil?) ? '' : result.first.content
end