mpi_client 0.0.1
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.
- data/lib/mpi_client.rb +52 -0
- data/spec/mpi_client_spec.rb +60 -0
- metadata +75 -0
data/lib/mpi_client.rb
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'network'
|
3
|
+
require 'nokogiri'
|
4
|
+
|
5
|
+
$:.unshift File.dirname(__FILE__)
|
6
|
+
|
7
|
+
require 'mpi_client/option_translator'
|
8
|
+
require 'mpi_client/mpi_response'
|
9
|
+
|
10
|
+
class MPIClient
|
11
|
+
CLIENT_METHODS = %w(create_account get_account_info update_account delete_account enrolled)
|
12
|
+
|
13
|
+
def initialize(server_url)
|
14
|
+
@connection = Network::Connection.new(server_url)
|
15
|
+
end
|
16
|
+
|
17
|
+
def method_missing(method, *args)
|
18
|
+
submit_request(method, args.first) if CLIENT_METHODS.include?(method.to_s)
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
def submit_request(request_type, options)
|
23
|
+
parse_response(@connection.post(prepare_request_data(request_type, options)))
|
24
|
+
end
|
25
|
+
|
26
|
+
def prepare_request_data(request_type, options)
|
27
|
+
builder = Nokogiri::XML::Builder.new(:encoding => 'UTF-8') do |xml|
|
28
|
+
xml.REQUEST(:type => request_type) do |xml|
|
29
|
+
xml.Transaction do |xml|
|
30
|
+
options.each { |k,v| xml.send(OptionTranslator.to_server(k), v) }
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
builder.to_xml
|
36
|
+
end
|
37
|
+
|
38
|
+
def parse_response(response_data)
|
39
|
+
doc = Nokogiri::XML(response_data)
|
40
|
+
|
41
|
+
if error = doc.xpath("//Error").first
|
42
|
+
response = {
|
43
|
+
:error_message => error.text,
|
44
|
+
:error_code => error[:code]
|
45
|
+
}
|
46
|
+
else
|
47
|
+
response = Hash[*doc.xpath("//Transaction/*").collect{|a| [OptionTranslator.to_client(a.name.to_sym), a.text] }.flatten]
|
48
|
+
end
|
49
|
+
|
50
|
+
MPIResponse.new(response)
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../lib/mpi_client.rb'
|
2
|
+
|
3
|
+
describe "MPIClient" do
|
4
|
+
before(:each) do
|
5
|
+
@client = MPIClient.new('http://127.0.0.1/3ds/')
|
6
|
+
end
|
7
|
+
|
8
|
+
describe "requests" do
|
9
|
+
it "should call submit_request and return it's result" do
|
10
|
+
response = MPIResponse.new({:account_id => 'b1adc7af83a302be94891cf17014c98a'})
|
11
|
+
options = mock
|
12
|
+
@client.should_receive(:submit_request).with(:create_account, options).and_return(response)
|
13
|
+
@client.create_account(options).should == response
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should prepare request data from request_type and options" do
|
18
|
+
options = { :id => 'one', :account_id => 'account id' }
|
19
|
+
request_type = :some_type
|
20
|
+
result = @client.send(:prepare_request_data, request_type, options)
|
21
|
+
result.should match %r{<REQUEST\ type="#{request_type}">}
|
22
|
+
|
23
|
+
options.each do |k,v|
|
24
|
+
key = OptionTranslator.to_server(k)
|
25
|
+
result.should match %r{<#{key}>#{v}</#{key}>}
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should parse input XML to MPIResponse and convert keys to client format" do
|
30
|
+
response = <<-RESPONSE
|
31
|
+
<REQUEST type=\"update_account\">
|
32
|
+
<Transaction>
|
33
|
+
<AccountId>9933fab999fd3fd0651df2c73bd6f12e</AccountId>
|
34
|
+
<Id>231</Id>
|
35
|
+
</Transaction>
|
36
|
+
</REQUEST>
|
37
|
+
RESPONSE
|
38
|
+
|
39
|
+
result = @client.send(:parse_response, response)
|
40
|
+
|
41
|
+
result.data.should == {
|
42
|
+
:account_id => '9933fab999fd3fd0651df2c73bd6f12e',
|
43
|
+
:id => '231'
|
44
|
+
}
|
45
|
+
result.should be_success
|
46
|
+
end
|
47
|
+
|
48
|
+
it "should return not success response, when response data contain Error field." do
|
49
|
+
response = <<-RESPONSE
|
50
|
+
<REQUEST type=\"update_account\">
|
51
|
+
<Error code="C2">Format XML-request is not valid</Error>
|
52
|
+
</REQUEST>
|
53
|
+
RESPONSE
|
54
|
+
|
55
|
+
result = @client.send(:parse_response, response)
|
56
|
+
result.should_not be_success
|
57
|
+
result.error_message.should == 'Format XML-request is not valid'
|
58
|
+
result.error_code.should == 'C2'
|
59
|
+
end
|
60
|
+
end
|
metadata
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mpi_client
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Dmitry Plashchynski
|
8
|
+
- Evgeniy Sugakov
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2009-10-09 00:00:00 +03:00
|
14
|
+
default_executable:
|
15
|
+
dependencies:
|
16
|
+
- !ruby/object:Gem::Dependency
|
17
|
+
name: nokogiri
|
18
|
+
type: :runtime
|
19
|
+
version_requirement:
|
20
|
+
version_requirements: !ruby/object:Gem::Requirement
|
21
|
+
requirements:
|
22
|
+
- - ">="
|
23
|
+
- !ruby/object:Gem::Version
|
24
|
+
version: 1.3.2
|
25
|
+
version:
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: alovak-network
|
28
|
+
type: :runtime
|
29
|
+
version_requirement:
|
30
|
+
version_requirements: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - ">="
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: 1.1.2
|
35
|
+
version:
|
36
|
+
description:
|
37
|
+
email: plashchynski@gmail.com
|
38
|
+
executables: []
|
39
|
+
|
40
|
+
extensions: []
|
41
|
+
|
42
|
+
extra_rdoc_files: []
|
43
|
+
|
44
|
+
files:
|
45
|
+
- lib/mpi_client.rb
|
46
|
+
has_rdoc: true
|
47
|
+
homepage: http://github.com/plashchynski/mpi_client/
|
48
|
+
licenses: []
|
49
|
+
|
50
|
+
post_install_message:
|
51
|
+
rdoc_options: []
|
52
|
+
|
53
|
+
require_paths:
|
54
|
+
- lib
|
55
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: "0"
|
60
|
+
version:
|
61
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
62
|
+
requirements:
|
63
|
+
- - ">="
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: "0"
|
66
|
+
version:
|
67
|
+
requirements: []
|
68
|
+
|
69
|
+
rubyforge_project:
|
70
|
+
rubygems_version: 1.3.5
|
71
|
+
signing_key:
|
72
|
+
specification_version: 3
|
73
|
+
summary: MPI client library
|
74
|
+
test_files:
|
75
|
+
- spec/mpi_client_spec.rb
|