raktojson 0.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7aca4689bf63d5f5d91e785f42c691bc41febb83
4
+ data.tar.gz: d3a31285966d08d88cd92ff740aa367d49c6ec72
5
+ SHA512:
6
+ metadata.gz: 7e51a9629bccb84ff401966e59e84fc491364810e8128d9ba334b42970a5cbc9f8ee6533575b483d90c6fbd96ab2792f70a837198744eedc335676a147520120
7
+ data.tar.gz: c21493a1f0ea4e5f15dea592bf2334c30f42b208a80e166ca719efd3d1a30ed6ad415662fd23970e46c754aa808d4f6153a397a00c958266c260997801b71807
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
4
+ before_install: gem install bundler -v 1.10.5
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in raktojson.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Maxime Mouchet
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,27 @@
1
+ # :pizza: RAKToJSON
2
+
3
+ Parser for Télécom Bretagne restaurant menus.
4
+
5
+ ## Installation
6
+
7
+ $ brew install pdftohtml # If needed
8
+ $ gem install raktojson
9
+
10
+ ## Usage
11
+
12
+ $ raktojson [--url URL]
13
+
14
+ ## Development
15
+
16
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
17
+
18
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
19
+
20
+ ## Contributing
21
+
22
+ Bug reports and pull requests are welcome on GitHub at https://github.com/maxmouchet/raktojson.
23
+
24
+ ## License
25
+
26
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
27
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "raktojson"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require 'pry'
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,13 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Install pdftohtml if needed
8
+ which -s pdftohtml
9
+
10
+ if [ $? != 1 ]; then
11
+ echo 'pdftohtml not found. Installing...'
12
+ brew install pdftohtml
13
+ fi
data/exe/raktojson ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+
5
+ require 'raktojson'
6
+ require 'raktojson/cli'
7
+
8
+ RAKToJSON::CLI.start(ARGV)
@@ -0,0 +1,49 @@
1
+ require 'raktojson/week_list_parser'
2
+ require 'raktojson/xml_menu_parser'
3
+
4
+ require 'optparse'
5
+ require 'open-uri'
6
+ require 'securerandom'
7
+
8
+ module RAKToJSON
9
+ class CLI
10
+
11
+ def self.start(argv)
12
+ options = {}
13
+ OptionParser.new do |opts|
14
+ opts.banner = 'Usage: raktojson [options]'
15
+
16
+ opts.on('--url=URL', 'Parse the menu(s) for URL') do |url|
17
+ options[:url] = url
18
+ end
19
+
20
+ end.parse(argv)
21
+
22
+ def download_pdf(link, &block)
23
+ destination = "/tmp/#{ SecureRandom.hex(16) }.pdf"
24
+ `curl -o #{ destination } #{ link } 2&>/dev/null`
25
+ block.call(destination)
26
+ File.delete(destination)
27
+ end
28
+
29
+ options[:url] ||= 'http://services.telecom-bretagne.eu/rak/fr/menus_restaurant/'
30
+
31
+ if options[:url].end_with?('.pdf')
32
+ $stderr.puts "Downloading menu from #{ options[:url] }"
33
+ links = [ options[:url] ]
34
+ else
35
+ $stderr.puts "Downloading menus from #{ options[:url] }"
36
+ links = WeekListParser.parse(options[:url])
37
+ end
38
+
39
+ $stderr.puts "#{ links.count } menus to parse"
40
+ links.each do |link|
41
+ download_pdf(link) do |pdf|
42
+ xml_menu = `pdftohtml -xml -stdout -enc UTF-8 #{ pdf }`
43
+ puts XMLMenuParser.parse(xml_menu)
44
+ end
45
+ end
46
+ end
47
+
48
+ end
49
+ end
@@ -0,0 +1,3 @@
1
+ module RAKToJSON
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,22 @@
1
+ # coding: utf-8
2
+ require 'oga'
3
+
4
+ module RAKToJSON
5
+ class WeekListParser
6
+ class << self
7
+
8
+ def parse(html_string)
9
+ document = Oga.parse_html(html_string)
10
+ nodes = document.xpath("//blockquote//a['href']")
11
+
12
+ links = []
13
+ nodes.each do |node|
14
+ links << 'http://services.telecom-bretagne.eu' + node.get('href')
15
+ end
16
+
17
+ links
18
+ end
19
+
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,82 @@
1
+ # coding: utf-8
2
+ require 'oga'
3
+ require 'date'
4
+
5
+ module RAKToJSON
6
+ class XMLMenuParser
7
+ class << self
8
+
9
+ def parse(xml_string)
10
+ document = Oga.parse_xml(xml_string)
11
+ nodes = document.xpath("//page[@number='1']/text[@left]")
12
+
13
+ start_date = parse_start_date(nodes)
14
+
15
+ columns = sort_by_columns(nodes)
16
+ days = columns_to_days(columns, start_date)
17
+
18
+ make_menus(days)
19
+ end
20
+
21
+ private
22
+
23
+ # Sort nodes by columns
24
+ def sort_by_columns nodes
25
+ columns = Array.new(approximated_center_of(nodes[6]))
26
+
27
+ nodes.each do |node|
28
+ next if node.text.start_with?('MENU PREVISIONNEL')
29
+
30
+ columns[approximated_center_of(node)] ||= []
31
+ if (node.text[0] == node.text[0].upcase) && (node.text[0] != 'à') then
32
+ columns[approximated_center_of(node)] << node.text.capitalize
33
+ else
34
+ columns[approximated_center_of(node)].last << " " << node.text
35
+ end
36
+ end
37
+
38
+ columns.compact
39
+ end
40
+
41
+ # Map columns to days
42
+ def columns_to_days columns, start_date
43
+ days = {}
44
+ columns.each_index { |index| days[(start_date + index).iso8601] = columns[index][1..-1] }
45
+ days
46
+ end
47
+
48
+ # Separate lunch from dinner
49
+ def make_menus days
50
+ menus = {}
51
+ days.map do |k,v|
52
+ lunch_index = v.index('Midi')
53
+ dinner_index = v.index('Soir')
54
+
55
+ next if lunch_index.nil? || dinner_index.nil?
56
+
57
+ menus[k] = {
58
+ lunch: v[lunch_index+1..dinner_index-1],
59
+ dinner: v[dinner_index+1..-1]
60
+ }
61
+ end
62
+
63
+ menus
64
+ end
65
+
66
+ def parse_start_date nodes
67
+ matches = /(?<start_day>\d{1,2})\.(?<start_month>\d{1,2})[\s\w]+(?<end_day>\d{1,2})\.(?<end_month>\d{1,2})\.(?<year>\d+)/.match(nodes.last.text)
68
+
69
+ Date.new(matches['year'].to_i, matches['start_month'].to_i, matches['start_day'].to_i)
70
+ end
71
+
72
+ def center_of node
73
+ node.get('left').to_f + (node.get('width').to_f / 2)
74
+ end
75
+
76
+ def approximated_center_of node
77
+ (center_of(node) / 10).to_i
78
+ end
79
+
80
+ end
81
+ end
82
+ end
data/lib/raktojson.rb ADDED
@@ -0,0 +1,3 @@
1
+ require 'raktojson/version'
2
+
3
+ module RAKToJSON; end
data/raktojson.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'raktojson/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "raktojson"
8
+ spec.version = RAKToJSON::VERSION
9
+ spec.authors = ["Maxime Mouchet"]
10
+ spec.email = ["max@maxmouchet.com"]
11
+
12
+ spec.summary = "Télécom Bretagne restaurant menus parser"
13
+ spec.homepage = "https://github.com/maxmouchet/raktojson"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "oga"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.10"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "rspec"
26
+ spec.add_development_dependency "cucumber"
27
+ end
metadata ADDED
@@ -0,0 +1,131 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: raktojson
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Maxime Mouchet
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-07-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: oga
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: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.10'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.10'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
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: cucumber
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
+ description:
84
+ email:
85
+ - max@maxmouchet.com
86
+ executables:
87
+ - raktojson
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".gitignore"
92
+ - ".rspec"
93
+ - ".travis.yml"
94
+ - Gemfile
95
+ - LICENSE.txt
96
+ - README.md
97
+ - Rakefile
98
+ - bin/console
99
+ - bin/setup
100
+ - exe/raktojson
101
+ - lib/raktojson.rb
102
+ - lib/raktojson/cli.rb
103
+ - lib/raktojson/version.rb
104
+ - lib/raktojson/week_list_parser.rb
105
+ - lib/raktojson/xml_menu_parser.rb
106
+ - raktojson.gemspec
107
+ homepage: https://github.com/maxmouchet/raktojson
108
+ licenses:
109
+ - MIT
110
+ metadata: {}
111
+ post_install_message:
112
+ rdoc_options: []
113
+ require_paths:
114
+ - lib
115
+ required_ruby_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ required_rubygems_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ requirements: []
126
+ rubyforge_project:
127
+ rubygems_version: 2.4.5
128
+ signing_key:
129
+ specification_version: 4
130
+ summary: Télécom Bretagne restaurant menus parser
131
+ test_files: []