royal_mail_scraper 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +0 -1
- data/Gemfile +1 -1
- data/lib/royal_mail_scraper.rb +1 -0
- data/lib/royal_mail_scraper/tracker/errors.rb +3 -0
- data/lib/royal_mail_scraper/tracker/request.rb +54 -9
- data/lib/royal_mail_scraper/version.rb +1 -1
- data/royal_mail_scraper.gemspec +1 -1
- data/spec/tracker_spec.rb +6 -2
- metadata +16 -15
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cb796bcde1965a278940828e0fd113e880217a7f
|
4
|
+
data.tar.gz: c7103ee570b9fbbbe0fd5516ed0a145c6dbb4569
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d4dacd52507ff0604a7c369112ecbb846b187e29c47e80a67b7082f0d44247ab8534a0ccd8b85f1fc896e220e942299064dac9f917ecd2d0634b4692a707d271
|
7
|
+
data.tar.gz: 82cddd49ac3272d75b583f3b83a0f7b3be8ce11f5930e6db7a7ffaceaac40723a2454c0ad96d616bfd0f4d974e1ba0d7beb48903c2a3cb3a9de9020abae0905a
|
data/.travis.yml
CHANGED
data/Gemfile
CHANGED
data/lib/royal_mail_scraper.rb
CHANGED
@@ -1,23 +1,68 @@
|
|
1
|
-
require '
|
1
|
+
require 'mechanize'
|
2
2
|
|
3
3
|
module RoyalMailScraper
|
4
4
|
class Tracker::Request < Struct.new(:tracking_number)
|
5
5
|
REQUEST_URI = URI('http://www.royalmail.com/trackdetails')
|
6
6
|
TIMEOUT = 10
|
7
|
+
RETRIES_ON_ERROR = 7
|
7
8
|
|
8
9
|
def execute
|
9
|
-
|
10
|
+
Tracker::Response.new(fetch_details_page.body)
|
11
|
+
end
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
def fetch_details_page
|
16
|
+
form = with_retry(RETRIES_ON_ERROR, Error) { fetch_form }
|
17
|
+
|
18
|
+
form.tracking_number = tracking_number
|
10
19
|
|
11
|
-
|
12
|
-
|
20
|
+
result = with_retry(RETRIES_ON_ERROR, Error) { submit_form(form) }
|
21
|
+
|
22
|
+
result
|
23
|
+
end
|
24
|
+
|
25
|
+
def submit_form(form)
|
26
|
+
result = form.submit
|
27
|
+
find_form(result)
|
28
|
+
result
|
29
|
+
end
|
13
30
|
|
14
|
-
|
15
|
-
|
16
|
-
|
31
|
+
def fetch_form
|
32
|
+
find_form(agent.get(REQUEST_URI))
|
33
|
+
end
|
34
|
+
|
35
|
+
def find_form(page)
|
36
|
+
page.form_with(id: 'bt-tracked-track-trace-form') ||
|
37
|
+
raise(Error, 'Tracking code form not found')
|
38
|
+
end
|
39
|
+
|
40
|
+
def agent
|
41
|
+
@agent ||= build_agent
|
42
|
+
end
|
43
|
+
|
44
|
+
def build_agent
|
45
|
+
agent = Mechanize.new
|
46
|
+
agent.open_timeout = TIMEOUT
|
47
|
+
agent.read_timeout = TIMEOUT
|
48
|
+
agent.user_agent_alias = 'Windows Chrome'
|
49
|
+
agent
|
50
|
+
end
|
17
51
|
|
18
|
-
|
52
|
+
def with_retry(retries, exception_class, interval = 0.5)
|
53
|
+
attempt = 0
|
19
54
|
|
20
|
-
|
55
|
+
begin
|
56
|
+
attempt += 1
|
57
|
+
yield
|
58
|
+
rescue *exception_class => e
|
59
|
+
if attempt == retries
|
60
|
+
raise e
|
61
|
+
else
|
62
|
+
sleep interval
|
63
|
+
retry
|
64
|
+
end
|
65
|
+
end
|
21
66
|
end
|
22
67
|
end
|
23
68
|
end
|
data/royal_mail_scraper.gemspec
CHANGED
@@ -18,7 +18,7 @@ Gem::Specification.new do |spec|
|
|
18
18
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
19
|
spec.require_paths = ["lib"]
|
20
20
|
|
21
|
-
spec.add_dependency "
|
21
|
+
spec.add_dependency "mechanize", "~> 2.7"
|
22
22
|
spec.add_development_dependency "bundler", "~> 1.3"
|
23
23
|
spec.add_development_dependency "rake"
|
24
24
|
end
|
data/spec/tracker_spec.rb
CHANGED
@@ -29,9 +29,13 @@ describe RoyalMailScraper::Tracker do
|
|
29
29
|
context 'with mocked response' do
|
30
30
|
let(:tracking_number) { 'KF000000000GB' }
|
31
31
|
let(:html) { File.binread(File.dirname(__FILE__) + '/assets/' + file) }
|
32
|
-
let(:
|
32
|
+
let(:page) { Mechanize::Page.new(URI('http://test'), nil, html) }
|
33
33
|
|
34
|
-
before
|
34
|
+
before do
|
35
|
+
expect_any_instance_of(RoyalMailScraper::Tracker::Request).to(
|
36
|
+
receive(:fetch_details_page).and_return(page)
|
37
|
+
)
|
38
|
+
end
|
35
39
|
|
36
40
|
context 'collected' do
|
37
41
|
let(:file) { 'collected.html' }
|
metadata
CHANGED
@@ -1,55 +1,55 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: royal_mail_scraper
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Laurynas Butkus
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-11-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
14
|
+
name: mechanize
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - ~>
|
17
|
+
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '2.7'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - ~>
|
24
|
+
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
26
|
+
version: '2.7'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: bundler
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - ~>
|
31
|
+
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '1.3'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- - ~>
|
38
|
+
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '1.3'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rake
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- -
|
45
|
+
- - ">="
|
46
46
|
- !ruby/object:Gem::Version
|
47
47
|
version: '0'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- -
|
52
|
+
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
55
|
description: A simple page scraper for Royal Mail Track and Trace
|
@@ -59,8 +59,8 @@ executables: []
|
|
59
59
|
extensions: []
|
60
60
|
extra_rdoc_files: []
|
61
61
|
files:
|
62
|
-
- .gitignore
|
63
|
-
- .travis.yml
|
62
|
+
- ".gitignore"
|
63
|
+
- ".travis.yml"
|
64
64
|
- Gemfile
|
65
65
|
- LICENSE.txt
|
66
66
|
- README.md
|
@@ -68,6 +68,7 @@ files:
|
|
68
68
|
- lib/royal_mail_scraper.rb
|
69
69
|
- lib/royal_mail_scraper/tracker.rb
|
70
70
|
- lib/royal_mail_scraper/tracker/detail.rb
|
71
|
+
- lib/royal_mail_scraper/tracker/errors.rb
|
71
72
|
- lib/royal_mail_scraper/tracker/request.rb
|
72
73
|
- lib/royal_mail_scraper/tracker/response.rb
|
73
74
|
- lib/royal_mail_scraper/tracker/status_map.rb
|
@@ -90,12 +91,12 @@ require_paths:
|
|
90
91
|
- lib
|
91
92
|
required_ruby_version: !ruby/object:Gem::Requirement
|
92
93
|
requirements:
|
93
|
-
- -
|
94
|
+
- - ">="
|
94
95
|
- !ruby/object:Gem::Version
|
95
96
|
version: '0'
|
96
97
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
97
98
|
requirements:
|
98
|
-
- -
|
99
|
+
- - ">="
|
99
100
|
- !ruby/object:Gem::Version
|
100
101
|
version: '0'
|
101
102
|
requirements: []
|