jekyll-geocode 0.1 → 0.2

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.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +13 -7
  3. data/lib/jekyll_geocode.rb +63 -37
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0a800dd5ba8b97233bf43ad3cb8e47ba0dd2b7f4
4
- data.tar.gz: 0dcf7e2d742bfeec6e1174ac6ec8e7cde0a7ae55
3
+ metadata.gz: 865de4a807502167c5b10544e8a0e11bace6aced
4
+ data.tar.gz: 521cd82b9052eb92b35175dc60a8f9da5a7be517
5
5
  SHA512:
6
- metadata.gz: c448b250b9aa67f690f8613cb459bf8557a6180d98e41aba159fe8ef7f9c979f7949014d8a562843fe4e6c9e1ed063007b3fbbc5b2fb2152773c2df87f792051
7
- data.tar.gz: 8a3ca5bef05ccc9de67de4c8d4eeba1f6306a37804f8a284ee81e267210d594a2e032dcce029479929f856bebaf6d86265f7e8994e1ee20d22f3d0609b82b2f1
6
+ metadata.gz: 8865d2f87f4cc5a2def825745e4cb0578ed4b36ebe5a5643b42fc5b28cc99d80e864526b8fb03f8d904a545c6b74ff26493338982096f36698193f082cea4f78
7
+ data.tar.gz: 7041189666286518b889ba6a523a5204433409d05d0292ec8cffc2893219b54cbd82fa62efd807ddb6e551679980c8437c76ea6f40965035fcf5b1cdc2e29014
data/README.md CHANGED
@@ -12,17 +12,23 @@ I took the [Nominatim](https://nominatim.openstreetmap.org/) open source service
12
12
  ## Example of YAML : _data/members.yml
13
13
 
14
14
  ```yaml
15
+ - name: "Bertrand Keller"
16
+ street: "place du vieux marché"
17
+ postcode: "76000"
18
+ city: "Rouen"
19
+ region: "normandy"
20
+ country: "france"
15
21
  - name: "John Doe"
16
- address: "rue Mendès France"
17
- postcode: 76190
18
- city: "Yvetot"
22
+ street: "rue Mendès France"
23
+ postcode: "76190"
24
+ city: "Yvetot"
19
25
  region: "normandy"
20
26
  country: "france"
21
27
  - name: "Samuel Le Bihan"
22
- address: "place du général de Gaulle France"
23
- postcode: 76000
24
- city: "Rouen"
25
- region: "Normandy"
28
+ street: "Place Niemeyer"
29
+ city: "Le Havre"
30
+ postcode: "76600"
31
+ region: "normandy"
26
32
  country: "france"
27
33
  ```
28
34
 
@@ -4,11 +4,15 @@ require 'yaml'
4
4
  require 'open-uri'
5
5
  require "i18n"
6
6
 
7
- module Jekyll_Get
7
+ module Jekyll_Geocode
8
8
  class Generator < Jekyll::Generator
9
9
  safe true
10
10
  priority :highest
11
11
 
12
+ def request_service(url)
13
+ JSON.load(open(URI.encode(url)))
14
+ end
15
+
12
16
  def generate(site)
13
17
 
14
18
  #regEx for removing empty line
@@ -57,54 +61,76 @@ module Jekyll_Get
57
61
 
58
62
  # Loop YML file
59
63
  members.each do |d|
60
- # Test if a JSON file exists for performance issues
61
- if !File.file?("#{data_source}/#{d[geo_name]}.json")
62
- geo_name_field = d[geo_name].downcase.tr(" ", "-")
63
- if d[geo_postcode]
64
- geo_postcode_field = ",#{d[geo_postcode]}"
65
- end
66
- if d[geo_city]
67
- geo_city_field = ",#{d[geo_city]}"
68
- end
69
- if d[geo_region]
70
- geo_region_field = ",#{d[geo_region]}"
64
+ geo_name_field = d[geo_name].downcase.tr(" ", "-")
65
+ if d[geo_postcode]
66
+ geo_postcode_field = ", #{d[geo_postcode]}"
67
+ end
68
+ if d[geo_city]
69
+ geo_city_field = ", #{d[geo_city]}"
70
+ end
71
+ if d[geo_region]
72
+ geo_region_field = ", #{d[geo_region]}"
73
+ end
74
+ if d[geo_country]
75
+ geo_country_field = ", #{d[geo_country]}"
76
+ end
77
+ geo_coord = "#{d[geo_address]}#{geo_postcode_field}#{geo_city_field}#{geo_region_field}#{geo_country_field}"
78
+ geo_request = "#{geo_service}#{geo_coord}&limit=1"
79
+ p geo_request
80
+
81
+
82
+ # Loop for an YML output
83
+ if outputfile
84
+
85
+ # No cache at the first loop
86
+ geo_cache = true
87
+
88
+ # Read the YML file
89
+ if File.file?(path_yaml)
90
+ file_yaml = YAML.load(File.open(path_yaml))
71
91
  end
72
- if d[geo_country]
73
- geo_country_field = ",#{d[geo_country]}"
92
+
93
+ # If YML file test if and address are the same as in the source file
94
+ if file_yaml
95
+ file_yaml.each do |coordinates|
96
+ if coordinates['address'] == geo_coord
97
+ geo_cache = false
98
+ end
99
+ end
100
+ else
101
+ geo_cache = true
74
102
  end
75
- json = URI.encode("#{geo_service}#{d[geo_address]}#{geo_postcode_field}#{geo_city_field}#{geo_region_field}#{geo_country_field}&limit=1")
76
- source = JSON.load(open(json))
77
103
 
78
- # Loop for an YML output
79
- if outputfile
80
- source.each do |coordinates|
81
- data = [ "title" => "#{d[geo_name]}", "url" => "#places-01", "data_set" => "01", "location" => { "latitude" => "#{coordinates["lat"]}","longitude" => "#{coordinates["lon"]}" } ]
104
+ # Write data
105
+ if geo_cache == true
106
+ p "geocode is requesting #{geo_coord}"
107
+ request_service(geo_request).each do |coordinates|
108
+ data = [ "title" => "#{d[geo_name]}", "url" => "##{d[geo_name]}", "data_set" => "01", "location" => { "latitude" => "#{coordinates["lat"]}","longitude" => "#{coordinates["lon"]}" }, "address" => "#{geo_coord}" ]
82
109
  data_yml = data.to_yaml
83
- # Test if there is any yaml files and create file
110
+ # Add data in the YML file
84
111
  if !File.file?(path_yaml)
85
112
  File.open(path_yaml, "w") {|f| f.write(data_yml) }
86
113
  end
87
- # Test if there is yaml files and add data recursively
88
114
  if File.file?(path_yaml)
89
115
  File.open(path_yaml, 'a') { |f|
90
- data_yml_simple = data_yml.gsub("---", "").gsub(regEx, '')
91
- f.puts data_yml_simple
116
+ f.puts data_yml.gsub("---", "").gsub(regEx, '')
92
117
  }
93
- # Load YML file and remove duplicate entries
94
- file_yaml = YAML.load(File.open(path_yaml))
95
- file_yaml_uniq = file_yaml.uniq { |s| s.first }
96
- File.open(path_yaml, "w") {|f| f.write(file_yaml_uniq.to_yaml) }
118
+ # Load YML file => remove duplicated entries
119
+ file_yaml = YAML.load(File.open(path_yaml)).uniq { |s| s.first }
120
+ File.open(path_yaml, "w") {|f| f.write(file_yaml.to_yaml) }
97
121
  end
98
122
  end
99
- # Loop for an JSON output
100
- else
101
- site.data[geo_name_field] = source
102
- #Create a JSON files if cache is enabled
103
- if site.config['jekyll_geocode']['cache']
104
- path_json = "#{data_source}/#{geo_name_field}.json"
105
- open(path_json, 'wb') do |file|
106
- file << JSON.generate(site.data[geo_name_field])
107
- end
123
+ end
124
+ end
125
+
126
+ # JSON output :: Test if a JSON file exists for performance issues
127
+ if !outputfile && !File.file?("#{data_source}/#{d[geo_name]}.json")
128
+ site.data[geo_name_field] = request_service(geo_request)
129
+ #Create a JSON files if cache is enabled
130
+ if site.config['jekyll_geocode']['cache']
131
+ path_json = "#{data_source}/#{geo_name_field}.json"
132
+ open(path_json, 'wb') do |file|
133
+ file << JSON.generate(site.data[geo_name_field])
108
134
  end
109
135
  end
110
136
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jekyll-geocode
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.1'
4
+ version: '0.2'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bertrand Keller
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-10-14 00:00:00.000000000 Z
11
+ date: 2016-10-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jekyll