geo_api 0.0.13 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: dabec2af76e68f5dc969bc7edf82e9a1edd9f303
4
- data.tar.gz: d402d1ae5bd493c33e325683659f0ec088cf18e6
3
+ metadata.gz: 4f55ed5220b1ed6a8386182489524d2321644003
4
+ data.tar.gz: 3ad607c47d03b4e4601a20c6b6add414cc11fafb
5
5
  SHA512:
6
- metadata.gz: 48856628437017b00b0153ec290cfe2e5bb2a3446658afcb72970f51bc95729390a16fbae2407e72da0b7f0fe58bfb18c74823ff4c1a231288feb1295edd8a06
7
- data.tar.gz: e2ff76e5e705494a1555f535bb15f53e4be7a7fdd1339d13af4bdd7e5dade64a7507751a4a26c15a39a2ad2440a70c08ec0851a03f7a841bd3c407a42d4ae08b
6
+ metadata.gz: 5ad3562bda56c8172cef6ad223a0b2c1cb7f651b7b949558e030570dd03bc7e3fe82c62d19bfd8bb4c0fa0efb52ed37e17fb9e34998f1fe4cc5bb2112131445b
7
+ data.tar.gz: 86a3f9ca3f6e1b6764e270955f27e969b4679c760357adb90cc506dac4e99d7c545fbe0887fdbc0cb2c50b2a226ab98052c24319351b4eebc187a08ef9329608
data/geo_api.gemspec CHANGED
@@ -8,8 +8,8 @@ Gem::Specification.new do |spec|
8
8
  spec.version = GeoApi::VERSION
9
9
  spec.authors = ["liuzelei"]
10
10
  spec.email = ["liuzelei@gmail.com"]
11
- spec.description = '%q{基础地理位置服务}'
12
- spec.summary = '%q{提供字符串解析,经纬度解析映射等}'
11
+ spec.description = '基础地理位置服务'
12
+ spec.summary = '提供字符串解析,经纬度解析映射等'
13
13
  spec.homepage = ""
14
14
  spec.license = "MIT"
15
15
 
@@ -1,7 +1,7 @@
1
1
  require 'mongoid'
2
2
  module GeoApi
3
3
  module Models
4
- class LocationLog
4
+ class Log
5
5
  include Mongoid::Document
6
6
 
7
7
  field :url, type: String
@@ -1,4 +1,4 @@
1
1
  # encoding: utf-8
2
2
  module GeoApi
3
- VERSION = "0.0.13"
3
+ VERSION = "0.1.0"
4
4
  end
data/lib/geo_api.rb CHANGED
@@ -1,9 +1,10 @@
1
1
  # encoding: utf-8
2
2
  require "geo_api/version"
3
- require 'geo_api/location_service'
4
3
  require 'geo_api/configuration'
5
- require 'geo_api/models/location_log'
4
+ require 'geo_api/models/log'
6
5
  require 'logger'
6
+ require 'net/http'
7
+ require 'json'
7
8
 
8
9
  module GeoApi
9
10
  class << self
@@ -18,5 +19,77 @@ module GeoApi
18
19
  def logger
19
20
  @logger ||= Logger.new(STDOUT)
20
21
  end
22
+
23
+ def get_location_from_string(location)
24
+ unless location.blank?
25
+ formated_address = location.split(/,|-|;|>|:|\+|\^/)
26
+ databack = Hash.new
27
+ databack["province"] = formated_address[0] if formated_address.length > 0
28
+ databack["city"] = formated_address[1] if formated_address.length > 1
29
+ databack["region"] = formated_address[2] if formated_address.length > 2
30
+ databack["detail"] = formated_address[3] if formated_address.length > 3
31
+ databack["latitude"] = ""
32
+ databack["longitude"] = ""
33
+
34
+ if ["重庆市", "上海市", "北京市", "天津市"].include?(databack["province"])
35
+ databack["region"] = databack["city"]
36
+ databack["city"] = databack["province"]
37
+ end
38
+
39
+ return databack
40
+ else
41
+ return nil
42
+ end
43
+ end
44
+
45
+ def get_location_from_coordinate(latitude, longitude, coordtype = 5)
46
+ params = { location: "%s,%s" % [latitude, longitude], coordtype: coordtype, key: GeoApi.config.key }
47
+ result = send_request(params)
48
+ if result && result["status"] == 0 && result["result"]
49
+ databack = Hash.new
50
+ databack["address"] = result["result"]["address"]
51
+ databack["province"] = result["result"]["address_component"]["province"]
52
+ databack["city"] = result["result"]["address_component"]["city"]
53
+ databack["region"] = result["result"]["address_component"]["district"]
54
+ databack["detail"] = result["result"]["address_component"]["street"] + result["result"]["address_component"]["street_number"]
55
+ databack["latitude"] = result["result"]["location"]["lat"].to_s
56
+ databack["longitude"] = result["result"]["location"]["lng"].to_s
57
+
58
+ return databack
59
+ else
60
+ return nil
61
+ end
62
+ end
63
+
64
+ def get_coordinate_from_string(location, city = nil)
65
+ params = { address: location, region: city, key: GeoApi.config.key }
66
+ result = send_request(params)
67
+
68
+ if result["status"] == 0
69
+ return result["result"]
70
+ else
71
+ return nil
72
+ end
73
+ end
74
+
75
+ private
76
+ def send_request(params)
77
+ uri = URI(GeoApi.config.server)
78
+ params[:output] = 'json'
79
+ uri.query = URI.encode_www_form(params)
80
+ res = Net::HTTP.get_response(uri)
81
+ log = GeoApi::Models::Log.new
82
+ log.url = uri
83
+ begin
84
+ log.request = JSON.parse(params.to_json)
85
+ rescue
86
+ log.raw_request = params
87
+ end
88
+ log.response = JSON.parse(res.body) if res.is_a?(Net::HTTPSuccess)# && !res.body.blank?
89
+ log.save
90
+ result = JSON.parse(res.body) if res.is_a?(Net::HTTPSuccess)# && !res.body.blank?
91
+
92
+ return result
93
+ end
21
94
  end
22
95
  end
@@ -3,20 +3,20 @@ require 'spec_helper'
3
3
 
4
4
  describe "test location" do
5
5
  it "should get location by string" do
6
- location = GeoApi::LocationService.instance.get_location_from_string("上海市-灵岩路-79弄1号")
6
+ location = GeoApi.get_location_from_string("上海市-灵岩路-79弄1号")
7
7
  expect(location["province"]).to eq("上海市")
8
8
  expect(location["city"]).to eq("上海市")
9
9
  #expect(location["region"]).to eq("浦东新区")
10
10
 
11
- location = GeoApi::LocationService.instance.get_location_from_string("")
11
+ location = GeoApi.get_location_from_string("")
12
12
  expect(location).to eq(nil)
13
13
 
14
- location = GeoApi::LocationService.instance.get_location_from_string("青海省,西宁市,城北区")
14
+ location = GeoApi.get_location_from_string("青海省,西宁市,城北区")
15
15
  expect(location["province"]).to eq("青海省")
16
16
  expect(location["city"]).to eq("西宁市")
17
17
  expect(location["region"]).to eq("城北区")
18
18
 
19
- # location = GeoApi::LocationService.instance.get_location_from_string("城北区")
19
+ # location = GeoApi.get_location_from_string("城北区")
20
20
  # expect(location["province"]).not_to eq("城北区")
21
21
  end
22
22
  end
@@ -27,7 +27,7 @@ describe "test location form coordinate" do
27
27
  latitude = "39.9834"
28
28
  longitude = "116.3229"
29
29
 
30
- location = GeoApi::LocationService.instance.get_location_from_coordinate(latitude, longitude, 5)
30
+ location = GeoApi.get_location_from_coordinate(latitude, longitude, 5)
31
31
  expect(location["province"]).to eq("北京市")
32
32
  expect(location["city"]).to eq("北京市")
33
33
  expect(location["region"]).to eq("海淀区")
@@ -35,28 +35,28 @@ describe "test location form coordinate" do
35
35
  latitude = "36.66"
36
36
  longitude = "101.76"
37
37
 
38
- location = GeoApi::LocationService.instance.get_location_from_coordinate(latitude, longitude, 5)
38
+ location = GeoApi.get_location_from_coordinate(latitude, longitude, 5)
39
39
  expect(location["province"]).to eq("青海省")
40
40
  expect(location["city"]).to eq("西宁市")
41
41
  expect(location["region"]).to eq("城北区")
42
42
  end
43
43
 
44
44
  it "should get coordinate from string" do
45
- result = GeoApi::LocationService.instance.get_coordinate_from_string("思南路115弄")
46
-
45
+ result = GeoApi.get_coordinate_from_string("思南路115弄")
46
+ p result
47
47
  expect(result["location"]).not_to be_empty
48
48
 
49
- result = GeoApi::LocationService.instance.get_coordinate_from_string("思南路115弄","上海市")
49
+ result = GeoApi.get_coordinate_from_string("思南路115弄", "上海市")
50
50
 
51
51
  expect(result["location"]).not_to be_empty
52
52
 
53
- result = GeoApi::LocationService.instance.get_coordinate_from_string("")
53
+ result = GeoApi.get_coordinate_from_string("")
54
54
 
55
55
  expect(result).to eq(nil)
56
56
 
57
57
 
58
58
  # 500.times do
59
- # result = GeoApi::LocationService.instance.get_coordinate_from_string("思南路115弄")
59
+ # result = GeoApi.get_coordinate_from_string("思南路115弄")
60
60
  # if result.nil?
61
61
  # GeoApi.logger.debug("====失败!")
62
62
  # end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: geo_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.13
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - liuzelei
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-17 00:00:00.000000000 Z
11
+ date: 2015-02-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -108,7 +108,7 @@ dependencies:
108
108
  - - ">="
109
109
  - !ruby/object:Gem::Version
110
110
  version: '0'
111
- description: "%q{基础地理位置服务}"
111
+ description: "基础地理位置服务"
112
112
  email:
113
113
  - liuzelei@gmail.com
114
114
  executables: []
@@ -128,8 +128,7 @@ files:
128
128
  - lib/generators/geo_api/templates/initializer.rb
129
129
  - lib/geo_api.rb
130
130
  - lib/geo_api/configuration.rb
131
- - lib/geo_api/location_service.rb
132
- - lib/geo_api/models/location_log.rb
131
+ - lib/geo_api/models/log.rb
133
132
  - lib/geo_api/version.rb
134
133
  - spec/location_spec.rb
135
134
  - spec/spec_helper.rb
@@ -156,7 +155,7 @@ rubyforge_project:
156
155
  rubygems_version: 2.4.5
157
156
  signing_key:
158
157
  specification_version: 4
159
- summary: "%q{提供字符串解析,经纬度解析映射等}"
158
+ summary: "提供字符串解析,经纬度解析映射等"
160
159
  test_files:
161
160
  - spec/location_spec.rb
162
161
  - spec/spec_helper.rb
@@ -1,88 +0,0 @@
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