la_poste 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3550b84ed53d17953232302112645a2cb77cc14f
4
+ data.tar.gz: b652e65a8311e384c9fd4c333607b9618fcb8740
5
+ SHA512:
6
+ metadata.gz: 7caad6e8810e770a7c2c3bb4d11605f8e73a11ae7964978c73c1eb501a6c900bb117a541c646037f577bc1b8ebdf1ee87b1e495f69e30912c3115537ead268a0
7
+ data.tar.gz: a0ee622e16b63f75d66fb8ff2d8556635f786033736a2f72b2466e72969275463a9c70bf37cc0ffd6b3c5d21ab6067e43e02301530ee712a710251369543e9bb
data/.coveralls.yml ADDED
@@ -0,0 +1 @@
1
+ service_name: travis-ci
data/.gitignore ADDED
@@ -0,0 +1,23 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
23
+ .DS_Store
data/.travis.yml ADDED
@@ -0,0 +1,8 @@
1
+ language: ruby
2
+
3
+ rvm:
4
+ - 1.9.3
5
+ - 2.0.0
6
+ - 2.1.2
7
+
8
+ script: 'bundle exec rake'
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in la_poste.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Hugo Lantaume
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,49 @@
1
+ # LaPoste - A simple Ruby gem to fetch tracking information from LaPoste
2
+
3
+ [![Build Status](https://secure.travis-ci.org/hugolantaume/la_poste.png?branch=master)](http://travis-ci.org/hugolantaume/la_poste)
4
+ [![Gem Version](https://badge.fury.io/rb/la_poste.svg)](http://badge.fury.io/rb/la_poste)
5
+ [![Coverage Status](https://coveralls.io/repos/hugolantaume/la_poste/badge.png?branch=master)](https://coveralls.io/r/hugolantaume/la_poste?branch=master)
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'la_poste'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install la_poste
20
+
21
+ ## How to use LaPoste
22
+
23
+ To fetch information from [La Poste](http://www.part.csuivi.courrier.laposte.fr/suivi/index), use the LaPoste::Tracker class:
24
+
25
+ require 'la_poste'
26
+ tracker = LaPoste::Tracker.new("1A09920544793")
27
+
28
+ puts JSON::pretty_generate(tracker.data[:now])
29
+ {
30
+ "tracker": "1A09920544793",
31
+ "type": "Lettre Recommandée AR",
32
+ "date": "13/08/2014",
33
+ "zone": "75",
34
+ "status": "Distribué"
35
+ }
36
+
37
+ puts JSON::pretty_generate(tracker.data[:logs].first)
38
+ {
39
+ "date": "13/08/2014",
40
+ "event": "Distribué, PARIS DESNOUETTES (75)"
41
+ }
42
+
43
+ ## Contributing
44
+
45
+ 1. Fork it
46
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
47
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
48
+ 4. Push to the branch (`git push origin my-new-feature`)
49
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ require "rspec/core/rake_task"
2
+ require "bundler/gem_tasks"
3
+
4
+ # Default directory to look in is `/specs`
5
+ # Run with `rake spec`
6
+ desc "Run all examples"
7
+ RSpec::Core::RakeTask.new(:spec) do |t|
8
+ t.rspec_opts = %w[--color]
9
+ end
10
+
11
+ task :default => [:spec]
data/la_poste.gemspec ADDED
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'la_poste/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "la_poste"
8
+ spec.version = LaPoste::VERSION
9
+ spec.authors = ["Hugo Lantaume"]
10
+ spec.email = ["hugolantaume@gmail.com"]
11
+ spec.summary = %q{Retrieve tracking information from LaPoste. }
12
+ spec.description = ""
13
+ spec.homepage = "http://github.com/hugolantaume/la_poste"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "httparty"
22
+ spec.add_dependency "nokogiri"
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.6"
25
+ spec.add_development_dependency "rake"
26
+ spec.add_development_dependency "rspec"
27
+ spec.add_development_dependency "rspec-nc"
28
+ spec.add_development_dependency "coveralls"
29
+ end
@@ -0,0 +1,77 @@
1
+ # encoding: utf-8
2
+
3
+ module LaPoste
4
+ URL = "http://www.part.csuivi.courrier.laposte.fr/suivi/index"
5
+
6
+ class Tracker
7
+
8
+ AttrReaders = [
9
+ :now,
10
+ :logs
11
+ ]
12
+
13
+ AttrReaders.each do |meth|
14
+ define_method meth do
15
+ @data[meth.to_sym]
16
+ end
17
+ end
18
+
19
+ attr_reader :data
20
+
21
+ def initialize(tracking_id)
22
+ @data = nil
23
+ response = HTTParty.post( URL, body: { id: tracking_id.to_s })
24
+ html_doc = Nokogiri::HTML(response)
25
+
26
+ # get number of tracking items
27
+ table = html_doc.css(".resultat_rech_simple_table tbody")
28
+ n_items = table.css("tr").length / 2
29
+
30
+ if n_items > 0
31
+
32
+ @data = {
33
+ now: {},
34
+ logs: []
35
+ }
36
+
37
+ _status_row = table.css("tr:nth-child(1)")
38
+ _logs_row = table.css("tr:nth-child(2)")
39
+
40
+ # status
41
+ _tracking_id = _status_row.css("td:nth-child(1)").text.strip
42
+ _type = _status_row.css("td:nth-child(2)").text.strip
43
+ _date = _status_row.css("td:nth-child(3)").text.strip
44
+ _zone = _status_row.css("td:nth-child(4)").text.strip
45
+ _status = _status_row.css("td:nth-child(5)").text.strip
46
+
47
+ @data[:now] = {
48
+ tracking_id: _tracking_id,
49
+ type: _type,
50
+ date: _date,
51
+ zone: _zone,
52
+ status: _status
53
+ }
54
+
55
+ # logs
56
+ table_infos = table.css("td.table_information")
57
+ acheminement_infos = table_infos.css(".ach_courrier_box")
58
+ if !acheminement_infos.empty?
59
+ _date = acheminement_infos.css("span.ach_courrier_box_date").text
60
+ _status = acheminement_infos.css("span.ach_courrier_box_statut").text
61
+ _cp = acheminement_infos.css("span.ach_courrier_box_cp").text
62
+ @data[:logs] << { date: _date, event: "#{_status}, #{_cp}" }
63
+ end
64
+ wrapper_infos = table_infos.css(".table_information_wrapper")
65
+ n_logs_items = wrapper_infos.css(".table_information_date_etat").length
66
+ (1..n_logs_items).each do |it_log|
67
+ _log = wrapper_infos.css(".table_information_date_etat")[it_log-1]
68
+ _date = _log.css(".table_information_date").text.strip.gsub!("le\u00A0", "")
69
+ _etat = _log.css(".table_information_etat").text.strip
70
+ @data[:logs] << { date: _date, event: _etat}
71
+ end
72
+ end
73
+ rescue
74
+ @data = nil
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,3 @@
1
+ module LaPoste
2
+ VERSION = "0.0.1"
3
+ end
data/lib/la_poste.rb ADDED
@@ -0,0 +1,4 @@
1
+ require "httparty"
2
+ require "nokogiri"
3
+ require "la_poste/version"
4
+ require "la_poste/tracker"
@@ -0,0 +1,113 @@
1
+ # encoding: utf-8
2
+
3
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
4
+
5
+ describe LaPoste do
6
+
7
+ valid_tracker = LaPoste::Tracker.new("1A09920544793")
8
+ invalid_tracker = LaPoste::Tracker.new("")
9
+
10
+ describe "behavior" do
11
+
12
+ it "allows to create a valid LaPoste object from a valid tracking id" do
13
+ expect(valid_tracker.data).not_to be_nil
14
+ end
15
+
16
+ it "is an instance of the LaPoste::Tracker Class" do
17
+ expect(valid_tracker).to be_instance_of(LaPoste::Tracker)
18
+ end
19
+
20
+ it "has a Hash :data attribute" do
21
+ expect(valid_tracker).to respond_to(:data)
22
+ expect(valid_tracker.data).to be_a Hash
23
+ end
24
+
25
+ it "has a Hash :data attribute with valid keys" do
26
+ expect(valid_tracker.data).to have_key :now
27
+ expect(valid_tracker.data[:now]).to be_a Hash
28
+ expect(valid_tracker.data).to have_key :logs
29
+ expect(valid_tracker.data[:logs]).to be_a Array
30
+ end
31
+
32
+ it "has a subhash :now with valid keys" do
33
+ expect(valid_tracker.data[:now]).to have_key :tracking_id
34
+ expect(valid_tracker.data[:now]).to have_key :type
35
+ expect(valid_tracker.data[:now]).to have_key :date
36
+ expect(valid_tracker.data[:now]).to have_key :zone
37
+ expect(valid_tracker.data[:now]).to have_key :status
38
+ end
39
+
40
+ it "has a subarray with valid Hash keys" do
41
+ expect(valid_tracker.data[:logs].first).to be_a Hash
42
+ expect(valid_tracker.data[:logs].first).to have_key :date
43
+ expect(valid_tracker.data[:logs].first).to have_key :event
44
+ end
45
+
46
+ it "has a Hash :now attribute" do
47
+ expect(valid_tracker).to respond_to :now
48
+ expect(valid_tracker.now).to be_a Hash
49
+ expect(valid_tracker.now).to eq(valid_tracker.data[:now])
50
+ end
51
+
52
+ it "has an Array :logs attribute" do
53
+ expect(valid_tracker).to respond_to :logs
54
+ expect(valid_tracker.logs).to be_a Array
55
+ expect(valid_tracker.logs).to eq(valid_tracker.data[:logs])
56
+ end
57
+ end
58
+
59
+ describe "valid attributes example" do
60
+
61
+ it "has the right now.tracking_id attribute" do
62
+ expect(valid_tracker.now[:tracking_id]).to eq("1A09920544793")
63
+ end
64
+
65
+ it "has the right now.type attribute" do
66
+ expect(valid_tracker.now[:type]).to eq("Lettre Recommandée AR")
67
+ end
68
+
69
+ it "has the right now.date attribute" do
70
+ expect(valid_tracker.now[:date]).to eq("13/08/2014")
71
+ end
72
+
73
+ it "has the right now.zone attribute" do
74
+ expect(valid_tracker.now[:zone]).to eq("75")
75
+ end
76
+
77
+ it "has the right now.status attribute" do
78
+ expect(valid_tracker.now[:status]).to eq("Distribué")
79
+ end
80
+
81
+ it "has the right number of logs" do
82
+ expect(valid_tracker.logs.length).to eq(5)
83
+ end
84
+
85
+ it "has the right first log" do
86
+ expect(valid_tracker.logs.first).to be_a Hash
87
+ expect(valid_tracker.logs.first[:date]).to eq("13/08/2014")
88
+ expect(valid_tracker.logs.first[:event]).to eq("Distribué, PARIS DESNOUETTES (75)")
89
+ end
90
+ end
91
+
92
+ describe "invalid attributes example" do
93
+
94
+ it "has a nil :data attribute when the tracking_id is invalid" do
95
+ expect(invalid_tracker.data).to be_nil
96
+ end
97
+ end
98
+ end
99
+
100
+
101
+
102
+
103
+
104
+
105
+
106
+
107
+
108
+
109
+
110
+
111
+
112
+
113
+
@@ -0,0 +1,3 @@
1
+ require 'coveralls'
2
+ Coveralls.wear!
3
+ require 'la_poste'
metadata ADDED
@@ -0,0 +1,157 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: la_poste
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Hugo Lantaume
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-08-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: httparty
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: nokogiri
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.6'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.6'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec-nc
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: coveralls
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: ''
112
+ email:
113
+ - hugolantaume@gmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".coveralls.yml"
119
+ - ".gitignore"
120
+ - ".travis.yml"
121
+ - Gemfile
122
+ - LICENSE.txt
123
+ - README.md
124
+ - Rakefile
125
+ - la_poste.gemspec
126
+ - lib/la_poste.rb
127
+ - lib/la_poste/tracker.rb
128
+ - lib/la_poste/version.rb
129
+ - spec/la_poste_spec.rb
130
+ - spec/spec_helper.rb
131
+ homepage: http://github.com/hugolantaume/la_poste
132
+ licenses:
133
+ - MIT
134
+ metadata: {}
135
+ post_install_message:
136
+ rdoc_options: []
137
+ require_paths:
138
+ - lib
139
+ required_ruby_version: !ruby/object:Gem::Requirement
140
+ requirements:
141
+ - - ">="
142
+ - !ruby/object:Gem::Version
143
+ version: '0'
144
+ required_rubygems_version: !ruby/object:Gem::Requirement
145
+ requirements:
146
+ - - ">="
147
+ - !ruby/object:Gem::Version
148
+ version: '0'
149
+ requirements: []
150
+ rubyforge_project:
151
+ rubygems_version: 2.4.1
152
+ signing_key:
153
+ specification_version: 4
154
+ summary: Retrieve tracking information from LaPoste.
155
+ test_files:
156
+ - spec/la_poste_spec.rb
157
+ - spec/spec_helper.rb