pec_calc_client 0.1.0 → 0.1.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/lib/pec_calc_client/connector.rb +18 -0
- data/lib/pec_calc_client/region.rb +17 -0
- data/lib/pec_calc_client/response.rb +14 -0
- data/lib/pec_calc_client/town.rb +16 -0
- data/lib/pec_calc_client/version.rb +9 -0
- data/spec/pec_calc_client/connector_spec.rb +26 -0
- data/spec/pec_calc_client/region_spec.rb +17 -0
- data/spec/pec_calc_client/response_spec.rb +25 -0
- data/spec/pec_calc_client/town_spec.rb +9 -0
- metadata +10 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 28d6acf99bc3392515d1cd82ebf962d742e36f3a
|
4
|
+
data.tar.gz: 752bfe90ab4886eff12149cb2ce10d7ad680f994
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 18aa1afe0ff7a0ee099e2c0d86cdf17fa886f4a625dc20441d179defc5387508c1b3caaefc71463782efebd10f891595bb8053b25b57e155b418a9b7dc4eaa28
|
7
|
+
data.tar.gz: 19cec9c3a3b311ace73743558d9bbb2b86583a36bc898c2cf9498bc993774c333672bdf0bb4f33000ad7a8dcb5ead800f9331fea42c32138a8832432f15b04a2
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
|
3
|
+
module PecCalcClient
|
4
|
+
class Connector
|
5
|
+
def initialize(url)
|
6
|
+
@url = url.to_s
|
7
|
+
end
|
8
|
+
|
9
|
+
def request
|
10
|
+
begin
|
11
|
+
response = Net::HTTP.get_response(URI(@url))
|
12
|
+
rescue
|
13
|
+
raise PecCalcClient::ConnectionError, 'Bad connection'
|
14
|
+
end
|
15
|
+
@response = Response.new response
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module PecCalcClient
|
2
|
+
class Region
|
3
|
+
|
4
|
+
URL = 'http://pecom.ru/ru/calc/towns.php'
|
5
|
+
|
6
|
+
attr_reader :name, :towns
|
7
|
+
|
8
|
+
def initialize name, towns
|
9
|
+
@name = name
|
10
|
+
@towns = towns.collect{ |id, name| Town.new id, name }
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.all
|
14
|
+
Connector.new(self::URL).request.to_hash.collect{ |name, towns| self.new name, towns }
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
describe 'Connector' do
|
2
|
+
|
3
|
+
url = 'http://pecom.ru/ru/calc/towns.php'
|
4
|
+
bad_url = 'http://1.ru'
|
5
|
+
|
6
|
+
let!(:connector){ PecCalcClient::Connector.new(url) }
|
7
|
+
let!(:bad_connector){ PecCalcClient::Connector.new(bad_url) }
|
8
|
+
|
9
|
+
describe '#initialize' do
|
10
|
+
it 'Should be object' do
|
11
|
+
connector = PecCalcClient::Connector.new(url)
|
12
|
+
expect(connector.instance_variable_get(:@url)).to eq url
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
describe '#request' do
|
17
|
+
it 'Should return Response' do
|
18
|
+
expect(connector.request.class.to_s).to eq 'PecCalcClient::Response'
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'Raise Exception' do
|
22
|
+
expect(lambda { bad_connector.request }).to raise_error(PecCalcClient::ConnectionError)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
|
2
|
+
describe 'Region' do
|
3
|
+
describe '#initialize' do
|
4
|
+
it 'Should initialize' do
|
5
|
+
region = PecCalcClient::Region.new('RegionName', [{ id: 14, name: 'Name1' }, { id: 11, name: 'Name' }] )
|
6
|
+
expect(region.name).to eq 'RegionName'
|
7
|
+
expect(region.towns.first.class.to_s).to eq 'PecCalcClient::Town'
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
describe '::all' do
|
12
|
+
it 'Return Regions' do
|
13
|
+
regions = PecCalcClient::Region.all
|
14
|
+
expect(regions.first.class.to_s).to eq 'PecCalcClient::Region'
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
describe 'Response' do
|
2
|
+
|
3
|
+
url = 'http://pecom.ru/ru/calc/towns.php'
|
4
|
+
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 }
|
8
|
+
|
9
|
+
describe '#initialize' do
|
10
|
+
it 'Should be 200' do
|
11
|
+
good_response = connector.request
|
12
|
+
expect(good_response.instance_variable_get(:@response).code).to eq '200'
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'Raise Exception BadResponse' do
|
16
|
+
expect( lambda{ not_found_connector.request } ).to raise_error(PecCalcClient::BadResponse)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
describe '#to_hash' do
|
21
|
+
it 'Should be hash' do
|
22
|
+
expect(response.to_hash.class.to_s).to eq 'Hash'
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pec_calc_client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- MAXOPKA
|
@@ -23,6 +23,15 @@ files:
|
|
23
23
|
- bin/console
|
24
24
|
- bin/setup
|
25
25
|
- lib/pec_calc_client.rb
|
26
|
+
- lib/pec_calc_client/connector.rb
|
27
|
+
- lib/pec_calc_client/region.rb
|
28
|
+
- lib/pec_calc_client/response.rb
|
29
|
+
- lib/pec_calc_client/town.rb
|
30
|
+
- lib/pec_calc_client/version.rb
|
31
|
+
- spec/pec_calc_client/connector_spec.rb
|
32
|
+
- spec/pec_calc_client/region_spec.rb
|
33
|
+
- spec/pec_calc_client/response_spec.rb
|
34
|
+
- spec/pec_calc_client/town_spec.rb
|
26
35
|
- spec/pec_calc_client_spec.rb
|
27
36
|
- spec/spec_helper.rb
|
28
37
|
homepage: https://rubygems.org/gems/pec_calc_client
|