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

#initialize(attributes) ⇒ Base

Returns a new instance of Base.



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

def initialize(attributes)
  @username = attributes[:username] || attributes["username"] || self.class.username
  @password = attributes[:password] || attributes["password"] || 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



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

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

.generate_query_string(attributes, target = self) ⇒ Object



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

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



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

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

.passwordObject



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

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

.post(query) ⇒ Object



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

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



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

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

Instance Method Details

#loggerObject



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

def logger
  NmiDirectPost.logger
end

#persisted?Boolean

Returns:

  • (Boolean)


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

def persisted?
  false
end

#success?Boolean

Returns:

  • (Boolean)


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

def success?
  1 == self.response
end