pickpoint_api 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 08af998b998f4ca9f2902135d967b5df4b5a31fe
4
+ data.tar.gz: 484a27051f28e58a5a6fb4a93bd8d6dd6fd1d0e1
5
+ SHA512:
6
+ metadata.gz: b18c637a31a3741b4d3de4da5c907917a343e3faa2effb070938f066b31148fc0910d0495c0ebdc1483112e53e4b42fb89a81e0794af4ca64e925cc8b7a9d774
7
+ data.tar.gz: 53c386ad19ca66bc492a5127c8c55b746d1a338254686c0b60e2fcd6812dc93a7b768f9432dd5dfd491c60c446b496b0c4c715128e9585a57851a07df9932d0c
data/.gitignore ADDED
File without changes
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.0
4
+ - 1.9.3
5
+ install: gem install rspec
6
+ script: "rspec spec/*"
7
+
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 Kinderly LTD
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,37 @@
1
+ # Pickpoint API
2
+
3
+ [![Build Status](https://travis-ci.org/kinderly/pickpoint_api.png?branch=master)](https://travis-ci.org/kinderly/pickpoint_api)
4
+
5
+ ## Description
6
+
7
+ This gem provides a basic Ruby wrapper over [Pickpoint](http://pickpoint.ru/) API.
8
+
9
+ ## Usage
10
+
11
+ ```ruby
12
+ require('pickpoint_api')
13
+
14
+ PickpointApi.session('login', 'password', test: true) do |s|
15
+ postamats = s.postamat_list
16
+
17
+ s.create_sending(@my_sending_hash)
18
+
19
+ tracking_response = s.track_sending(@invoice_id_1)
20
+
21
+ labels_response_pdf = s.make_label([@invoice_id_1, @invoice_id_2])
22
+
23
+ reestr_response_pdf = s.make_reestr([@invoice_id_1, @invoice_id_2])
24
+
25
+ end
26
+ ```
27
+
28
+ Alternatively, you can create a Session object explicitly:
29
+
30
+ ```ruby
31
+ require ('pickpoint_api')
32
+
33
+ session = PickpointApi::Session.new(test: true)
34
+ session.login('login', 'password')
35
+ tracking_response = session.track_sending(@invoice_id_1)
36
+ session.logout
37
+ ```
@@ -0,0 +1,135 @@
1
+ module PickpointApi::Constants
2
+ API_HOST = 'e-solution.pickpoint.ru'
3
+ API_PORT = 80
4
+ API_TEST_PATH = '/apitest'
5
+ API_PROD_PATH = '/api'
6
+
7
+ ACTIONS = {
8
+ login:
9
+ {
10
+ path: '/login',
11
+ method: :post
12
+ },
13
+ logout:
14
+ {
15
+ path: '/logout',
16
+ method: :post
17
+ },
18
+ create_sending:
19
+ {
20
+ path: '/createsending',
21
+ method: :post
22
+ },
23
+ create_shipment:
24
+ {
25
+ path: '/createshipment',
26
+ method: :post
27
+ },
28
+ make_return:
29
+ {
30
+ path: '/makereturn',
31
+ method: :post
32
+ },
33
+ get_return_invoice_list:
34
+ {
35
+ path: '/getreturninvoiceslist',
36
+ method: :post
37
+ },
38
+ track_sending:
39
+ {
40
+ path: '/tracksending',
41
+ method: :post
42
+ },
43
+ sending_info:
44
+ {
45
+ path: '/sendinginfo',
46
+ method: :post
47
+ },
48
+ get_delivery_cost:
49
+ {
50
+ path: '/getdeliverycost',
51
+ method: :post
52
+ },
53
+ courier:
54
+ {
55
+ path: '/courier',
56
+ method: :post
57
+ },
58
+ courier_cancel:
59
+ {
60
+ path: '/couriercancel',
61
+ method: :post
62
+ },
63
+ make_reestr:
64
+ {
65
+ path: '/makereestr',
66
+ method: :post
67
+ },
68
+ make_reestr_number:
69
+ {
70
+ path: '/makereestrnumber',
71
+ method: :post
72
+ },
73
+ get_reestr:
74
+ {
75
+ path: '/getreestr',
76
+ method: :post
77
+ },
78
+ make_label:
79
+ {
80
+ path: '/makelabel',
81
+ method: :post
82
+ },
83
+ make_zlabel:
84
+ {
85
+ path: '/makeZlabel',
86
+ method: :post
87
+ },
88
+ city_list:
89
+ {
90
+ path: '/citylist',
91
+ method: :get
92
+ },
93
+ postamat_list:
94
+ {
95
+ path: '/postamatlist',
96
+ method: :get
97
+ },
98
+ get_zone:
99
+ {
100
+ path: '/getzone',
101
+ method: :post
102
+ },
103
+ get_money_return_order:
104
+ {
105
+ path: '/getmoneyreturnorder',
106
+ method: :post
107
+ },
108
+ get_product_return_order:
109
+ {
110
+ path: '/getproductreturnorder',
111
+ method: :post
112
+ },
113
+ enclose_info:
114
+ {
115
+ path: '/encloseinfo',
116
+ method: :post
117
+ },
118
+ track_sendings:
119
+ {
120
+ path: '/tracksendings',
121
+ method: :post
122
+ },
123
+ get_states:
124
+ {
125
+ path: '/getstates',
126
+ method: :get
127
+ },
128
+ get_invoices_change_state:
129
+ {
130
+ path: '/getInvoicesChangeState',
131
+ method: :post
132
+ }
133
+ }.freeze
134
+
135
+ end
@@ -0,0 +1,5 @@
1
+ module PickpointApi::Exceptions
2
+
3
+ ApiError = Class.new(StandardError)
4
+
5
+ end
@@ -0,0 +1,179 @@
1
+ require('net/http')
2
+ require('json')
3
+
4
+ class PickpointApi::Session
5
+ attr_reader :state
6
+ attr_reader :test
7
+
8
+ def initialize(hash = {})
9
+ @state = :new
10
+ @test = hash[:test] == true
11
+ end
12
+
13
+ def login(login, password)
14
+ if @state!= :new
15
+ return nil
16
+ end
17
+
18
+ data = {'Login' => login, 'Password' => password}
19
+ response = execute_action(:login, data)
20
+ response = JSON.parse(response)
21
+
22
+ if response['ErrorMessage'].nil? && !response['SessionId'].nil?
23
+ @session_id = response['SessionId']
24
+ @state = :started
25
+ else
26
+ @state = :error
27
+ raise ::PickpointApi::Exceptions::ApiError, response['ErrorMessage']
28
+ end
29
+ end
30
+
31
+ def logout
32
+ if !@session_id.nil? && @state == :started
33
+ data = {'SessionId' => @session_id}
34
+ response = execute_action :logout, data
35
+ response = JSON.parse(response)
36
+ if response['Success']
37
+ @state = :closed
38
+ @session_id = nil
39
+ return true
40
+ else
41
+ return false
42
+ end
43
+ else
44
+ return false
45
+ end
46
+ end
47
+
48
+ def create_sending(data)
49
+ if @state == :started
50
+ data = attach_session_id(data, 'Sendings')
51
+ response = execute_action(:create_sending, data)
52
+ response = JSON.parse(response)
53
+ end
54
+ end
55
+
56
+ def track_sending(invoice_id)
57
+ if @state != :started
58
+ return nil
59
+ end
60
+
61
+ data = invoice_id
62
+ data = attach_session_id(data, 'InvoiceNumber')
63
+ response = execute_action(:track_sending, data)
64
+
65
+ if response.nil? || response.empty?
66
+ return nil
67
+ end
68
+
69
+ response = JSON.parse(response)
70
+ end
71
+
72
+ def postamat_list
73
+ if @state == :started
74
+ response = execute_action(:postamat_list)
75
+ response = JSON.parse(response)
76
+ end
77
+ end
78
+
79
+ def make_label(invoice_id)
80
+ if @state != :started
81
+ return nil
82
+ end
83
+
84
+ if invoice_id.kind_of?(Array)
85
+ data = invoice_id
86
+ elsif
87
+ data = [invoice_id]
88
+ end
89
+
90
+ data = attach_session_id(data,'Invoices')
91
+ response = execute_action(:make_label, data)
92
+
93
+ if response.start_with?("Error")
94
+ raise ::PickpointApi::Exceptions::ApiError, response
95
+ return nil
96
+ else
97
+ return response
98
+ end
99
+ end
100
+
101
+ def make_reestr(invoice_id)
102
+ if @state != :started
103
+ return nil
104
+ end
105
+
106
+ if invoice_id.kind_of?(Array)
107
+ data = invoice_id
108
+ elsif
109
+ data = [invoice_id]
110
+ end
111
+
112
+ data = attach_session_id(data,'Invoices')
113
+ response = execute_action(:make_reestr, data)
114
+
115
+ if response.start_with?("Error")
116
+ raise ::PickpointApi::Exceptions::ApiError, response
117
+ return nil
118
+ else
119
+ return response
120
+ end
121
+ end
122
+
123
+ private
124
+
125
+ def api_path
126
+ if @test
127
+ ::PickpointApi::Constants::API_TEST_PATH
128
+ else
129
+ ::PickpointApi::Constants::API_PROD_PATH
130
+ end
131
+ end
132
+
133
+ def create_request(action)
134
+ action_config = ::PickpointApi::Constants::ACTIONS[action]
135
+ if action_config.nil?
136
+ return nil
137
+ end
138
+
139
+ action_path = "#{api_path}#{action_config[:path]}"
140
+
141
+ if action_config[:method] == :post
142
+ req = ::Net::HTTP::Post.new action_path
143
+ elsif action_config[:method] == :get
144
+ req = ::Net::HTTP::Get.new action_path
145
+ end
146
+
147
+ req.content_type = 'application/json'
148
+
149
+ req
150
+ end
151
+
152
+ def attach_session_id(data, key)
153
+ {
154
+ 'SessionId' => @session_id,
155
+ key => data
156
+ }
157
+ end
158
+
159
+ def execute_action(action, data = {})
160
+ req = create_request action
161
+
162
+ if req.nil?
163
+ return nil
164
+ end
165
+
166
+ req.body = data.to_json
167
+
168
+ response = send_request(req)
169
+
170
+ response.body
171
+ end
172
+
173
+ def send_request(req)
174
+ ::Net::HTTP.start(::PickpointApi::Constants::API_HOST, ::PickpointApi::Constants::API_PORT) do |http|
175
+ http.request(req)
176
+ end
177
+ end
178
+
179
+ end
@@ -0,0 +1,21 @@
1
+ module PickpointApi
2
+
3
+ def self.session login, password, hash = {}
4
+ begin
5
+ session = Session.new hash
6
+ session.login login, password
7
+ yield session
8
+ rescue => ex
9
+ raise ::PickpointApi::Exceptions::ApiError, ex.message
10
+ ensure
11
+ if !session.nil? && session.state != :closed
12
+ session.logout
13
+ end
14
+ end
15
+ end
16
+
17
+ end
18
+
19
+ require_relative('pickpoint_api/constants.rb')
20
+ require_relative('pickpoint_api/session.rb')
21
+ require_relative('pickpoint_api/exceptions.rb')
@@ -0,0 +1,15 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "pickpoint_api"
3
+ s.version = "0.1"
4
+ s.authors = ["Kinderly LTD"]
5
+ s.email = ["nuinuhin@gmail.com"]
6
+ s.homepage = "http://github.com/kinderly/pickpoint_api"
7
+
8
+ s.summary = %q{A wrapper for Pickpoint API}
9
+ s.description = %q{This gem provides a Ruby wrapper over Pickpoint API.}
10
+ s.license = "MIT"
11
+
12
+ s.files = `git ls-files`.split("\n")
13
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
14
+ s.require_paths = ["lib"]
15
+ end
@@ -0,0 +1,15 @@
1
+ require_relative '../lib/pickpoint_api.rb'
2
+ require_relative './support/dummy_data.rb'
3
+ require_relative './support/http_mocking.rb'
4
+
5
+ include DummyData
6
+
7
+ describe 'PickpointApi' do
8
+ it 'should run session' do
9
+ HttpMocking.set_next_response(LOGIN_SUCCESSFULL)
10
+
11
+ PickpointApi.session('login', 'password', test:true) do |s|
12
+ HttpMocking.set_next_response(LOGOUT_SUCCESSFULL)
13
+ end
14
+ end
15
+ end
data/spec/session.rb ADDED
@@ -0,0 +1,111 @@
1
+ # coding: utf-8
2
+
3
+ require_relative '../lib/pickpoint_api.rb'
4
+ require_relative './support/dummy_data.rb'
5
+ require_relative './support/http_mocking.rb'
6
+
7
+ include DummyData
8
+
9
+ describe ::PickpointApi::Session do
10
+ before(:each) do
11
+ @session = ::PickpointApi::Session.new(test: true)
12
+
13
+ ::HttpMocking.set_next_response(LOGIN_SUCCESSFULL)
14
+ end
15
+
16
+ it 'can instatiate' do
17
+ expect(@session).not_to be_nil
18
+ expect(@session.test).to eq true
19
+ expect(@session.state).to eq :new
20
+ end
21
+
22
+ describe '.login' do
23
+ it 'should log-in' do
24
+ @session.login('login', 'password')
25
+ expect(@session.state).to eq :started
26
+ end
27
+
28
+ it 'should handle incorrect login' do
29
+ ::HttpMocking.set_next_response(LOGIN_FAILED)
30
+ expect { @session.login('login', 'password') }.to raise_error ::PickpointApi::Exceptions::ApiError
31
+ expect(@session.state).to eq(:error)
32
+ end
33
+ end
34
+
35
+ describe '.logout' do
36
+ it 'should log out' do
37
+ @session.login('login', 'password')
38
+ ::HttpMocking.set_next_response(LOGOUT_SUCCESSFULL)
39
+ expect(@session.logout).to eq(true)
40
+ expect(@session.state).to eq(:closed)
41
+ end
42
+ end
43
+
44
+ describe '.create_sending' do
45
+ it 'should handle succesfull request' do
46
+ @session.login('login', 'password')
47
+ ::HttpMocking.set_next_response(CREATE_SENDING_SUCCESSFULL)
48
+ res = @session.create_sending(SAMPLE_SENDING_REQUEST)
49
+ expect(res['CreatedSendings']).not_to be_empty
50
+ expect(res['RejectedSendings']).to be_empty
51
+ end
52
+ end
53
+
54
+ describe '.track_sending' do
55
+ it 'should handle succesfull request' do
56
+ @session.login('login', 'password')
57
+ ::HttpMocking.set_next_response({}.to_json)
58
+ res = @session.track_sending('21121312')
59
+ end
60
+ end
61
+
62
+ describe '.postamat_list' do
63
+ it 'should handle succesfull request' do
64
+ @session.login('login', 'password')
65
+ ::HttpMocking.set_next_response({}.to_json)
66
+ @session.postamat_list
67
+ end
68
+ end
69
+
70
+ describe '.make_label' do
71
+ it 'should handle succesfull request' do
72
+ @session.login('login', 'password')
73
+ ::HttpMocking.set_next_response('%PDF')
74
+ @session.make_label('12345')
75
+ end
76
+
77
+ it 'should handle multiple invoices' do
78
+ @session.login('login', 'password')
79
+ ::HttpMocking.set_next_response('%PDF')
80
+ @session.make_label(['123123','123213'])
81
+ end
82
+
83
+ it 'should handle api error' do
84
+ @session.login('login', 'password')
85
+ ::HttpMocking.set_next_response('Error: Случилось что-то ужасное')
86
+ expect{@session.make_label(['123123'])}.to raise_error PickpointApi::Exceptions::ApiError
87
+ end
88
+ end
89
+
90
+ describe '.make_reestr' do
91
+ it 'should handle succesfull request' do
92
+ @session.login('login', 'password')
93
+ ::HttpMocking.set_next_response('%PDF')
94
+ @session.make_reestr('12345')
95
+ end
96
+
97
+ it 'should handle multiple invoices' do
98
+ @session.login('login', 'password')
99
+ ::HttpMocking.set_next_response('%PDF')
100
+ @session.make_reestr(['123123','123213'])
101
+
102
+ end
103
+
104
+ it 'should handle api error' do
105
+ @session.login('login', 'password')
106
+ ::HttpMocking.set_next_response('Error: Случилось что-то ужасное')
107
+ expect{@session.make_reestr(['123123'])}.to raise_error PickpointApi::Exceptions::ApiError
108
+ end
109
+ end
110
+
111
+ end
@@ -0,0 +1,45 @@
1
+ # coding: utf-8
2
+
3
+ require ('json')
4
+
5
+ module DummyData
6
+ LOGIN_SUCCESSFULL = {'SessionId' => '111111111'}.to_json
7
+ LOGIN_FAILED = {'ErrorMessage' => 'Неверный логин или пароль'}.to_json
8
+ LOGOUT_SUCCESSFULL = {'Success' => true}.to_json
9
+ CREATE_SENDING_SUCCESSFULL = {
10
+ "CreatedSendings" => [
11
+ {
12
+ 'EDTN' => "14",
13
+ 'IKN' => "9990000000",
14
+ 'Barcode' => '200819647155'
15
+ }
16
+ ],
17
+ "RejectedSendings" => []
18
+ }.to_json
19
+
20
+ SAMPLE_SENDING_REQUEST = {
21
+ "EDTN" => 14,
22
+ "IKN" => "9990000000",
23
+ "Invoice" => {
24
+ "SenderCode" => 2121,
25
+ "Description" => "A description",
26
+ "RecipientName" => "John Doe",
27
+ "PostamatNumber" => "6101-004",
28
+ "MobilePhone" => "+79030000000",
29
+ "Email" => nil,
30
+ "PostageType" => "10003",
31
+ "GettingType" => "101",
32
+ "PayType" => "1",
33
+ "Sum" => 4490.0,
34
+ "SubEncloses" => [
35
+ {
36
+ "Line" => 1,
37
+ "ProductCode" => 4559802,
38
+ "GoodsCode" => "79380-555",
39
+ "Name" => "Fire extinguisher large",
40
+ "Price" => 4290.0
41
+ }]
42
+ }
43
+ }
44
+
45
+ end
@@ -0,0 +1,14 @@
1
+ require 'net/http'
2
+
3
+
4
+ module HttpMocking
5
+
6
+ def self.set_next_response(response_body, code = 200, msg='OK')
7
+ Net::HTTP.any_instance.stub(:request) do |req|
8
+ response = Net::HTTPResponse.new(1.0, code, msg)
9
+ response.stub(:body => response_body)
10
+ response
11
+ end
12
+ end
13
+
14
+ end
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pickpoint_api
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ platform: ruby
6
+ authors:
7
+ - Kinderly LTD
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-03-03 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: This gem provides a Ruby wrapper over Pickpoint API.
14
+ email:
15
+ - nuinuhin@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".gitignore"
21
+ - ".travis.yml"
22
+ - LICENSE
23
+ - README.md
24
+ - lib/pickpoint_api.rb
25
+ - lib/pickpoint_api/constants.rb
26
+ - lib/pickpoint_api/exceptions.rb
27
+ - lib/pickpoint_api/session.rb
28
+ - pickpoint_api.gemspec
29
+ - spec/pickpoint_api.rb
30
+ - spec/session.rb
31
+ - spec/support/dummy_data.rb
32
+ - spec/support/http_mocking.rb
33
+ homepage: http://github.com/kinderly/pickpoint_api
34
+ licenses:
35
+ - MIT
36
+ metadata: {}
37
+ post_install_message:
38
+ rdoc_options: []
39
+ require_paths:
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ requirements: []
52
+ rubyforge_project:
53
+ rubygems_version: 2.2.2
54
+ signing_key:
55
+ specification_version: 4
56
+ summary: A wrapper for Pickpoint API
57
+ test_files: []