postmon_ruby 2.0.0 → 2.1.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: ff5b09bd792c1b13f2df53c3a5e500c06ca3eff9
4
- data.tar.gz: 8e6400cf6705e415d68e1fe5b958e670d5ab7240
3
+ metadata.gz: 39598f0c0e34c0e99ff6c54aea8b765776b5135f
4
+ data.tar.gz: a0bd6342d7a753619d76d06663c9ee64124e0b07
5
5
  SHA512:
6
- metadata.gz: 693631c654886c4595a88e8d685ba24e05a63a0a7b691d4c90e209cbab147b7b13cac951ef3a75a2e22b9fdb412e5feddccf04e9416436dd347d9f1a430f7eff
7
- data.tar.gz: bdcb4c8e14479ff2985dc0ec37fcfdee0e4d4d8b8d9546c23f83467facdc35e902b840ed25aa34ac76eb2e1122788912a52e57bf0ad4a33acb4c3d2bd35ac97a
6
+ metadata.gz: 702c005ee15983c85e1d15791317fa26e506eafcb0d3a40f0a6fae9b1d36c8d4ebd3ecaa00b473957e836aafc869048065de26d3a203ebe2719e863017f8b36d
7
+ data.tar.gz: 35a1096e6ee240aff7b718d6a9a220063cd8b24ed87ee55e90fa6f7fe8467c01f2dfae876efd786c73c20215fa74069a2b44747b97b6a7ab43df9ee3cc8b0157
data/README.md CHANGED
@@ -35,6 +35,15 @@ puts resultado.area_km2
35
35
  puts resultado.codigo_ibge
36
36
  ```
37
37
 
38
+ ####Consultar informações de um estado:
39
+ ```ruby
40
+ require 'postmon_ruby'
41
+ resultado = PostmonRuby::Client.search :estado, "SP"
42
+ puts resultado.area_km2
43
+ puts resultado.codigo_ibge
44
+ puts resultado.nome
45
+ ```
46
+
38
47
  ## Contributing
39
48
 
40
49
  1. Fork it
data/bin/postmon-ruby ADDED
@@ -0,0 +1,27 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'optparse'
4
+ require './lib/postmon_ruby'
5
+
6
+ options = {}
7
+
8
+ OptionParser.new do |opts|
9
+ opts.banner = "Usage postmon [options]"
10
+ opts.on("--cep CEP", String, "Consultar CEP") do |cep|
11
+ options[:cep] = cep
12
+ end
13
+
14
+ opts.on("--cidade UF,Cidade", Array, "Consultar informações de uma cidade") do |uf_cidade|
15
+ options[:cidade] = uf_cidade
16
+ end
17
+
18
+ opts.on("--estado UF", String, "Consultar informações de um estado") do |uf|
19
+ options[:estado] = uf
20
+ end
21
+
22
+ opts.on("--rastreio CODIGO_POSTAGEM", String, "Consultar informações de rastreio de um pacote") do |rastreio|
23
+ options[:rastreio] = rastreio
24
+ end
25
+ end.parse!
26
+
27
+ PostmonRuby::GemOutput.get_info(options)
@@ -7,11 +7,11 @@ module PostmonRuby
7
7
  def initialize(options={})
8
8
  @not_found = true if options.nil?
9
9
  @@address_attributes.each do |attribute|
10
- send(:"#{attribute}=", options[attribute.to_s] || "")
10
+ send(:"#{attribute}=", options[attribute.to_s] || "")
11
11
  end
12
12
  end
13
13
 
14
14
  private
15
15
  attr_writer *@@address_attributes
16
16
  end
17
- end
17
+ end
@@ -6,4 +6,4 @@ module PostmonRuby
6
6
  # PostmonRuby::Finders::Finders.get_finder(finder).search(arguments)
7
7
  end
8
8
  end
9
- end
9
+ end
@@ -3,7 +3,9 @@ module PostmonRuby
3
3
  class Finders
4
4
  @finders = {
5
5
  cep: PostmonRuby::Finders::CepFinder.new,
6
- cidade: PostmonRuby::Finders::CityFinder.new
6
+ cidade: PostmonRuby::Finders::CityFinder.new,
7
+ estado: PostmonRuby::Finders::StateFinder.new,
8
+ rastreio: PostmonRuby::Finders::TrackerFinder.new
7
9
  }
8
10
 
9
11
  def self.get_finder(finder)
@@ -0,0 +1,18 @@
1
+ module PostmonRuby
2
+ module Finders
3
+ class StateFinder < PostmonRuby::Finders::Finder
4
+ def endpoint
5
+ "/uf"
6
+ end
7
+
8
+ def arguments_size
9
+ 1
10
+ end
11
+
12
+ def search(*arguments)
13
+ arguments.flatten!
14
+ PostmonRuby::State.new( HTTParty.get(self.arguments_uri(arguments)) )
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,20 @@
1
+ module PostmonRuby
2
+ module Finders
3
+ module Tracker
4
+ class ECTTrackerFinder < PostmonRuby::Finders::Finder
5
+ def endpoint
6
+ "rastreio/ect"
7
+ end
8
+
9
+ def arguments_size
10
+ 1
11
+ end
12
+
13
+ def search(*arguments)
14
+ arguments.flatten!
15
+ PostmonRuby::Track.new( HTTParty.get(self.arguments_uri(arguments)) )
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,18 @@
1
+ module PostmonRuby
2
+ module Finders
3
+ class TrackerFinder < PostmonRuby::Finders::Finder
4
+ @@trackers = {
5
+ ect: PostmonRuby::Finders::Tracker::ECTTrackerFinder.new
6
+ }
7
+ def arguments_size
8
+ 2
9
+ end
10
+
11
+ def search(*arguments)
12
+ arguments.flatten!
13
+ @@trackers[arguments.first].search(arguments[1..-1])
14
+ end
15
+
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,67 @@
1
+ module PostmonRuby
2
+ class GemOutput
3
+ def self.get_info(options)
4
+ unless options[:cep].nil?
5
+ cep(options[:cep])
6
+ end
7
+
8
+ unless options[:cidade].nil?
9
+ cidade(options[:cidade].first, options[:cidade].last)
10
+ end
11
+
12
+ unless options[:estado].nil?
13
+ estado(options[:estado])
14
+ end
15
+
16
+ unless options[:rastreio].nil?
17
+ rastreio(options[:rastreio])
18
+ end
19
+ end
20
+
21
+ private
22
+ def self.cep(cep)
23
+ resultado = Client.search :cep, cep
24
+ output_variables(resultado)
25
+ end
26
+
27
+ def self.cidade(uf, cidade)
28
+ resultado = Client.search :cidade, uf, cidade
29
+ output_variables(resultado)
30
+ end
31
+
32
+ def self.estado(uf)
33
+ resultado = PostmonRuby::Client.search :estado, uf
34
+ output_variables(resultado)
35
+ end
36
+
37
+ def self.rastreio(rastreio)
38
+ resultado = PostmonRuby::Client.search :rastreio, :ect, rastreio
39
+ output_variables(resultado)
40
+ end
41
+
42
+ def self.output_variables(resultado)
43
+ if resultado.not_found
44
+ puts "Nenhum resultado encontrado."
45
+ else
46
+ info = {}
47
+
48
+ resultado.instance_variables.each do |var|
49
+ info[var.to_s.delete("@").capitalize] = resultado.instance_variable_get(var)
50
+ end
51
+
52
+ print_array_output(info)
53
+ end
54
+ end
55
+
56
+ def self.print_array_output(array, level = 0)
57
+ array.each do |k, v|
58
+ print "\t" if level > 0
59
+ if(v.kind_of?(Enumerable) || k.kind_of?(Enumerable))
60
+ print_array_output(v||k, 1)
61
+ else
62
+ puts "#{k}: #{v}"
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,17 @@
1
+ module PostmonRuby
2
+ class State
3
+ @@state_attributes = [ :area_km2, :codigo_ibge, :nome ]
4
+
5
+ attr_reader :not_found, *@@state_attributes
6
+
7
+ def initialize(options={})
8
+ @not_found = true if options.nil?
9
+ @@state_attributes.each do |attribute|
10
+ send(:"#{attribute}=", options[attribute.to_s] || "")
11
+ end
12
+ end
13
+
14
+ private
15
+ attr_writer *@@state_attributes
16
+ end
17
+ end
@@ -0,0 +1,20 @@
1
+ module PostmonRuby
2
+ class Track
3
+ #TODO criar um model para salvar o historico do rastreio
4
+ @@track_attributes = [ :codigo, :servico ]
5
+
6
+ attr_reader :not_found, *@@track_attributes
7
+ attr_reader :historico
8
+
9
+ def initialize(options={})
10
+ @not_found = true if options.nil?
11
+ @@track_attributes.each do |attribute|
12
+ send(:"#{attribute}=", options[attribute.to_s] || "")
13
+ end
14
+ @historico = options["historico"] || []
15
+ end
16
+
17
+ private
18
+ attr_writer *@@track_attributes
19
+ end
20
+ end
@@ -1,3 +1,3 @@
1
1
  module PostmonRuby
2
- VERSION = "2.0.0"
2
+ VERSION = "2.1.0"
3
3
  end
data/lib/postmon_ruby.rb CHANGED
@@ -1,12 +1,17 @@
1
- require "postmon_ruby/version"
2
- require "postmon_ruby/client"
3
- require "postmon_ruby/address"
4
- require "postmon_ruby/city"
5
- require "postmon_ruby/finders/finder"
6
- require "postmon_ruby/finders/cep_finder"
7
- require "postmon_ruby/finders/city_finder"
8
- require "postmon_ruby/finders/finders"
9
-
1
+ require "./lib/postmon_ruby/version"
2
+ require "./lib/postmon_ruby/client"
3
+ require "./lib/postmon_ruby/city"
4
+ require "./lib/postmon_ruby/state"
5
+ require "./lib/postmon_ruby/address"
6
+ require "./lib/postmon_ruby/gem_output"
7
+ require "./lib/postmon_ruby/track"
8
+ require "./lib/postmon_ruby/finders/finder"
9
+ require "./lib/postmon_ruby/finders/cep_finder"
10
+ require "./lib/postmon_ruby/finders/city_finder"
11
+ require "./lib/postmon_ruby/finders/state_finder"
12
+ require "./lib/postmon_ruby/finders/tracker/ect_tracker_finder"
13
+ require "./lib/postmon_ruby/finders/tracker_finder"
14
+ require "./lib/postmon_ruby/finders/finders"
10
15
  require "httparty"
11
16
 
12
17
  module PostmonRuby
data/postmon_ruby.gemspec CHANGED
@@ -14,7 +14,7 @@ Gem::Specification.new do |spec|
14
14
  spec.license = "MIT"
15
15
 
16
16
  spec.files = `git ls-files`.split($/)
17
- spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.executables = 'postmon-ruby'
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ["lib"]
20
20
 
@@ -23,4 +23,5 @@ Gem::Specification.new do |spec|
23
23
  spec.add_development_dependency "rake"
24
24
  spec.add_development_dependency "pry"
25
25
  spec.add_development_dependency "rspec"
26
+ spec.add_development_dependency "webmock", "~> 1.17"
26
27
  end
@@ -47,6 +47,6 @@ module PostmonRuby
47
47
  end
48
48
 
49
49
  end
50
- end
50
+ end
51
51
  end
52
- end
52
+ end
@@ -0,0 +1,62 @@
1
+ require "spec_helper"
2
+
3
+ module PostmonRuby
4
+ module Finders
5
+ module Tracker
6
+
7
+ describe ECTTrackerFinder do
8
+ describe "#search" do
9
+ context "with too many arguments" do
10
+ it "raise an ArgumentError" do
11
+ expect{ PostmonRuby::Client.search(:rastreio, :ect, "TESTE", "1234123") }.to raise_error(ArgumentError)
12
+ end
13
+ end
14
+
15
+ context "with less arguments" do
16
+ it "raise an ArgumentError" do
17
+ expect{ PostmonRuby::Client.search(:rastreio, :ect) }.to raise_error(ArgumentError)
18
+ end
19
+ end
20
+
21
+ context "with valid tracking code" do
22
+ # WebMock.disable_net_connect!
23
+ let(:track) {
24
+ #TODO - stubar a request para retornar um json
25
+ # stub_request(:any, "api.postmon.com.br/v1/rastreio/ect/TESTE").
26
+ # to_return(body: File.new('spec/support/track.json').read.to_json, status: 200, content_type: 'application/json')
27
+ # PostmonRuby::Client.search(:rastreio, :ect, "TESTE" )
28
+ PostmonRuby::Client.search(:rastreio, :ect, "PG864394568BR")
29
+ }
30
+ it "returns a PostmonRuby::Track object" do
31
+ expect(track).to be_a(PostmonRuby::Track)
32
+ end
33
+ it "returns array in history attribute" do
34
+ expect(track.historico).to be_a(Array)
35
+ end
36
+ it "returns 6 itens in history" do
37
+ track.historico.should have(6).items
38
+ end
39
+ it "returns a complete track info" do
40
+ expect(track.servico).to eq "ect"
41
+ expect(track.codigo).to eq "PG864394568BR"
42
+ expect(track.historico.first["local"]).to eq "CEE VILA NOVA CACHOEIRINHA - SAO PAULO/SP"
43
+ expect(track.historico.first["situacao"]).to eq "Postado"
44
+ end
45
+ # WebMock.allow_net_connect!
46
+ end
47
+
48
+ context "with invalid tracking code" do
49
+ let(:track) { PostmonRuby::Client.search(:rastreio, :ect, "00000000") }
50
+ it "returns a PostmonRuby::Track object" do
51
+ expect(track).to be_a(PostmonRuby::Track)
52
+ end
53
+ it "returns true in 'not_found'" do
54
+ expect(track.not_found).to be_true
55
+ end
56
+ end
57
+
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,44 @@
1
+ require "spec_helper"
2
+
3
+ module PostmonRuby
4
+ module Finders
5
+ describe StateFinder do
6
+ describe "#search" do
7
+ context "with too many arguments" do
8
+ it "raise an ArgumentError" do
9
+ expect{ PostmonRuby::Client.search(:estado, "SP", "xxxxx") }.to raise_error(ArgumentError)
10
+ end
11
+ end
12
+
13
+ context "with less arguments" do
14
+ it "raise an ArgumentError" do
15
+ expect{ PostmonRuby::Client.search(:estado) }.to raise_error(ArgumentError)
16
+ end
17
+ end
18
+
19
+ context "with valid state" do
20
+ let(:state) { PostmonRuby::Client.search(:estado, "SP" ) }
21
+ it "returns a PostmonRuby::State object" do
22
+ expect(state).to be_a(PostmonRuby::State)
23
+ end
24
+ it "returns a complete state info" do
25
+ expect(state.area_km2).to eq "248.222,801"
26
+ expect(state.codigo_ibge).to eq "35"
27
+ expect(state.nome).to eq "São Paulo"
28
+ end
29
+ end
30
+
31
+ context "with invalid state" do
32
+ let(:state) { PostmonRuby::Client.search(:estado, "SK") }
33
+ it "returns a PostmonRuby::State object" do
34
+ expect(state).to be_a(PostmonRuby::State)
35
+ end
36
+ it "returns true in 'not_found'" do
37
+ expect(state.not_found).to be_true
38
+ end
39
+ end
40
+
41
+ end
42
+ end
43
+ end
44
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,10 +1,13 @@
1
1
  require "postmon_ruby"
2
+ require "webmock/rspec"
3
+ include WebMock::API
2
4
  # This file was generated by the `rspec --init` command. Conventionally, all
3
5
  # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
4
6
  # Require this file using `require "spec_helper"` to ensure that it is only
5
7
  # loaded once.
6
8
  #
7
9
  # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
10
+ WebMock.allow_net_connect!
8
11
  RSpec.configure do |config|
9
12
  config.treat_symbols_as_metadata_keys_with_true_values = true
10
13
  config.run_all_when_everything_filtered = true
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: postmon_ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Carlos Ribeiro
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-04-23 00:00:00.000000000 Z
11
+ date: 2014-05-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty
@@ -80,10 +80,25 @@ dependencies:
80
80
  - - '>='
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: webmock
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ~>
88
+ - !ruby/object:Gem::Version
89
+ version: '1.17'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ~>
95
+ - !ruby/object:Gem::Version
96
+ version: '1.17'
83
97
  description: A rubygem to access the Postmon API
84
98
  email:
85
99
  - mail@carlosribeiro.me
86
- executables: []
100
+ executables:
101
+ - postmon-ruby
87
102
  extensions: []
88
103
  extra_rdoc_files: []
89
104
  files:
@@ -93,6 +108,7 @@ files:
93
108
  - LICENSE.txt
94
109
  - README.md
95
110
  - Rakefile
111
+ - bin/postmon-ruby
96
112
  - lib/postmon_ruby.rb
97
113
  - lib/postmon_ruby/address.rb
98
114
  - lib/postmon_ruby/city.rb
@@ -101,11 +117,19 @@ files:
101
117
  - lib/postmon_ruby/finders/city_finder.rb
102
118
  - lib/postmon_ruby/finders/finder.rb
103
119
  - lib/postmon_ruby/finders/finders.rb
120
+ - lib/postmon_ruby/finders/state_finder.rb
121
+ - lib/postmon_ruby/finders/tracker/ect_tracker_finder.rb
122
+ - lib/postmon_ruby/finders/tracker_finder.rb
123
+ - lib/postmon_ruby/gem_output.rb
124
+ - lib/postmon_ruby/state.rb
125
+ - lib/postmon_ruby/track.rb
104
126
  - lib/postmon_ruby/version.rb
105
127
  - postmon_ruby.gemspec
106
128
  - spec/postmon_ruby/cep_finder_spec.rb
107
129
  - spec/postmon_ruby/city_finder_spec.rb
108
130
  - spec/postmon_ruby/client_spec.rb
131
+ - spec/postmon_ruby/ect_tracker_finder_spec.rb
132
+ - spec/postmon_ruby/state_finder_spec.rb
109
133
  - spec/spec_helper.rb
110
134
  homepage: https://github.com/PostmonAPI/postmon-ruby
111
135
  licenses:
@@ -135,4 +159,6 @@ test_files:
135
159
  - spec/postmon_ruby/cep_finder_spec.rb
136
160
  - spec/postmon_ruby/city_finder_spec.rb
137
161
  - spec/postmon_ruby/client_spec.rb
162
+ - spec/postmon_ruby/ect_tracker_finder_spec.rb
163
+ - spec/postmon_ruby/state_finder_spec.rb
138
164
  - spec/spec_helper.rb