sora_geocoding 0.1.0 → 0.2.0

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
  SHA256:
3
- metadata.gz: cccbe927ebc316c0b92d678bb685dfa4be47613aa6f1c2c002b383cae68e21fa
4
- data.tar.gz: ab1b13c1e34e5344a802f79ce827621d2ae0d4085b7477fc173181c65adb07d4
3
+ metadata.gz: 49a29a7ba7a53ef21bd7959e05d0f0b3381891f52970dc41b0e7ef20506034aa
4
+ data.tar.gz: 5260b9777d6b6a2a280903ecdd24bbea597d2122fbde0d1432e36392d7d633e1
5
5
  SHA512:
6
- metadata.gz: 3b2f09cac420de0006952689898560f28584314657e56f5e6866f6d8d34f0ae315d35c815217cf0faa0849a959418cd9ef96b4043e7c3991c6a3c9fc6bcd2e04
7
- data.tar.gz: b9a326d9f73e1e6a37351fdff724cca4b0906ad86936b5b3f58038b4b8eee42fb340e449b686a585a02c6d4f4d6e2026890c79ef8ffed305e3256190295b702c
6
+ metadata.gz: be6193b493832c887e824d2b35470830a0303309cd8e78c0516c9bfdb621bc71512af9bf6ffe32671894308899cab2106f803e4ccdd053bd6686ff574883d190
7
+ data.tar.gz: 59038b5a336e3705fbd0fcb25cadf30d3a68d131294bfd89a3185fead48ee50ad26544e40c9faa18e53791204384acb3d619217f7b8800523201a06311e0c161
@@ -0,0 +1,30 @@
1
+ # This workflow uses actions that are not certified by GitHub.
2
+ # They are provided by a third-party and are governed by
3
+ # separate terms of service, privacy policy, and support
4
+ # documentation.
5
+ # This workflow will download a prebuilt Ruby version, install dependencies and run tests with Rake
6
+ # For more information see: https://github.com/marketplace/actions/setup-ruby-jruby-and-truffleruby
7
+
8
+ name: Ruby
9
+
10
+ on:
11
+ push:
12
+ branches: [ master ]
13
+ pull_request:
14
+ branches: [ master ]
15
+
16
+ jobs:
17
+ test:
18
+
19
+ runs-on: ubuntu-latest
20
+
21
+ steps:
22
+ - uses: actions/checkout@v2
23
+ - name: Set up Ruby
24
+ uses: ruby/setup-ruby@v1
25
+ with:
26
+ ruby-version: 2.6
27
+ - name: Install dependencies
28
+ run: bundle install
29
+ - name: Run tests
30
+ run: bundle exec rake
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- sora_geocoding (0.1.0)
4
+ sora_geocoding (0.2.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/Guardfile CHANGED
@@ -1,5 +1,6 @@
1
1
  guard :rspec, cmd: 'bundle exec rspec' do
2
2
  watch(%r{^spec/.+_spec\.rb$})
3
- watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
3
+ watch(%r{^lib/sora_geocoding/(.+)\.rb$}) { |m| "spec/unit/#{m[1]}_spec.rb" }
4
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/unit/#{m[1]}_spec.rb" }
4
5
  watch('spec/spec_helper.rb') { 'spec' }
5
6
  end
data/README.md CHANGED
@@ -1,9 +1,5 @@
1
1
  # SoraGeocoding
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/sora_geocoding`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
6
-
7
3
  ## Installation
8
4
 
9
5
  Add this line to your application's Gemfile:
@@ -60,15 +56,38 @@ SoraGeocoding.coordinates('東京都', {site: 'yahoo', yahoo_appid: 'xxxxxxxxx'}
60
56
  => {:site=>"yahoo", :coordinates=>{:lat=>"35.64657460", :lon=>"139.65324950"}}
61
57
  ```
62
58
 
59
+ ### Geohash
60
+ - Specify the latitude and longitude
61
+ ```
62
+ SoraGeocoding.geohash("35.676192", "139.650311")
63
+ => "xn76cydkbh5h"
64
+ ```
65
+
66
+ ### Some Options
67
+ | option | Description | Default |
68
+ |:------------:|:-----------------------------------------|:---------------|
69
+ | timeout | timeout (secs) | 3 |
70
+ | http_headers | HTTP headers | {} |
71
+ | use_https | use HTTPS for requests? | false |
72
+ | http_proxy | HTTP proxy server (user:pass@host:port) | nil |
73
+ | https_proxy | HTTPS proxy server (user:pass@host:port) | nil |
74
+ | basic_auth | user and password for basic auth ({:user => "user", :password => "password"}) | {} |
75
+ | site | Specify 'geocoding' or 'yahoo' | nil |
76
+ | yahoo_appid | API key for Yahoo Geocoder API | nil |
77
+ | logger_level | log level, if kernel logger is used | ::Logger::WARN |
78
+ | always_raise | Always handle errors. ex) :all, SocketError, Timeout::Error | [] |
79
+
63
80
  ## Development
64
81
 
65
- 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.
82
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bundle exec rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
83
+
84
+ If you want to modify the source code while checking the tests, run the `bundle exec guard` in another console.
66
85
 
67
86
  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).
68
87
 
69
88
  ## Contributing
70
89
 
71
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/sora_geocoding. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
90
+ Bug reports and pull requests are welcome on GitHub at https://github.com/sorainc/sora_geocoding. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
72
91
 
73
92
  ## License
74
93
 
@@ -76,7 +95,8 @@ The gem is available as open source under the terms of the [MIT License](https:/
76
95
 
77
96
  ## Code of Conduct
78
97
 
79
- Everyone interacting in the SoraGeocoding project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/sora_geocoding/blob/master/CODE_OF_CONDUCT.md).
98
+ Everyone interacting in the SoraGeocoding project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/sorainc/sora_geocoding/blob/master/CODE_OF_CONDUCT.md).
80
99
 
81
100
  ## Reference
82
101
  - https://github.com/alexreisner/geocoder
102
+ - https://github.com/masuidrive/pr_geohash
@@ -6,6 +6,7 @@ require 'sora_geocoding/base'
6
6
  require 'sora_geocoding/query'
7
7
  require 'sora_geocoding/url'
8
8
  require 'sora_geocoding/request'
9
+ require 'sora_geocoding/geohash'
9
10
  require 'sora_geocoding/results/geocoding'
10
11
  require 'sora_geocoding/results/yahoo_geocoder'
11
12
 
@@ -32,6 +33,14 @@ module SoraGeocoding
32
33
  { site: results[:site], coordinates: site_specific_coordinates(results[:site], results[:data]) }
33
34
  end
34
35
 
36
+ #
37
+ # Generate a Geohash from latitude and longitude.
38
+ #
39
+ def self.geohash(latitude, longitude)
40
+ geohash = Geohash.new(latitude, longitude)
41
+ geohash.encode
42
+ end
43
+
35
44
  class << self
36
45
  private
37
46
  def site_specific_coordinates(site, data)
@@ -0,0 +1,88 @@
1
+ require 'open-uri'
2
+ require 'sora_geocoding/base'
3
+
4
+ module SoraGeocoding
5
+ #
6
+ # generate Geohash
7
+ # - https://ja.wikipedia.org/wiki/%E3%82%B8%E3%82%AA%E3%83%8F%E3%83%83%E3%82%B7%E3%83%A5
8
+ #
9
+ class Geohash < Base
10
+ BASE32 = '0123456789bcdefghjkmnpqrstuvwxyz'.freeze
11
+ REFINED_RANGE = [[-90, 90], [-180, 180]].freeze
12
+
13
+ def initialize(lat, lon)
14
+ @lat = lat
15
+ @lon = lon
16
+ end
17
+
18
+ def encode
19
+ calc_geohash(
20
+ convert_byte(@lat, REFINED_RANGE[0][0], REFINED_RANGE[0][1], range(@lat)),
21
+ convert_byte(@lon, REFINED_RANGE[1][0], REFINED_RANGE[1][1], range(@lon))
22
+ )
23
+ end
24
+
25
+ private
26
+ def range(val)
27
+ size = calc_size(val)
28
+ range = calc_range(size)
29
+ max_range(range)
30
+ end
31
+
32
+ def max_range(range)
33
+ [range, calc_range(-6)].max
34
+ end
35
+
36
+ def calc_range(size)
37
+ 10**size
38
+ end
39
+
40
+ def calc_size(val)
41
+ val.to_s.split('.')[-1].size * -1
42
+ end
43
+
44
+ def convert_byte(val, min, max, range)
45
+ bits = []
46
+ while (min - max).abs > range
47
+ mid = calc_mid(min, max)
48
+ bit = calc_bit(val, mid)
49
+
50
+ if bit.zero?
51
+ max = mid
52
+ else
53
+ min = mid
54
+ end
55
+
56
+ bits.push bit
57
+ end
58
+ bits.join
59
+ end
60
+
61
+ def calc_mid(min, max)
62
+ (min + max).to_f / 2
63
+ end
64
+
65
+ def calc_bit(val, mid)
66
+ val.to_f > mid ? 1 : 0
67
+ end
68
+
69
+ def calc_geohash(lat, lon)
70
+ bytes = split_bits(lat, lon)
71
+ bytes[-1] = bytes[-1].ljust(5, '0')
72
+ bytes.map { |v| BASE32[convert_to_decimal(v), 1] }.join
73
+ end
74
+
75
+ def integrate_bits(odd_bit, even_bit)
76
+ even_bit.chars.zip(odd_bit.chars).flatten.join
77
+ end
78
+
79
+ def split_bits(odd_bit, even_bit)
80
+ bytes = integrate_bits(odd_bit, even_bit)
81
+ bytes.chars.each_slice(5).map(&:join)
82
+ end
83
+
84
+ def convert_to_decimal(val)
85
+ "0b#{val}".to_i(0)
86
+ end
87
+ end
88
+ end
@@ -1,3 +1,3 @@
1
1
  module SoraGeocoding
2
- VERSION = '0.1.0'.freeze
2
+ VERSION = '0.2.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sora_geocoding
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - hirontan
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-08-05 00:00:00.000000000 Z
11
+ date: 2020-08-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -62,6 +62,7 @@ executables: []
62
62
  extensions: []
63
63
  extra_rdoc_files: []
64
64
  files:
65
+ - ".github/workflows/ruby.yml"
65
66
  - ".gitignore"
66
67
  - ".rspec"
67
68
  - ".rubocop.yml"
@@ -83,6 +84,7 @@ files:
83
84
  - lib/sora_geocoding/base.rb
84
85
  - lib/sora_geocoding/configuration.rb
85
86
  - lib/sora_geocoding/exceptions.rb
87
+ - lib/sora_geocoding/geohash.rb
86
88
  - lib/sora_geocoding/logger.rb
87
89
  - lib/sora_geocoding/query.rb
88
90
  - lib/sora_geocoding/request.rb