mpi_client 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,15 @@
1
+ class MPIResponse
2
+ attr_reader :data
3
+
4
+ def initialize(data = {})
5
+ @data = data
6
+ end
7
+
8
+ def success?
9
+ error_code.nil? && error_message.nil?
10
+ end
11
+
12
+ def method_missing(method)
13
+ data[method]
14
+ end
15
+ end
@@ -0,0 +1,30 @@
1
+ class OptionTranslator
2
+ OPTION_MAP = {
3
+ :id => :Id,
4
+ :site_name => :Name,
5
+ :url => :URL,
6
+ :cert_subject => :IP,
7
+ :acquirer_bin => :AcquirerBIN,
8
+ :country_code => :CountryCode,
9
+ :password => :Password,
10
+ :pub_cert => :PublicCertificate,
11
+ :priv_key => :PrivateKey,
12
+ :directory_server_url => :DirectoryServerURL,
13
+ :brand => :CardType,
14
+ :response_url => :ResponseURL,
15
+ :client_url => :ClientURL,
16
+ :term_url => :TermURL,
17
+ :account_id => :AccountId,
18
+ :error => :Error,
19
+ :card_number => :CardNumber,
20
+ :status => :status
21
+ }
22
+
23
+ def self.to_client(option)
24
+ OPTION_MAP.invert[option]
25
+ end
26
+
27
+ def self.to_server(option)
28
+ OPTION_MAP[option]
29
+ end
30
+ end
data/lib/mpi_client.rb CHANGED
@@ -15,18 +15,19 @@ class MPIClient
15
15
  end
16
16
 
17
17
  def method_missing(method, *args)
18
- submit_request(method, args.first) if CLIENT_METHODS.include?(method.to_s)
18
+ submit_request(method, *args) if CLIENT_METHODS.include?(method.to_s)
19
19
  end
20
20
 
21
21
  private
22
- def submit_request(request_type, options)
23
- parse_response(@connection.post(prepare_request_data(request_type, options)))
22
+ def submit_request(request_type, *args)
23
+ parse_response(@connection.post(prepare_request_data(request_type, *args)))
24
24
  end
25
25
 
26
- def prepare_request_data(request_type, options)
26
+ def prepare_request_data(request_type, options, transaction_attrs = {})
27
+ options = options.dup
27
28
  builder = Nokogiri::XML::Builder.new(:encoding => 'UTF-8') do |xml|
28
29
  xml.REQUEST(:type => request_type) do |xml|
29
- xml.Transaction do |xml|
30
+ xml.Transaction(transaction_attrs) do |xml|
30
31
  options.each { |k,v| xml.send(OptionTranslator.to_server(k), v) }
31
32
  end
32
33
  end
@@ -49,4 +50,4 @@ class MPIClient
49
50
 
50
51
  MPIResponse.new(response)
51
52
  end
52
- end
53
+ end
@@ -0,0 +1,49 @@
1
+ require File.dirname(__FILE__) + '/../lib/mpi_client.rb'
2
+
3
+ MPI_SERVER_URL = 'http://192.168.65.11/xml'
4
+
5
+ describe "MPIClient requests" do
6
+ before(:each) do
7
+ @mpi_client = MPIClient.new(MPI_SERVER_URL)
8
+ end
9
+
10
+ it "should create account, update, get_info and delete" do
11
+ options = {
12
+ :id => 'some id',
13
+ :site_name => 'site name',
14
+ :url => 'http://siteurl/',
15
+ :cert_subject => '*',
16
+ :acquirer_bin => '3213',
17
+ :country_code => '432',
18
+ :password => 'qwerty',
19
+ :directory_server_url => 'http://www.visa.com/ds/',
20
+ :brand => '1',
21
+ :response_url => 'http://www.response.com/gk/',
22
+ :client_url => 'http://www.client.com/',
23
+ :term_url => 'http://www.term.com/',
24
+ }
25
+
26
+ account_id = @mpi_client.create_account(options).account_id
27
+
28
+ options.merge!({:account_id => account_id, :site_name => 'new_site_name'})
29
+
30
+ @mpi_client.update_account(options).account_id.should == account_id
31
+ @mpi_client.get_account_info({ :account_id => account_id }).site_name.should == options[:site_name]
32
+ @mpi_client.delete_account({ :account_id => account_id }).account_id.should == account_id
33
+ end
34
+
35
+ describe "card enrollment check" do
36
+ it "should return Y if card is enrolled" do
37
+ enrollment_response('4012001037141112').status.should == 'Y'
38
+ end
39
+
40
+ it "should return N if card isn't enrolled" do
41
+ enrollment_response('4012001038443335').status.should == 'N'
42
+ end
43
+
44
+ def enrollment_response(card_number)
45
+ @mpi_client.enrolled({:account_id=> '0'*32, :card_number=> card_number})
46
+ end
47
+ end
48
+
49
+ end
@@ -18,14 +18,20 @@ describe "MPIClient" do
18
18
  options = { :id => 'one', :account_id => 'account id' }
19
19
  request_type = :some_type
20
20
  result = @client.send(:prepare_request_data, request_type, options)
21
- result.should match %r{<REQUEST\ type="#{request_type}">}
21
+ result.should match %r{<REQUEST type="#{request_type}">}
22
22
 
23
23
  options.each do |k,v|
24
24
  key = OptionTranslator.to_server(k)
25
25
  result.should match %r{<#{key}>#{v}</#{key}>}
26
26
  end
27
27
  end
28
-
28
+
29
+ it "should prepare transaction attributes" do
30
+ id = 10
31
+ result = @client.send(:prepare_request_data, 'any', {}, {:id => id} )
32
+ result.should match %r{<Transaction id="#{id}".?>}
33
+ end
34
+
29
35
  it "should parse input XML to MPIResponse and convert keys to client format" do
30
36
  response = <<-RESPONSE
31
37
  <REQUEST type=\"update_account\">
@@ -0,0 +1,31 @@
1
+ require File.dirname(__FILE__) + '/../lib/mpi_client.rb'
2
+
3
+ describe "MPIResponse" do
4
+ it "should contain data" do
5
+ response_data = {:a => 1, :b => 2}
6
+ MPIResponse.new(response_data).data.should == response_data
7
+ end
8
+
9
+ describe "automatic accessors" do
10
+ it "should return 'data' hash value" do
11
+ response = MPIResponse.new({:status => 'Y'})
12
+ response.status.should == response.data[:status]
13
+ end
14
+
15
+ it "should return nil if there is no value in 'data' hash" do
16
+ response = MPIResponse.new()
17
+ response.status.should be_nil
18
+ end
19
+ end
20
+
21
+ describe "success?" do
22
+ it "should be true if error code and error message is nil" do
23
+ MPIResponse.new.should be_success
24
+ end
25
+
26
+ it "should be false if error code or error message not nil" do
27
+ response = MPIResponse.new(:error_code => "C3", :error_message => 'Somthing went wrong.')
28
+ response.should_not be_success
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,25 @@
1
+ require File.dirname(__FILE__) + '/../lib/mpi_client.rb'
2
+
3
+ describe "OptionTranslator" do
4
+ it "should translate options" do
5
+ { :id => :Id,
6
+ :site_name => :Name,
7
+ :url => :URL,
8
+ :cert_subject => :IP,
9
+ :acquirer_bin => :AcquirerBIN,
10
+ :country_code => :CountryCode,
11
+ :password => :Password,
12
+ :pub_cert => :PublicCertificate,
13
+ :priv_key => :PrivateKey,
14
+ :directory_server_url => :DirectoryServerURL,
15
+ :brand => :CardType,
16
+ :response_url => :ResponseURL,
17
+ :client_url => :ClientURL,
18
+ :term_url => :TermURL,
19
+ :account_id => :AccountId
20
+ }.each do |client_option, server_option|
21
+ OptionTranslator.to_client(server_option).should == client_option
22
+ OptionTranslator.to_server(client_option).should == server_option
23
+ end
24
+ end
25
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mpi_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dmitry Plashchynski
@@ -42,7 +42,13 @@ extensions: []
42
42
  extra_rdoc_files: []
43
43
 
44
44
  files:
45
+ - lib/mpi_client/mpi_response.rb
46
+ - lib/mpi_client/option_translator.rb
45
47
  - lib/mpi_client.rb
48
+ - remote_spec/mpi_client_spec.rb
49
+ - spec/mpi_client_spec.rb
50
+ - spec/mpi_response_spec.rb
51
+ - spec/option_translator_spec.rb
46
52
  has_rdoc: true
47
53
  homepage: http://github.com/plashchynski/mpi_client/
48
54
  licenses: []
@@ -67,9 +73,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
67
73
  requirements: []
68
74
 
69
75
  rubyforge_project:
70
- rubygems_version: 1.3.5
76
+ rubygems_version: 1.3.4
71
77
  signing_key:
72
78
  specification_version: 3
73
79
  summary: MPI client library
74
- test_files:
75
- - spec/mpi_client_spec.rb
80
+ test_files: []
81
+