Class: NmiDirectPost::Base

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Conversion, ActiveModel::Validations
Defined in:
lib/nmi_direct_post/base.rb

Direct Known Subclasses

CustomerVault, Transaction

Constant Summary collapse

POST_URI =
"https://secure.nmi.com/api/transact.php"
GET_URI =
"https://secure.nmi.com/api/query.php"
AUTH_PARAMS =
[:username, :password]
NO_CONNECTION =
"Please set a username by calling NmiDirectPost::Base.establish_connection(ENV['NMI_USERNAME'], ENV['NMI_PASSWORD'])"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBase

Returns a new instance of Base.



31
32
33
# File 'lib/nmi_direct_post/base.rb', line 31

def initialize
  @username, @password = self.class.username, self.class.password
end

Instance Attribute Details

#responseObject (readonly)

Returns the value of attribute response.



25
26
27
# File 'lib/nmi_direct_post/base.rb', line 25

def response
  @response
end

#response_codeObject (readonly)

Returns the value of attribute response_code.



25
26
27
# File 'lib/nmi_direct_post/base.rb', line 25

def response_code
  @response_code
end

#response_textObject (readonly)

Returns the value of attribute response_text.



25
26
27
# File 'lib/nmi_direct_post/base.rb', line 25

def response_text
  @response_text
end

Class Method Details

.establish_connection(username, password) ⇒ Object



49
50
51
# File 'lib/nmi_direct_post/base.rb', line 49

def establish_connection(username, password)
  @username, @password = username, password
end

.generate_query_string(attributes, target = self) ⇒ Object



61
62
63
# File 'lib/nmi_direct_post/base.rb', line 61

def generate_query_string(attributes, target = self)
  ((attributes.reject { |attr| target.__send__(attr).blank? }).collect { |attr| "#{attr}=#{Addressable::URI.escape(target.__send__(attr).to_s)}"}).join('&')
end

.get(query) ⇒ Object



65
66
67
68
69
# File 'lib/nmi_direct_post/base.rb', line 65

def get(query)
  uri = [GET_URI, query].join('?')
  data = get_http_response(uri).body
  Hash.from_xml(data)["nm_response"]
end

.passwordObject



57
58
59
# File 'lib/nmi_direct_post/base.rb', line 57

def password
  @password || (in_base? ? raise_no_connection_error : superclass.password).tap { |_| raise_no_connection_error if _.blank? }
end

.post(query) ⇒ Object



71
72
73
74
75
# File 'lib/nmi_direct_post/base.rb', line 71

def post(query)
  uri = [POST_URI, query].join('?')
  data = get_http_response(uri)
  Addressable::URI.parse([POST_URI, data.body].join('?')).query_values
end

.usernameObject



53
54
55
# File 'lib/nmi_direct_post/base.rb', line 53

def username
  @username || (in_base? ? raise_no_connection_error : superclass.username).tap { |_| raise_no_connection_error if _.blank? }
end

Instance Method Details

#loggerObject



43
44
45
# File 'lib/nmi_direct_post/base.rb', line 43

def logger
  NmiDirectPost.logger
end

#persisted?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/nmi_direct_post/base.rb', line 35

def persisted?
  false
end

#success?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/nmi_direct_post/base.rb', line 39

def success?
  1 == self.response
end