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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1f8e024637ff9ea194e3506a0c4136ee40f4925c
4
- data.tar.gz: ed9b333c2e543865d30173cc07145ca1c41b0e0b
3
+ metadata.gz: f1649565b1eb9a909a3317eefd280cc6b77c436c
4
+ data.tar.gz: 9aafc336b1a3c22e01278971e3c50a28dd8c75d2
5
5
  SHA512:
6
- metadata.gz: a2e2c8563ef730037e0f96f39e4929723cc444d1dbf2791ec75d6f757333dba1295a99eedc10ce8189d376bd60c46918aa0f169ca8898c113bde45498820bb09
7
- data.tar.gz: 8efe7beee275daa3df819d053f26ec30b632b0d2a53ead28c40eb2552a0a8adbab3067e9f9908ba7cf54f0320427e0a3ed964ed53fa3be9b7310e1cf2bf158a9
6
+ metadata.gz: b5a19004f6e6267cc2f96f096c5b8fc16199fe27df1d82a600ffa7ff3c036071ac2708e590d2e854218c431e66d5e5de1132cec25b5d82bf7ebc691c0441fc1b
7
+ data.tar.gz: 910a1abf359bdc9b343c9c9981204f70f4d8f0c6b8175fac3cae28679fa29ee996f7f965d27f756e2e377cd141a3dbe8b7e18d68ef75a12542ec1ff3d6dc224a
data/Gemfile.lock CHANGED
@@ -1,8 +1,9 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- pec_calc_client (0.1.2)
4
+ pec_calc_client (1.0.0)
5
5
  json (~> 1.8)
6
+ rack (~> 1.6)
6
7
 
7
8
  GEM
8
9
  remote: https://rubygems.org/
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 "bundler/gem_tasks"
1
+ require 'bundler/gem_tasks'
data/bin/console CHANGED
@@ -1,14 +1,14 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require "bundler/setup"
4
- require "pec/calc/client"
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 "pry"
10
+ # require 'pry'
11
11
  # Pry.start
12
12
 
13
- require "irb"
13
+ require 'irb'
14
14
  IRB.start
@@ -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{ plombir strah ashan night pal pallets }
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
- set_params params
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{ |place, i| [ i, place.to_a ] }.to_h
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
@@ -1,11 +1,10 @@
1
1
  module PecCalcClient
2
+ # Result of delivery calculation
2
3
  class CalcResult
3
-
4
4
  attr_reader :result
5
5
 
6
6
  def initialize(params)
7
7
  @result = params
8
8
  end
9
-
10
9
  end
11
10
  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(/[\[\]\.\-]/, '[' => '%5B', ']' => '%5D', '.' => '%2E', '-' => '%2D')
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{ town tent gidro manip speed moscow }
7
+ PARAMS = %w( town tent gidro manip speed moscow )
8
8
 
9
9
  set_readers
10
10
 
11
11
  def initialize(params)
12
- set_params params
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 = %w{ width length height volume weight oversized sturdy_packaging }
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{ |k, v| instance_variable_set("@#{ k }", v) if PLACE_PARAMS.include? k.to_s }
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("@#{ k }") }
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 name, towns
8
+ def initialize(name, towns)
9
9
  @name = name
10
- @towns = towns.collect{ |id, name| Town.new id, name }
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{ |name, towns| self.new name, towns }
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
- raise BadResponse, 'Respons Code not 200' if @response.code != '200'
8
+ fail BadResponse, 'Respons Code not 200' if @response.code != '200'
8
9
  end
9
10
 
10
11
  def to_hash
@@ -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{ town tent gidro manip speed moscow }
7
+ PARAMS = %w( town tent gidro manip speed moscow )
8
8
 
9
9
  set_readers
10
10
 
11
11
  def initialize(params)
12
- set_params params
12
+ init_params params
13
13
  end
14
-
15
14
  end
16
15
  end
@@ -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 id, name
7
- @id, @name = id, name
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
- self.new town
13
+ new town
13
14
  end
14
15
  end
15
16
  end
@@ -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 set_params(params)
13
- params.each{ |k, v| instance_variable_set("@#{ k }", v) if self.class::PARAMS.include? k.to_s }
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("@#{ k }") ] }.to_h
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["#{ File.dirname(__FILE__) }/pec_calc_client/*.rb"].each {|file| require file }
29
+ Dir["#{File.dirname(__FILE__)}/pec_calc_client/*.rb"].each do |file|
30
+ require file
31
+ end
@@ -1,7 +1,7 @@
1
1
  describe 'CalcResult' do
2
2
  describe '#initialize' do
3
3
  it 'Should be initialized' do
4
- calc_result = PecCalcClient::CalcResult.new(Hash.new)
4
+ calc_result = PecCalcClient::CalcResult.new({})
5
5
  expect(calc_result.result.class).to eq Hash
6
6
  end
7
7
  end
@@ -1,13 +1,41 @@
1
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 }
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{ |k| [ k.to_sym, calc.send(k) ] }.to_h
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(lambda { bad_connector.request }).to raise_error(PecCalcClient::ConnectionError)
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
- delivery_params = { town: 64883, tent: 1, gidro: 1, manip: 1, speed: 0, moscow: 0 }
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{ |k| [ k.to_sym, delivery.send(k) ] }.to_h
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 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 }
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([place.width, place.length, place.height, place.volume, place.weight, place.oversized, place.sturdy_packaging]).to eq place_params.values
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('RegionName', [{ id: 14, name: 'Name1' }, { id: 11, name: 'Name' }] )
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( lambda{ not_found_connector.request } ).to raise_error(PecCalcClient::BadResponse)
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{ |k| [ k.to_sym, take.send(k) ] }.to_h
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
@@ -1,7 +1,7 @@
1
1
  describe 'Town' do
2
2
  describe '#initialize' do
3
3
  it 'Return Town' do
4
- town = PecCalcClient::Town.new( 13, 'Town' )
4
+ town = PecCalcClient::Town.new(13, 'Town')
5
5
  expect(town.id).to eq 13
6
6
  expect(town.name).to eq 'Town'
7
7
  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.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-10-11 00:00:00.000000000 Z
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: Gem for working with public API http://pecom.ru/business/developers/api_public/
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: []