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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3a5442cfe0d5844b3435aad18a8e75e790f9e9eb
4
- data.tar.gz: 9a73808bf1aaf4ee026efa530f97893e3713bed6
3
+ metadata.gz: 28d6acf99bc3392515d1cd82ebf962d742e36f3a
4
+ data.tar.gz: 752bfe90ab4886eff12149cb2ce10d7ad680f994
5
5
  SHA512:
6
- metadata.gz: a284fbbe25724c9afd66948f3d1b40eb8e7477b5217d84266d8907682294ec6f65e351be8c81f48c5b70fa09e6e6703887fa47cffb9d46489a982e67a92fcc70
7
- data.tar.gz: ed854dd3868407f512e46c0c3585cc8799350e42884e45edb808885b489f8544015d682493501a20a80679767e7f4acaf83dc5b97275a7c23c3479c215c36e9c
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,14 @@
1
+ require 'json'
2
+
3
+ module PecCalcClient
4
+ class Response
5
+ def initialize(response)
6
+ @response = response
7
+ raise BadResponse, 'Respons Code not 200' if response.code != '200'
8
+ end
9
+
10
+ def to_hash
11
+ JSON.parse @response.body
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,16 @@
1
+ module PecCalcClient
2
+ class Town
3
+
4
+ attr_reader :id, :name
5
+
6
+ def initialize id, name
7
+ @id, @name = id, name
8
+ end
9
+
10
+ def self.all
11
+ Connector.new(URL).request.to_json.collect do |town|
12
+ self.new town
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,9 @@
1
+ module PecCalcClient #:nodoc:
2
+ module VERSION #:nodoc:
3
+ MAJOR = 0
4
+ MINOR = 1
5
+ TINY = 1
6
+
7
+ STRING = [MAJOR, MINOR, TINY].join('.')
8
+ end
9
+ 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
@@ -0,0 +1,9 @@
1
+ describe 'Town' do
2
+ describe '#initialize' do
3
+ it 'Return Town' do
4
+ town = PecCalcClient::Town.new( 13, 'Town' )
5
+ expect(town.id).to eq 13
6
+ expect(town.name).to eq 'Town'
7
+ end
8
+ end
9
+ 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.0
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