pec_calc_client 0.1.2 → 1.0.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 061c7195b4134e7398830f60428628c237ea35e6
4
- data.tar.gz: 355d4faabba4869da2d6b03314f9033e77ad8739
3
+ metadata.gz: 1f8e024637ff9ea194e3506a0c4136ee40f4925c
4
+ data.tar.gz: ed9b333c2e543865d30173cc07145ca1c41b0e0b
5
5
  SHA512:
6
- metadata.gz: a396004dfbb4c6e4c5fe85ae98f6866aee5589bebda8b7ac7778125ae832605151b17a1395036d11d7560a670d66abd2019f0d0fe6ca9fc5e69792e98bf79879
7
- data.tar.gz: 5392fcb455cc9d3b26ad88fe3e99e1743e745525d2fb564ee5a7e3cb24d186e3db2b54e2ee9319062f3aaf052fa81b07a82f2b178480e0a457faffb9530314a3
6
+ metadata.gz: a2e2c8563ef730037e0f96f39e4929723cc444d1dbf2791ec75d6f757333dba1295a99eedc10ce8189d376bd60c46918aa0f169ca8898c113bde45498820bb09
7
+ data.tar.gz: 8efe7beee275daa3df819d053f26ec30b632b0d2a53ead28c40eb2552a0a8adbab3067e9f9908ba7cf54f0320427e0a3ed964ed53fa3be9b7310e1cf2bf158a9
data/Gemfile CHANGED
@@ -8,4 +8,5 @@ group :development do
8
8
  gem 'faker'
9
9
  end
10
10
 
11
+ gem 'rack'
11
12
  gem 'json'
data/Gemfile.lock CHANGED
@@ -1,7 +1,8 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- pec_calc_client (0.1.0)
4
+ pec_calc_client (0.1.2)
5
+ json (~> 1.8)
5
6
 
6
7
  GEM
7
8
  remote: https://rubygems.org/
@@ -11,6 +12,7 @@ GEM
11
12
  i18n (~> 0.5)
12
13
  i18n (0.7.0)
13
14
  json (1.8.3)
15
+ rack (1.6.4)
14
16
  rspec (3.3.0)
15
17
  rspec-core (~> 3.3.0)
16
18
  rspec-expectations (~> 3.3.0)
@@ -32,6 +34,7 @@ DEPENDENCIES
32
34
  faker
33
35
  json
34
36
  pec_calc_client!
37
+ rack
35
38
  rspec
36
39
 
37
40
  BUNDLED WITH
data/README.md CHANGED
@@ -4,6 +4,7 @@ Gem for integration with API transport company PEC
4
4
 
5
5
  [API description](http://pecom.ru/business/developers/api_public/)
6
6
 
7
+ [![Gem Version](https://badge.fury.io/rb/pec_calc_client.svg)](https://badge.fury.io/rb/pec_calc_client)
7
8
  ## Installation
8
9
 
9
10
  Add this line to your application's Gemfile:
@@ -0,0 +1,39 @@
1
+ module PecCalcClient
2
+ class Calc
3
+
4
+ extend RequestClass
5
+ include RequestObject
6
+
7
+ URL = 'http://pecom.ru/bitrix/components/pecom/calc/ajax.php'
8
+
9
+ PARAMS = %w{ plombir strah ashan night pal pallets }
10
+
11
+ set_readers
12
+
13
+ attr_accessor :delivery, :take, :places
14
+
15
+ def initialize(params)
16
+ set_params params
17
+ end
18
+
19
+ def calc
20
+ result = Connector.new(URL).request(prepare_params)
21
+ prepare_result result.to_hash
22
+ end
23
+
24
+ def prepare_params
25
+ out = to_h
26
+ out[:deliver] = @delivery.to_h
27
+ out[:take] = @take.to_h
28
+ out[:places] = @places.each_with_index.map{ |place, i| [ i, place.to_a ] }.to_h
29
+ out
30
+ end
31
+
32
+ private
33
+
34
+ def prepare_result(result)
35
+ CalcResult.new result.to_hash
36
+ end
37
+
38
+ end
39
+ end
@@ -0,0 +1,11 @@
1
+ module PecCalcClient
2
+ class CalcResult
3
+
4
+ attr_reader :result
5
+
6
+ def initialize(params)
7
+ @result = params
8
+ end
9
+
10
+ end
11
+ end
@@ -1,4 +1,5 @@
1
1
  require 'net/http'
2
+ require 'rack/utils'
2
3
 
3
4
  module PecCalcClient
4
5
  class Connector
@@ -6,13 +7,24 @@ module PecCalcClient
6
7
  @url = url.to_s
7
8
  end
8
9
 
9
- def request
10
+ def request(params = {})
11
+ prepare_params params
10
12
  begin
11
- response = Net::HTTP.get_response(URI(@url))
13
+ response = Net::HTTP.get_response(URI(url_with_params))
12
14
  rescue
13
15
  raise PecCalcClient::ConnectionError, 'Bad connection'
14
16
  end
15
- @response = Response.new response
17
+ Response.new response
18
+ end
19
+
20
+ private
21
+
22
+ def prepare_params(params)
23
+ @params_string = Rack::Utils.build_nested_query(params).gsub(/[\[\]\.\-]/, '[' => '%5B', ']' => '%5D', '.' => '%2E', '-' => '%2D')
24
+ end
25
+
26
+ def url_with_params
27
+ "#{@url}?#{@params_string}"
16
28
  end
17
29
  end
18
30
  end
@@ -0,0 +1,15 @@
1
+ module PecCalcClient
2
+ class Delivery
3
+
4
+ extend RequestClass
5
+ include RequestObject
6
+
7
+ PARAMS = %w{ town tent gidro manip speed moscow }
8
+
9
+ set_readers
10
+
11
+ def initialize(params)
12
+ set_params params
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,16 @@
1
+ module PecCalcClient
2
+ class Place
3
+
4
+ PLACE_PARAMS = %w{ width length height volume weight oversized sturdy_packaging }
5
+
6
+ PLACE_PARAMS.each{ |param_name| attr_reader param_name }
7
+
8
+ def initialize(params)
9
+ params.each{ |k, v| instance_variable_set("@#{ k }", v) if PLACE_PARAMS.include? k.to_s }
10
+ end
11
+
12
+ def to_a
13
+ PLACE_PARAMS.map{ |k| instance_variable_get("@#{ k }") }
14
+ end
15
+ end
16
+ end
@@ -4,11 +4,11 @@ module PecCalcClient
4
4
  class Response
5
5
  def initialize(response)
6
6
  @response = response
7
- raise BadResponse, 'Respons Code not 200' if response.code != '200'
7
+ raise BadResponse, 'Respons Code not 200' if @response.code != '200'
8
8
  end
9
9
 
10
10
  def to_hash
11
- JSON.parse @response.body
11
+ JSON.parse @response.body if @response.body.to_s != ''
12
12
  end
13
13
  end
14
14
  end
@@ -0,0 +1,16 @@
1
+ module PecCalcClient
2
+ class Take
3
+
4
+ extend RequestClass
5
+ include RequestObject
6
+
7
+ PARAMS = %w{ town tent gidro manip speed moscow }
8
+
9
+ set_readers
10
+
11
+ def initialize(params)
12
+ set_params params
13
+ end
14
+
15
+ end
16
+ end
@@ -1,8 +1,8 @@
1
1
  module PecCalcClient #:nodoc:
2
2
  module VERSION #:nodoc:
3
- MAJOR = 0
4
- MINOR = 1
5
- TINY = 2
3
+ MAJOR = 1
4
+ MINOR = 0
5
+ TINY = 0
6
6
 
7
7
  STRING = [MAJOR, MINOR, TINY].join('.')
8
8
  end
@@ -1,8 +1,22 @@
1
- # require 'pec/calc/client/version'
2
-
3
- Dir["#{ File.dirname(__FILE__) }/pec_calc_client/*.rb"].each {|file| require file }
4
-
5
1
  module PecCalcClient
6
2
  class ConnectionError < Exception; end
7
3
  class BadResponse < Exception; end
4
+
5
+ module RequestClass
6
+ def set_readers
7
+ self::PARAMS.each{ |param_name| attr_reader param_name }
8
+ end
9
+ end
10
+
11
+ module RequestObject
12
+ def set_params(params)
13
+ params.each{ |k, v| instance_variable_set("@#{ k }", v) if self.class::PARAMS.include? k.to_s }
14
+ end
15
+
16
+ def to_h
17
+ self.class::PARAMS.map{ |k| [k, instance_variable_get("@#{ k }") ] }.to_h
18
+ end
19
+ end
8
20
  end
21
+
22
+ Dir["#{ File.dirname(__FILE__) }/pec_calc_client/*.rb"].each {|file| require file }
@@ -0,0 +1,8 @@
1
+ describe 'CalcResult' do
2
+ describe '#initialize' do
3
+ it 'Should be initialized' do
4
+ calc_result = PecCalcClient::CalcResult.new(Hash.new)
5
+ expect(calc_result.result.class).to eq Hash
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,30 @@
1
+ describe 'Calc' do
2
+
3
+ calc_params = { plombir: 12, strah: 33, ashan: 1, night: 1, pal: 3, pallets: 4 }
4
+ place_params = { width: 0.5, length: 0.3,height: 0.5, volume: 0.2, weight: 0.03, oversized: true, sturdy_packaging: true }
5
+ delivery_params = { town: 134104, tent: 1, gidro: 1, manip: 1, speed: 0, moscow: 0 }
6
+ take_params = { town: 175004, tent: 1, gidro: 1, manip: 1, speed: 1, moscow: 1 }
7
+
8
+ let! :calc do
9
+ calc = PecCalcClient::Calc.new calc_params
10
+ calc.places = (1..3).map{ PecCalcClient::Place.new place_params }
11
+ calc.delivery = PecCalcClient::Delivery.new delivery_params
12
+ calc.take = PecCalcClient::Take.new take_params
13
+ calc
14
+ end
15
+
16
+ describe '#initialize' do
17
+ it 'Should be initialized' do
18
+ calc = PecCalcClient::Calc.new(calc_params)
19
+ hash_of_params = PecCalcClient::Calc::PARAMS.map{ |k| [ k.to_sym, calc.send(k) ] }.to_h
20
+ expect(hash_of_params).to eq calc_params
21
+ end
22
+ end
23
+
24
+ describe '#calc' do
25
+ it 'Should return result' do
26
+ result = calc.calc
27
+ expect(result.class.to_s).to eq 'PecCalcClient::CalcResult'
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,12 @@
1
+ describe 'Delivery' do
2
+
3
+ delivery_params = { town: 64883, tent: 1, gidro: 1, manip: 1, speed: 0, moscow: 0 }
4
+
5
+ describe '#initialize' do
6
+ it 'Should be initialized' do
7
+ delivery = PecCalcClient::Delivery.new(delivery_params)
8
+ hash_of_params = PecCalcClient::Delivery::PARAMS.map{ |k| [ k.to_sym, delivery.send(k) ] }.to_h
9
+ expect(hash_of_params).to eq delivery_params
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,18 @@
1
+ describe do 'Place'
2
+
3
+ place_params = { width: 0.5, length: 0.3,height: 0.5, volume: 0.2, weight: 0.03, oversized: 1, sturdy_packaging: 1 }
4
+ let!(:place){ PecCalcClient::Place.new place_params }
5
+
6
+ describe '#initialize' do
7
+ it 'Should be initialized' do
8
+ place = PecCalcClient::Place.new(place_params)
9
+ expect([place.width, place.length, place.height, place.volume, place.weight, place.oversized, place.sturdy_packaging]).to eq place_params.values
10
+ end
11
+ end
12
+
13
+ describe '#to_a' do
14
+ it 'Shold return correctly string' do
15
+ expect(place.to_a).to eq place_params.values
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,12 @@
1
+ describe 'Take' do
2
+
3
+ take_params = { town: -457, tent: 1, gidro: 1, manip: 1, speed: 1, moscow: 1 }
4
+
5
+ describe '#initialize' do
6
+ it 'Should be initialized' do
7
+ take = PecCalcClient::Take.new(take_params)
8
+ hash_of_params = PecCalcClient::Take::PARAMS.map{ |k| [ k.to_sym, take.send(k) ] }.to_h
9
+ expect(hash_of_params).to eq take_params
10
+ end
11
+ end
12
+ end
@@ -1,2 +1 @@
1
1
  require 'spec_helper'
2
-
metadata CHANGED
@@ -1,29 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pec_calc_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - MAXOPKA
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-10-06 00:00:00.000000000 Z
11
+ date: 2015-10-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ~>
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
19
  version: '1.8'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ~>
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.8'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rack
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.6'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.6'
27
41
  description: Gem for working with public API http://pecom.ru/business/developers/api_public/
28
42
  email: d.n.krivenko@gmail.com
29
43
  executables: []
@@ -37,14 +51,24 @@ files:
37
51
  - bin/console
38
52
  - bin/setup
39
53
  - lib/pec_calc_client.rb
54
+ - lib/pec_calc_client/calc.rb
55
+ - lib/pec_calc_client/calc_result.rb
40
56
  - lib/pec_calc_client/connector.rb
57
+ - lib/pec_calc_client/delivery.rb
58
+ - lib/pec_calc_client/place.rb
41
59
  - lib/pec_calc_client/region.rb
42
60
  - lib/pec_calc_client/response.rb
61
+ - lib/pec_calc_client/take.rb
43
62
  - lib/pec_calc_client/town.rb
44
63
  - lib/pec_calc_client/version.rb
64
+ - spec/pec_calc_client/calc_result_spec.rb
65
+ - spec/pec_calc_client/calc_spec.rb
45
66
  - spec/pec_calc_client/connector_spec.rb
67
+ - spec/pec_calc_client/delivery_spec.rb
68
+ - spec/pec_calc_client/place_spec.rb
46
69
  - spec/pec_calc_client/region_spec.rb
47
70
  - spec/pec_calc_client/response_spec.rb
71
+ - spec/pec_calc_client/take_spec.rb
48
72
  - spec/pec_calc_client/town_spec.rb
49
73
  - spec/pec_calc_client_spec.rb
50
74
  - spec/spec_helper.rb
@@ -58,17 +82,17 @@ require_paths:
58
82
  - lib
59
83
  required_ruby_version: !ruby/object:Gem::Requirement
60
84
  requirements:
61
- - - '>='
85
+ - - ">="
62
86
  - !ruby/object:Gem::Version
63
87
  version: '0'
64
88
  required_rubygems_version: !ruby/object:Gem::Requirement
65
89
  requirements:
66
- - - '>='
90
+ - - ">="
67
91
  - !ruby/object:Gem::Version
68
92
  version: '0'
69
93
  requirements: []
70
94
  rubyforge_project:
71
- rubygems_version: 2.4.3
95
+ rubygems_version: 2.4.6
72
96
  signing_key:
73
97
  specification_version: 4
74
98
  summary: Gem for PEC transport Company API