geo_api 0.0.13

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: dabec2af76e68f5dc969bc7edf82e9a1edd9f303
4
+ data.tar.gz: d402d1ae5bd493c33e325683659f0ec088cf18e6
5
+ SHA512:
6
+ metadata.gz: 48856628437017b00b0153ec290cfe2e5bb2a3446658afcb72970f51bc95729390a16fbae2407e72da0b7f0fe58bfb18c74823ff4c1a231288feb1295edd8a06
7
+ data.tar.gz: e2ff76e5e705494a1555f535bb15f53e4be7a7fdd1339d13af4bdd7e5dade64a7507751a4a26c15a39a2ad2440a70c08ec0851a03f7a841bd3c407a42d4ae08b
data/.coveralls.yml ADDED
@@ -0,0 +1,2 @@
1
+ service_name: travis-ci
2
+ repo_token: osG15lSe7BWiSzgnBBZ4BvStAZVCMK3qm
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.0
4
+ services:
5
+ - mongodb
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
4
+
5
+ gem 'mongoid', '~> 4.0.0.beta1', github: 'mongoid/mongoid', ref: 'f9e6fdb'
6
+ gem 'bson_ext'
7
+ gem 'coveralls', require: false
data/Guardfile ADDED
@@ -0,0 +1,8 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard :rspec do
5
+ watch(%r{^spec/.+_spec\.rb$})
6
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
7
+ watch('spec/spec_helper.rb') { "spec" }
8
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 liuzelei
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,43 @@
1
+ # GeoApi
2
+
3
+ 位置接口,目前调用百度地理位置api获取格式化的地理位置数据.
4
+
5
+ [![Travis](https://travis-ci.org/liuzelei/geo_api.svg?branch=master)](https://travis-ci.org/liuzelei/geo_api)
6
+ [![Code Climate](https://codeclimate.com/github/liuzelei/geo_api.png)](https://codeclimate.com/github/liuzelei/geo_api)
7
+ [![Coverage Status](https://coveralls.io/repos/liuzelei/geo_api/badge.png)](https://coveralls.io/r/liuzelei/geo_api)
8
+
9
+ ## 安装
10
+
11
+ 添加以下代码到Gemfile:
12
+
13
+ gem 'geo_api', git: 'git@github.com:liuzelei/geo_api.git', tag: 'v0.0.5'
14
+
15
+ 配置代码:
16
+
17
+ GeoApi.setup do |config|
18
+ config.server = "http://api.map.baidu.com/geocoder/v2/"
19
+ config.key = "" #百度api的key
20
+ end
21
+
22
+ ## 使用
23
+
24
+ #### 获取某个字符串的格式化地址信息
25
+
26
+ GeoApi.LocationService.instance.get_location_from_string("中山公园上海")
27
+
28
+ #### 获取某个经纬度下的格式化地址信息
29
+
30
+ GeoApi.LocationService.instance.get_location_from_coordinate(12.34, 56.78, "bd09ll")
31
+
32
+ 地址的格式化结果是
33
+
34
+ databack["province"] = "上海市"
35
+ databack["city"] = "上海市"
36
+ databack["region"] = "浦东新区"
37
+ databack["detail"] = "灵岩路79弄-1号"
38
+ databack["latitude"] = "31.172419183453"
39
+ databack["longitude"] = "121.50055678999"
40
+
41
+ #### 将其他坐标转换为百度坐标
42
+
43
+ GeoApi::LocationService.instance.coord_to_baidu("121.4660492,31.2117575;121.4664967,31.2111130", "3", "5")
data/Rakefile ADDED
@@ -0,0 +1,5 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ task default: :spec
5
+ RSpec::Core::RakeTask.new
data/geo_api.gemspec ADDED
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'geo_api/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "geo_api"
8
+ spec.version = GeoApi::VERSION
9
+ spec.authors = ["liuzelei"]
10
+ spec.email = ["liuzelei@gmail.com"]
11
+ spec.description = '%q{基础地理位置服务}'
12
+ spec.summary = '%q{提供字符串解析,经纬度解析映射等}'
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_development_dependency "guard"
25
+ spec.add_development_dependency "guard-rspec"
26
+
27
+ spec.add_runtime_dependency "bson"
28
+ spec.add_runtime_dependency "mongoid"
29
+ end
@@ -0,0 +1,10 @@
1
+ # encoding: utf-8
2
+ module GeoApi
3
+ class InstallGenerator < Rails::Generators::Base
4
+ source_root File.expand_path('../templates', __FILE__)
5
+
6
+ def create_initializer_file
7
+ template 'initializer.rb', 'config/initializers/geo_api.rb'
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,5 @@
1
+ # encoding: utf-8
2
+ GeoApi.setup do |config|
3
+ config.server = "http://apis.map.qq.com/ws/geocoder/v1/"
4
+ config.key = ""
5
+ end
@@ -0,0 +1,20 @@
1
+ # encoding: utf-8
2
+ module GeoApi
3
+ class Configuration
4
+ def server
5
+ @server ||= "http://apis.map.qq.com/ws/geocoder/v1"
6
+ end
7
+
8
+ def server=(server)
9
+ @server = server
10
+ end
11
+
12
+ def key
13
+ @key ||= "test_key"
14
+ end
15
+
16
+ def key=(key)
17
+ @key = key
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,88 @@
1
+ # encoding: utf-8
2
+ require 'singleton'
3
+ require 'net/http'
4
+ require 'json'
5
+
6
+ module GeoApi
7
+ class LocationService
8
+ include Singleton
9
+
10
+ def get_location_from_string(location)
11
+ #params = { address: location }
12
+ #result = send_request(params)
13
+
14
+ unless location.blank?
15
+
16
+ formated_address = location.split(/,|-|;|>|:|\+|\^/)
17
+ databack = Hash.new
18
+ databack["province"] = formated_address[0] if formated_address.length > 0
19
+ databack["city"] = formated_address[1] if formated_address.length > 1
20
+ databack["region"] = formated_address[2] if formated_address.length > 2
21
+ databack["detail"] = formated_address[3] if formated_address.length > 3
22
+ databack["latitude"] = ""
23
+ databack["longitude"] = ""
24
+
25
+ if ["重庆市", "上海市", "北京市", "天津市"].include?(databack["province"])
26
+ databack["region"] = databack["city"]
27
+ databack["city"] = databack["province"]
28
+ end
29
+
30
+ return databack
31
+ else
32
+ return nil
33
+ end
34
+ end
35
+
36
+ def get_location_from_coordinate(latitude, longitude, coordtype = 5)
37
+ params = { location: "%s,%s" % [latitude, longitude], coordtype: coordtype, key: GeoApi.config.key }
38
+ result = send_request(params)
39
+ if result && result["status"] == 0 && result["result"]
40
+ databack = Hash.new
41
+ databack["address"] = result["result"]["address"]
42
+ databack["province"] = result["result"]["address_component"]["province"]
43
+ databack["city"] = result["result"]["address_component"]["city"]
44
+ databack["region"] = result["result"]["address_component"]["district"]
45
+ databack["detail"] = result["result"]["address_component"]["street"] + result["result"]["address_component"]["street_number"]
46
+ databack["latitude"] = result["result"]["location"]["lat"].to_s
47
+ databack["longitude"] = result["result"]["location"]["lng"].to_s
48
+
49
+ return databack
50
+ else
51
+ return nil
52
+ end
53
+ end
54
+
55
+ def get_coordinate_from_string(location, city = nil)
56
+ params = { address: location, region: city, key: GeoApi.config.key }
57
+ result = send_request(params)
58
+
59
+ if result["status"] == 0
60
+ return result["result"]
61
+ else
62
+ return nil
63
+ end
64
+ end
65
+
66
+
67
+ private
68
+ def send_request(params)
69
+ uri = URI(GeoApi.config.server)
70
+ params[:output] = 'json'
71
+ uri.query = URI.encode_www_form(params)
72
+ res = Net::HTTP.get_response(uri)
73
+ log = GeoApi::Models::LocationLog.new
74
+ log.url = uri
75
+ begin
76
+ log.request = JSON.parse(params.to_json)
77
+ rescue
78
+ log.raw_request = params
79
+ end
80
+ log.response = JSON.parse(res.body) if res.is_a?(Net::HTTPSuccess)# && !res.body.blank?
81
+ log.save
82
+ result = JSON.parse(res.body) if res.is_a?(Net::HTTPSuccess)# && !res.body.blank?
83
+
84
+ return result
85
+ end
86
+
87
+ end
88
+ end
@@ -0,0 +1,14 @@
1
+ require 'mongoid'
2
+ module GeoApi
3
+ module Models
4
+ class LocationLog
5
+ include Mongoid::Document
6
+
7
+ field :url, type: String
8
+ field :request, type: Hash
9
+ field :raw_request, type: String
10
+ field :response, type: Hash
11
+
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,4 @@
1
+ # encoding: utf-8
2
+ module GeoApi
3
+ VERSION = "0.0.13"
4
+ end
data/lib/geo_api.rb ADDED
@@ -0,0 +1,22 @@
1
+ # encoding: utf-8
2
+ require "geo_api/version"
3
+ require 'geo_api/location_service'
4
+ require 'geo_api/configuration'
5
+ require 'geo_api/models/location_log'
6
+ require 'logger'
7
+
8
+ module GeoApi
9
+ class << self
10
+ def setup
11
+ yield config
12
+ end
13
+
14
+ def config
15
+ @config ||= Configuration.new
16
+ end
17
+
18
+ def logger
19
+ @logger ||= Logger.new(STDOUT)
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,65 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe "test location" do
5
+ it "should get location by string" do
6
+ location = GeoApi::LocationService.instance.get_location_from_string("上海市-灵岩路-79弄1号")
7
+ expect(location["province"]).to eq("上海市")
8
+ expect(location["city"]).to eq("上海市")
9
+ #expect(location["region"]).to eq("浦东新区")
10
+
11
+ location = GeoApi::LocationService.instance.get_location_from_string("")
12
+ expect(location).to eq(nil)
13
+
14
+ location = GeoApi::LocationService.instance.get_location_from_string("青海省,西宁市,城北区")
15
+ expect(location["province"]).to eq("青海省")
16
+ expect(location["city"]).to eq("西宁市")
17
+ expect(location["region"]).to eq("城北区")
18
+
19
+ # location = GeoApi::LocationService.instance.get_location_from_string("城北区")
20
+ # expect(location["province"]).not_to eq("城北区")
21
+ end
22
+ end
23
+
24
+ describe "test location form coordinate" do
25
+
26
+ it "should get location by coordinate" do
27
+ latitude = "39.9834"
28
+ longitude = "116.3229"
29
+
30
+ location = GeoApi::LocationService.instance.get_location_from_coordinate(latitude, longitude, 5)
31
+ expect(location["province"]).to eq("北京市")
32
+ expect(location["city"]).to eq("北京市")
33
+ expect(location["region"]).to eq("海淀区")
34
+
35
+ latitude = "36.66"
36
+ longitude = "101.76"
37
+
38
+ location = GeoApi::LocationService.instance.get_location_from_coordinate(latitude, longitude, 5)
39
+ expect(location["province"]).to eq("青海省")
40
+ expect(location["city"]).to eq("西宁市")
41
+ expect(location["region"]).to eq("城北区")
42
+ end
43
+
44
+ it "should get coordinate from string" do
45
+ result = GeoApi::LocationService.instance.get_coordinate_from_string("思南路115弄")
46
+
47
+ expect(result["location"]).not_to be_empty
48
+
49
+ result = GeoApi::LocationService.instance.get_coordinate_from_string("思南路115弄","上海市")
50
+
51
+ expect(result["location"]).not_to be_empty
52
+
53
+ result = GeoApi::LocationService.instance.get_coordinate_from_string("")
54
+
55
+ expect(result).to eq(nil)
56
+
57
+
58
+ # 500.times do
59
+ # result = GeoApi::LocationService.instance.get_coordinate_from_string("思南路115弄")
60
+ # if result.nil?
61
+ # GeoApi.logger.debug("====失败!")
62
+ # end
63
+ # end
64
+ end
65
+ end
@@ -0,0 +1,50 @@
1
+ # encoding utf-8
2
+ require 'coveralls'
3
+ Coveralls.wear!
4
+
5
+ require 'bundler/setup'
6
+ require 'geo_api'
7
+ require 'mongoid'
8
+
9
+ Bundler.setup
10
+
11
+ ENV["RAILS_ENV"] ||= 'test'
12
+ ENV["MONGOID_SPEC_HOST"] ||= "localhost"
13
+ ENV["MONGOID_SPEC_PORT"] ||= "27017"
14
+
15
+ HOST = ENV["MONGOID_SPEC_HOST"]
16
+ PORT = ENV["MONGOID_SPEC_PORT"].to_i
17
+
18
+ def database_id
19
+ "sashimi_test"
20
+ end
21
+
22
+
23
+ CONFIG = {
24
+ sessions: {
25
+ default: {
26
+ database: database_id,
27
+ hosts: [ "#{HOST}:#{PORT}" ]
28
+ }
29
+ }
30
+ }
31
+
32
+ # Set the database that the spec suite connects to.
33
+ Mongoid.configure do |config|
34
+ config.load_configuration(CONFIG)
35
+ end
36
+
37
+ GeoApi.setup do |config|
38
+ # config.server = "http://api.map.baidu.com/geocoder/v2/"
39
+ # config.convert_server = "http://api.map.baidu.com/geoconv/v1/"
40
+ # config.key = "m48tmnl9BPwnhNOctmrVKMRa"
41
+
42
+ config.server = "http://apis.map.qq.com/ws/geocoder/v1/"
43
+ config.key = "NM2BZ-V3HRG-ZHMQO-IPJQD-Q375T-EPFRZ"
44
+ end
45
+
46
+ RSpec.configure do |config|
47
+ config.before(:each) do
48
+ # Mongoid.purge!
49
+ end
50
+ end
metadata ADDED
@@ -0,0 +1,162 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: geo_api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.13
5
+ platform: ruby
6
+ authors:
7
+ - liuzelei
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-04-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: guard
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: guard-rspec
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
+ - !ruby/object:Gem::Dependency
84
+ name: bson
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: mongoid
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: "%q{基础地理位置服务}"
112
+ email:
113
+ - liuzelei@gmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".coveralls.yml"
119
+ - ".gitignore"
120
+ - ".travis.yml"
121
+ - Gemfile
122
+ - Guardfile
123
+ - LICENSE.txt
124
+ - README.md
125
+ - Rakefile
126
+ - geo_api.gemspec
127
+ - lib/generators/geo_api/install_generator.rb
128
+ - lib/generators/geo_api/templates/initializer.rb
129
+ - lib/geo_api.rb
130
+ - lib/geo_api/configuration.rb
131
+ - lib/geo_api/location_service.rb
132
+ - lib/geo_api/models/location_log.rb
133
+ - lib/geo_api/version.rb
134
+ - spec/location_spec.rb
135
+ - spec/spec_helper.rb
136
+ homepage: ''
137
+ licenses:
138
+ - MIT
139
+ metadata: {}
140
+ post_install_message:
141
+ rdoc_options: []
142
+ require_paths:
143
+ - lib
144
+ required_ruby_version: !ruby/object:Gem::Requirement
145
+ requirements:
146
+ - - ">="
147
+ - !ruby/object:Gem::Version
148
+ version: '0'
149
+ required_rubygems_version: !ruby/object:Gem::Requirement
150
+ requirements:
151
+ - - ">="
152
+ - !ruby/object:Gem::Version
153
+ version: '0'
154
+ requirements: []
155
+ rubyforge_project:
156
+ rubygems_version: 2.4.5
157
+ signing_key:
158
+ specification_version: 4
159
+ summary: "%q{提供字符串解析,经纬度解析映射等}"
160
+ test_files:
161
+ - spec/location_spec.rb
162
+ - spec/spec_helper.rb