pec_calc_client 1.0.0 → 1.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 +4 -4
- data/Gemfile.lock +2 -1
- data/README.md +2 -2
- data/Rakefile +1 -1
- data/bin/console +4 -4
- data/lib/pec_calc_client/calc.rb +6 -5
- data/lib/pec_calc_client/calc_result.rb +1 -2
- data/lib/pec_calc_client/connector.rb +5 -1
- data/lib/pec_calc_client/delivery.rb +3 -3
- data/lib/pec_calc_client/place.rb +15 -5
- data/lib/pec_calc_client/region.rb +6 -4
- data/lib/pec_calc_client/response.rb +2 -1
- data/lib/pec_calc_client/take.rb +3 -4
- data/lib/pec_calc_client/town.rb +5 -4
- data/lib/pec_calc_client.rb +14 -5
- data/spec/pec_calc_client/calc_result_spec.rb +1 -1
- data/spec/pec_calc_client/calc_spec.rb +37 -7
- data/spec/pec_calc_client/connector_spec.rb +4 -5
- data/spec/pec_calc_client/delivery_spec.rb +11 -3
- data/spec/pec_calc_client/place_spec.rb +20 -5
- data/spec/pec_calc_client/region_spec.rb +4 -2
- data/spec/pec_calc_client/response_spec.rb +5 -5
- data/spec/pec_calc_client/take_spec.rb +3 -2
- data/spec/pec_calc_client/town_spec.rb +1 -1
- metadata +5 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f1649565b1eb9a909a3317eefd280cc6b77c436c
|
4
|
+
data.tar.gz: 9aafc336b1a3c22e01278971e3c50a28dd8c75d2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b5a19004f6e6267cc2f96f096c5b8fc16199fe27df1d82a600ffa7ff3c036071ac2708e590d2e854218c431e66d5e5de1132cec25b5d82bf7ebc691c0441fc1b
|
7
|
+
data.tar.gz: 910a1abf359bdc9b343c9c9981204f70f4d8f0c6b8175fac3cae28679fa29ee996f7f965d27f756e2e377cd141a3dbe8b7e18d68ef75a12542ec1ff3d6dc224a
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -23,14 +23,14 @@ Or install it yourself as:
|
|
23
23
|
|
24
24
|
## Usage
|
25
25
|
|
26
|
-
Get Delivery Regions:
|
26
|
+
Get Delivery Regions:
|
27
27
|
|
28
28
|
```ruby
|
29
29
|
require 'pec_calc_client'
|
30
30
|
|
31
31
|
PecCalcClient::Region.all
|
32
32
|
```
|
33
|
-
Extract Delivery Cities by region:
|
33
|
+
Extract Delivery Cities by region:
|
34
34
|
|
35
35
|
```ruby
|
36
36
|
require 'pec_calc_client'
|
data/Rakefile
CHANGED
@@ -1 +1 @@
|
|
1
|
-
require
|
1
|
+
require 'bundler/gem_tasks'
|
data/bin/console
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
|
3
|
-
require
|
4
|
-
require
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'pec/calc/client'
|
5
5
|
|
6
6
|
# You can add fixtures and/or initialization code here to make experimenting
|
7
7
|
# with your gem easier. You can also use a different console, if you like.
|
8
8
|
|
9
9
|
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
-
# require
|
10
|
+
# require 'pry'
|
11
11
|
# Pry.start
|
12
12
|
|
13
|
-
require
|
13
|
+
require 'irb'
|
14
14
|
IRB.start
|
data/lib/pec_calc_client/calc.rb
CHANGED
@@ -1,19 +1,19 @@
|
|
1
1
|
module PecCalcClient
|
2
|
+
# Calculator container
|
2
3
|
class Calc
|
3
|
-
|
4
4
|
extend RequestClass
|
5
5
|
include RequestObject
|
6
6
|
|
7
7
|
URL = 'http://pecom.ru/bitrix/components/pecom/calc/ajax.php'
|
8
8
|
|
9
|
-
PARAMS = %w
|
9
|
+
PARAMS = %w( plombir strah ashan night pal pallets )
|
10
10
|
|
11
11
|
set_readers
|
12
12
|
|
13
13
|
attr_accessor :delivery, :take, :places
|
14
14
|
|
15
15
|
def initialize(params)
|
16
|
-
|
16
|
+
init_params params
|
17
17
|
end
|
18
18
|
|
19
19
|
def calc
|
@@ -25,7 +25,9 @@ module PecCalcClient
|
|
25
25
|
out = to_h
|
26
26
|
out[:deliver] = @delivery.to_h
|
27
27
|
out[:take] = @take.to_h
|
28
|
-
out[:places] = @places.each_with_index.map
|
28
|
+
out[:places] = @places.each_with_index.map do |place, i|
|
29
|
+
[i, place.to_a]
|
30
|
+
end.to_h
|
29
31
|
out
|
30
32
|
end
|
31
33
|
|
@@ -34,6 +36,5 @@ module PecCalcClient
|
|
34
36
|
def prepare_result(result)
|
35
37
|
CalcResult.new result.to_hash
|
36
38
|
end
|
37
|
-
|
38
39
|
end
|
39
40
|
end
|
@@ -2,6 +2,7 @@ require 'net/http'
|
|
2
2
|
require 'rack/utils'
|
3
3
|
|
4
4
|
module PecCalcClient
|
5
|
+
# HTTP Connector
|
5
6
|
class Connector
|
6
7
|
def initialize(url)
|
7
8
|
@url = url.to_s
|
@@ -20,7 +21,10 @@ module PecCalcClient
|
|
20
21
|
private
|
21
22
|
|
22
23
|
def prepare_params(params)
|
23
|
-
@params_string = Rack::Utils.build_nested_query(params).gsub(
|
24
|
+
@params_string = Rack::Utils.build_nested_query(params).gsub(
|
25
|
+
/[\[\]\.\-]/,
|
26
|
+
'[' => '%5B', ']' => '%5D', '.' => '%2E', '-' => '%2D'
|
27
|
+
)
|
24
28
|
end
|
25
29
|
|
26
30
|
def url_with_params
|
@@ -1,15 +1,15 @@
|
|
1
1
|
module PecCalcClient
|
2
|
+
# Delivery
|
2
3
|
class Delivery
|
3
|
-
|
4
4
|
extend RequestClass
|
5
5
|
include RequestObject
|
6
6
|
|
7
|
-
PARAMS = %w
|
7
|
+
PARAMS = %w( town tent gidro manip speed moscow )
|
8
8
|
|
9
9
|
set_readers
|
10
10
|
|
11
11
|
def initialize(params)
|
12
|
-
|
12
|
+
init_params params
|
13
13
|
end
|
14
14
|
end
|
15
15
|
end
|
@@ -1,16 +1,26 @@
|
|
1
1
|
module PecCalcClient
|
2
|
+
# Delivery Place
|
2
3
|
class Place
|
4
|
+
PLACE_PARAMS = %w(
|
5
|
+
width
|
6
|
+
length
|
7
|
+
height
|
8
|
+
volume
|
9
|
+
weight
|
10
|
+
oversized
|
11
|
+
sturdy_packaging
|
12
|
+
)
|
3
13
|
|
4
|
-
PLACE_PARAMS
|
5
|
-
|
6
|
-
PLACE_PARAMS.each{ |param_name| attr_reader param_name }
|
14
|
+
PLACE_PARAMS.each { |param_name| attr_reader param_name }
|
7
15
|
|
8
16
|
def initialize(params)
|
9
|
-
params.each
|
17
|
+
params.each do |k, v|
|
18
|
+
instance_variable_set("@#{k}", v) if PLACE_PARAMS.include? k.to_s
|
19
|
+
end
|
10
20
|
end
|
11
21
|
|
12
22
|
def to_a
|
13
|
-
PLACE_PARAMS.map{ |k| instance_variable_get("@#{
|
23
|
+
PLACE_PARAMS.map { |k| instance_variable_get("@#{k}") }
|
14
24
|
end
|
15
25
|
end
|
16
26
|
end
|
@@ -1,17 +1,19 @@
|
|
1
1
|
module PecCalcClient
|
2
|
+
# Delivery region
|
2
3
|
class Region
|
3
|
-
|
4
4
|
URL = 'http://pecom.ru/ru/calc/towns.php'
|
5
5
|
|
6
6
|
attr_reader :name, :towns
|
7
7
|
|
8
|
-
def initialize
|
8
|
+
def initialize(name, towns)
|
9
9
|
@name = name
|
10
|
-
@towns = towns.collect{ |id,
|
10
|
+
@towns = towns.collect { |id, town_name| Town.new id, town_name }
|
11
11
|
end
|
12
12
|
|
13
13
|
def self.all
|
14
|
-
Connector.new(self::URL).request.to_hash.collect
|
14
|
+
Connector.new(self::URL).request.to_hash.collect do |name, towns|
|
15
|
+
new name, towns
|
16
|
+
end
|
15
17
|
end
|
16
18
|
end
|
17
19
|
end
|
@@ -1,10 +1,11 @@
|
|
1
1
|
require 'json'
|
2
2
|
|
3
3
|
module PecCalcClient
|
4
|
+
# Response from api server
|
4
5
|
class Response
|
5
6
|
def initialize(response)
|
6
7
|
@response = response
|
7
|
-
|
8
|
+
fail BadResponse, 'Respons Code not 200' if @response.code != '200'
|
8
9
|
end
|
9
10
|
|
10
11
|
def to_hash
|
data/lib/pec_calc_client/take.rb
CHANGED
@@ -1,16 +1,15 @@
|
|
1
1
|
module PecCalcClient
|
2
|
+
# Take from sender
|
2
3
|
class Take
|
3
|
-
|
4
4
|
extend RequestClass
|
5
5
|
include RequestObject
|
6
6
|
|
7
|
-
PARAMS = %w
|
7
|
+
PARAMS = %w( town tent gidro manip speed moscow )
|
8
8
|
|
9
9
|
set_readers
|
10
10
|
|
11
11
|
def initialize(params)
|
12
|
-
|
12
|
+
init_params params
|
13
13
|
end
|
14
|
-
|
15
14
|
end
|
16
15
|
end
|
data/lib/pec_calc_client/town.rb
CHANGED
@@ -1,15 +1,16 @@
|
|
1
1
|
module PecCalcClient
|
2
|
+
# Delivery town
|
2
3
|
class Town
|
3
|
-
|
4
4
|
attr_reader :id, :name
|
5
5
|
|
6
|
-
def initialize
|
7
|
-
@id
|
6
|
+
def initialize(id, name)
|
7
|
+
@id = id
|
8
|
+
@name = name
|
8
9
|
end
|
9
10
|
|
10
11
|
def self.all
|
11
12
|
Connector.new(URL).request.to_json.collect do |town|
|
12
|
-
|
13
|
+
new town
|
13
14
|
end
|
14
15
|
end
|
15
16
|
end
|
data/lib/pec_calc_client.rb
CHANGED
@@ -1,22 +1,31 @@
|
|
1
|
+
# Main container
|
1
2
|
module PecCalcClient
|
3
|
+
# Error of connection
|
2
4
|
class ConnectionError < Exception; end
|
5
|
+
# Exception for non 200 HTTP code
|
3
6
|
class BadResponse < Exception; end
|
4
7
|
|
8
|
+
# Methods for request class
|
5
9
|
module RequestClass
|
6
10
|
def set_readers
|
7
|
-
self::PARAMS.each{ |param_name| attr_reader param_name }
|
11
|
+
self::PARAMS.each { |param_name| attr_reader param_name }
|
8
12
|
end
|
9
13
|
end
|
10
14
|
|
15
|
+
# Methods for request instance
|
11
16
|
module RequestObject
|
12
|
-
def
|
13
|
-
params.each
|
17
|
+
def init_params(params)
|
18
|
+
params.each do |k, v|
|
19
|
+
instance_variable_set("@#{k}", v) if self.class::PARAMS.include?(k.to_s)
|
20
|
+
end
|
14
21
|
end
|
15
22
|
|
16
23
|
def to_h
|
17
|
-
self.class::PARAMS.map{ |k| [k, instance_variable_get("@#{
|
24
|
+
self.class::PARAMS.map { |k| [k, instance_variable_get("@#{k}")] }.to_h
|
18
25
|
end
|
19
26
|
end
|
20
27
|
end
|
21
28
|
|
22
|
-
Dir["#{
|
29
|
+
Dir["#{File.dirname(__FILE__)}/pec_calc_client/*.rb"].each do |file|
|
30
|
+
require file
|
31
|
+
end
|
@@ -1,13 +1,41 @@
|
|
1
1
|
describe 'Calc' do
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
2
|
+
calc_params = {
|
3
|
+
plombir: 12,
|
4
|
+
strah: 33,
|
5
|
+
ashan: 1,
|
6
|
+
night: 1,
|
7
|
+
pal: 3,
|
8
|
+
pallets: 4
|
9
|
+
}
|
10
|
+
place_params = {
|
11
|
+
width: 0.5,
|
12
|
+
length: 0.3,
|
13
|
+
height: 0.5,
|
14
|
+
volume: 0.2,
|
15
|
+
weight: 0.03,
|
16
|
+
oversized: true,
|
17
|
+
sturdy_packaging: true
|
18
|
+
}
|
19
|
+
delivery_params = {
|
20
|
+
town: 134_104,
|
21
|
+
tent: 1,
|
22
|
+
gidro: 1,
|
23
|
+
manip: 1,
|
24
|
+
speed: 0,
|
25
|
+
moscow: 0
|
26
|
+
}
|
27
|
+
take_params = {
|
28
|
+
town: 175_004,
|
29
|
+
tent: 1,
|
30
|
+
gidro: 1,
|
31
|
+
manip: 1,
|
32
|
+
speed: 1,
|
33
|
+
moscow: 1
|
34
|
+
}
|
7
35
|
|
8
36
|
let! :calc do
|
9
37
|
calc = PecCalcClient::Calc.new calc_params
|
10
|
-
calc.places = (1..3).map{ PecCalcClient::Place.new place_params }
|
38
|
+
calc.places = (1..3).map { PecCalcClient::Place.new place_params }
|
11
39
|
calc.delivery = PecCalcClient::Delivery.new delivery_params
|
12
40
|
calc.take = PecCalcClient::Take.new take_params
|
13
41
|
calc
|
@@ -16,7 +44,9 @@ describe 'Calc' do
|
|
16
44
|
describe '#initialize' do
|
17
45
|
it 'Should be initialized' do
|
18
46
|
calc = PecCalcClient::Calc.new(calc_params)
|
19
|
-
hash_of_params = PecCalcClient::Calc::PARAMS.map
|
47
|
+
hash_of_params = PecCalcClient::Calc::PARAMS.map do |k|
|
48
|
+
[k.to_sym, calc.send(k)]
|
49
|
+
end.to_h
|
20
50
|
expect(hash_of_params).to eq calc_params
|
21
51
|
end
|
22
52
|
end
|
@@ -1,10 +1,9 @@
|
|
1
1
|
describe 'Connector' do
|
2
|
-
|
3
2
|
url = 'http://pecom.ru/ru/calc/towns.php'
|
4
3
|
bad_url = 'http://1.ru'
|
5
4
|
|
6
|
-
let!(:connector){ PecCalcClient::Connector.new(url) }
|
7
|
-
let!(:bad_connector){ PecCalcClient::Connector.new(bad_url) }
|
5
|
+
let!(:connector) { PecCalcClient::Connector.new(url) }
|
6
|
+
let!(:bad_connector) { PecCalcClient::Connector.new(bad_url) }
|
8
7
|
|
9
8
|
describe '#initialize' do
|
10
9
|
it 'Should be object' do
|
@@ -19,8 +18,8 @@ describe 'Connector' do
|
|
19
18
|
end
|
20
19
|
|
21
20
|
it 'Raise Exception' do
|
22
|
-
expect(
|
21
|
+
expect(-> { bad_connector.request })
|
22
|
+
.to raise_error(PecCalcClient::ConnectionError)
|
23
23
|
end
|
24
24
|
end
|
25
|
-
|
26
25
|
end
|
@@ -1,11 +1,19 @@
|
|
1
1
|
describe 'Delivery' do
|
2
|
-
|
3
|
-
|
2
|
+
delivery_params = {
|
3
|
+
town: 64_883,
|
4
|
+
tent: 1,
|
5
|
+
gidro: 1,
|
6
|
+
manip: 1,
|
7
|
+
speed: 0,
|
8
|
+
moscow: 0
|
9
|
+
}
|
4
10
|
|
5
11
|
describe '#initialize' do
|
6
12
|
it 'Should be initialized' do
|
7
13
|
delivery = PecCalcClient::Delivery.new(delivery_params)
|
8
|
-
hash_of_params = PecCalcClient::Delivery::PARAMS.map
|
14
|
+
hash_of_params = PecCalcClient::Delivery::PARAMS.map do |k|
|
15
|
+
[k.to_sym, delivery.send(k)]
|
16
|
+
end.to_h
|
9
17
|
expect(hash_of_params).to eq delivery_params
|
10
18
|
end
|
11
19
|
end
|
@@ -1,12 +1,27 @@
|
|
1
|
-
describe
|
2
|
-
|
3
|
-
|
4
|
-
|
1
|
+
describe 'Place' do
|
2
|
+
place_params = {
|
3
|
+
width: 0.5,
|
4
|
+
length: 0.3,
|
5
|
+
height: 0.5,
|
6
|
+
volume: 0.2,
|
7
|
+
weight: 0.03,
|
8
|
+
oversized: 1,
|
9
|
+
sturdy_packaging: 1
|
10
|
+
}
|
11
|
+
let!(:place) { PecCalcClient::Place.new place_params }
|
5
12
|
|
6
13
|
describe '#initialize' do
|
7
14
|
it 'Should be initialized' do
|
8
15
|
place = PecCalcClient::Place.new(place_params)
|
9
|
-
expect([
|
16
|
+
expect([
|
17
|
+
place.width,
|
18
|
+
place.length,
|
19
|
+
place.height,
|
20
|
+
place.volume,
|
21
|
+
place.weight,
|
22
|
+
place.oversized,
|
23
|
+
place.sturdy_packaging
|
24
|
+
]).to eq place_params.values
|
10
25
|
end
|
11
26
|
end
|
12
27
|
|
@@ -1,8 +1,10 @@
|
|
1
|
-
|
2
1
|
describe 'Region' do
|
3
2
|
describe '#initialize' do
|
4
3
|
it 'Should initialize' do
|
5
|
-
region = PecCalcClient::Region.new(
|
4
|
+
region = PecCalcClient::Region.new(
|
5
|
+
'RegionName',
|
6
|
+
[{ id: 14, name: 'Name1' }, { id: 11, name: 'Name' }]
|
7
|
+
)
|
6
8
|
expect(region.name).to eq 'RegionName'
|
7
9
|
expect(region.towns.first.class.to_s).to eq 'PecCalcClient::Town'
|
8
10
|
end
|
@@ -1,10 +1,9 @@
|
|
1
1
|
describe 'Response' do
|
2
|
-
|
3
2
|
url = 'http://pecom.ru/ru/calc/towns.php'
|
4
3
|
not_found_url = 'http://pecom.ru/not_this_url'
|
5
|
-
let!(:connector){ PecCalcClient::Connector.new(url) }
|
6
|
-
let!(:not_found_connector){ PecCalcClient::Connector.new(not_found_url) }
|
7
|
-
let!(:response){ connector.request }
|
4
|
+
let!(:connector) { PecCalcClient::Connector.new(url) }
|
5
|
+
let!(:not_found_connector) { PecCalcClient::Connector.new(not_found_url) }
|
6
|
+
let!(:response) { connector.request }
|
8
7
|
|
9
8
|
describe '#initialize' do
|
10
9
|
it 'Should be 200' do
|
@@ -13,7 +12,8 @@ describe 'Response' do
|
|
13
12
|
end
|
14
13
|
|
15
14
|
it 'Raise Exception BadResponse' do
|
16
|
-
expect(
|
15
|
+
expect(-> { not_found_connector.request })
|
16
|
+
.to raise_error(PecCalcClient::BadResponse)
|
17
17
|
end
|
18
18
|
end
|
19
19
|
|
@@ -1,11 +1,12 @@
|
|
1
1
|
describe 'Take' do
|
2
|
-
|
3
2
|
take_params = { town: -457, tent: 1, gidro: 1, manip: 1, speed: 1, moscow: 1 }
|
4
3
|
|
5
4
|
describe '#initialize' do
|
6
5
|
it 'Should be initialized' do
|
7
6
|
take = PecCalcClient::Take.new(take_params)
|
8
|
-
hash_of_params = PecCalcClient::Take::PARAMS.map
|
7
|
+
hash_of_params = PecCalcClient::Take::PARAMS.map do |k|
|
8
|
+
[k.to_sym, take.send(k)]
|
9
|
+
end.to_h
|
9
10
|
expect(hash_of_params).to eq take_params
|
10
11
|
end
|
11
12
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pec_calc_client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- MAXOPKA
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-12-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: json
|
@@ -38,7 +38,9 @@ dependencies:
|
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '1.6'
|
41
|
-
description:
|
41
|
+
description: |-
|
42
|
+
Gem for working with public API http://pecom.ru/business/dev
|
43
|
+
elopers/api_public/
|
42
44
|
email: d.n.krivenko@gmail.com
|
43
45
|
executables: []
|
44
46
|
extensions: []
|