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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4f34da7f0b92b97623f600d4658ed8507f3b67c9
4
- data.tar.gz: 0db41cda4ecd1213132d0ceb6611447859275d13
3
+ metadata.gz: cb796bcde1965a278940828e0fd113e880217a7f
4
+ data.tar.gz: c7103ee570b9fbbbe0fd5516ed0a145c6dbb4569
5
5
  SHA512:
6
- metadata.gz: 3ef7758c075bc74a6b7ae8ccaba8138f7ce2c631f37b1f7ef91441494effe5eb2407a48b441130df69acbd694270bcc06a73f806f103446280e91a09a9b6c58f
7
- data.tar.gz: a42572c88d4fc72279051e776ff278af118bb1a2f60ebd2b37eda1d90c7f5bcd728f374806c1b17ec814677af23347fd334a417f640c58d3ad236b0d9ba004c9
6
+ metadata.gz: d4dacd52507ff0604a7c369112ecbb846b187e29c47e80a67b7082f0d44247ab8534a0ccd8b85f1fc896e220e942299064dac9f917ecd2d0634b4692a707d271
7
+ data.tar.gz: 82cddd49ac3272d75b583f3b83a0f7b3be8ce11f5930e6db7a7ffaceaac40723a2454c0ad96d616bfd0f4d974e1ba0d7beb48903c2a3cb3a9de9020abae0905a
data/.travis.yml CHANGED
@@ -2,7 +2,6 @@ rvm:
2
2
  - 1.9.3
3
3
  - 2.0.0
4
4
  - 2.1.1
5
- - rbx-2.2
6
5
  - jruby-19mode
7
6
  script: "bundle exec rake spec"
8
7
 
data/Gemfile CHANGED
@@ -3,7 +3,7 @@ source 'https://rubygems.org'
3
3
  # Specify your gem's dependencies in royal_mail.gemspec
4
4
  gemspec
5
5
 
6
- gem 'nokogiri', '~> 1.5'
6
+ gem 'mechanize', '~> 2.7'
7
7
 
8
8
  group :test do
9
9
  gem 'rspec'
@@ -1,5 +1,6 @@
1
1
  require "royal_mail_scraper/version"
2
2
  require "royal_mail_scraper/tracker"
3
+ require "royal_mail_scraper/tracker/errors"
3
4
  require "royal_mail_scraper/tracker/request"
4
5
  require "royal_mail_scraper/tracker/response"
5
6
  require "royal_mail_scraper/tracker/detail"
@@ -0,0 +1,3 @@
1
+ module RoyalMailScraper
2
+ class Error < StandardError; end
3
+ end
@@ -1,23 +1,68 @@
1
- require 'net/http'
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
- params = { tracking_number: tracking_number }
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
- req = Net::HTTP::Post.new(REQUEST_URI.request_uri)
12
- req.form_data = params
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
- http = Net::HTTP.new(REQUEST_URI.host, REQUEST_URI.port)
15
- http.open_timeout = TIMEOUT
16
- http.read_timeout = TIMEOUT
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
- res = http.request(req)
52
+ def with_retry(retries, exception_class, interval = 0.5)
53
+ attempt = 0
19
54
 
20
- Tracker::Response.new(res.body)
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
@@ -1,3 +1,3 @@
1
1
  module RoyalMailScraper
2
- VERSION = "1.0.0"
2
+ VERSION = "1.0.1"
3
3
  end
@@ -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 "nokogiri", "~> 1.5"
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(:http_response) { double(body: html) }
32
+ let(:page) { Mechanize::Page.new(URI('http://test'), nil, html) }
33
33
 
34
- before { expect_any_instance_of(Net::HTTP).to receive(:request).and_return(http_response) }
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.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-07-29 00:00:00.000000000 Z
11
+ date: 2014-11-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: nokogiri
14
+ name: mechanize
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ~>
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '1.5'
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: '1.5'
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: []