motion-geocoder 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1a927671c6b05f81d81c1e48fd06653eed841f09
4
+ data.tar.gz: da77e5598ba275bfda3a64601a67aeddc84a985f
5
+ SHA512:
6
+ metadata.gz: 2fc100e13a75d3a3cdcdfe9d7b07aa5954e2ba265eb875bca9238176b5ac5e4b28e981d9ad6df106c30d260c7c0ee0ea33d3370f6098d36e2b4152fcfc442279
7
+ data.tar.gz: 759d807599af768f600a2dea32e6ffc25222ca63787053080081d8e442dbc342dc122feb20cbdffa831a7b80e133c861207b565f39872dc63c01517d283670c3
@@ -0,0 +1,17 @@
1
+ unless defined?(Motion::Project::Config)
2
+ raise "This file must be required within a RubyMotion project Rakefile."
3
+ end
4
+
5
+ Motion::Project::App.setup do |app|
6
+ Dir.glob(File.join(File.dirname(__FILE__), 'motion-geocoder/**/*.rb')).each do |file|
7
+ app.files.unshift(file)
8
+ end
9
+
10
+ app.frameworks += ['MapKit']
11
+
12
+ app.pods ||= Motion::Project::CocoaPods.new(app)
13
+ app.pods do
14
+ pod 'AFNetworking'
15
+ end
16
+
17
+ end
@@ -0,0 +1,21 @@
1
+ module MotionGeocoder
2
+ module AppleApi
3
+ module Geocode
4
+ extend self
5
+
6
+ def call(lat, lng, &block)
7
+
8
+ start_loc = CLLocation.alloc.initWithLatitude lat, longitude: lng
9
+ geocoder = CLGeocoder.alloc.init
10
+ geocoder.reverseGeocodeLocation start_loc, completionHandler: lambda {|location, x|
11
+ unless location.empty?
12
+ title = "#{location[0].name} #{location[0].locality}" #{location[0].postalCode}
13
+ block.call title
14
+ end
15
+ }
16
+
17
+ end
18
+
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,45 @@
1
+ module MotionGeocoder
2
+ module AppleApi
3
+ module Place
4
+ @@local_search = nil
5
+
6
+ def self.build_request(input, params)
7
+ lat = params[:lat]
8
+ lng = params[:lng]
9
+ rad = params[:radius] || 5000
10
+
11
+ region = MKCoordinateRegionMakeWithDistance CLLocationCoordinate2DMake(lat, lng), rad, rad
12
+ request = MKLocalSearchRequest.alloc.init
13
+ request.region = region
14
+ request.naturalLanguageQuery = NSString.stringWithUTF8String(input)
15
+ request
16
+ end
17
+
18
+ def self.auto_complete(input, params={}, &block)
19
+ @@local_search.cancel if !!@@local_search
20
+
21
+ request = build_request input, params
22
+
23
+ @@local_search = MKLocalSearch.alloc.initWithRequest request
24
+ @@local_search.startWithCompletionHandler lambda {|response, e|
25
+ if response && !response.mapItems.empty?
26
+ results = response.mapItems.map do |item|
27
+ {
28
+ name: item.name,
29
+ address: item.placemark.title,
30
+ coordinate: {
31
+ lat: item.placemark.coordinate.latitude,
32
+ lng: item.placemark.coordinate.longitude
33
+ }
34
+ }
35
+ end
36
+ block.call results
37
+ end
38
+ }
39
+
40
+ end
41
+
42
+ end
43
+ end
44
+
45
+ end
@@ -0,0 +1,21 @@
1
+ module MotionGeocoder
2
+
3
+ module GoogleApi
4
+
5
+ extend self
6
+
7
+ def root
8
+ "https://maps.googleapis.com/maps/api/"
9
+ end
10
+
11
+ def key
12
+ @key
13
+ end
14
+
15
+ def register(key)
16
+ @key = key
17
+ end
18
+
19
+ end
20
+
21
+ end
@@ -0,0 +1,33 @@
1
+ module MotionGeocoder
2
+
3
+ module GoogleApi
4
+
5
+ module Geocode
6
+ extend self
7
+
8
+ def call(lat, lng, &block)
9
+ url = reverse_geo_url lat, lng
10
+
11
+ AFMotion::JSON.get(url) do |res|
12
+ block.call formatted_address(res)
13
+ end
14
+ end
15
+
16
+ def formatted_address(res)
17
+ case res.object['status']
18
+ when "OK"
19
+ res.object['results'][0]['formatted_address']
20
+ else
21
+ nil
22
+ end
23
+ end
24
+
25
+ def reverse_geo_url(lat, lng)
26
+ "#{MotionGeocoder::GoogleApi.root}geocode/json?latlng=#{lat},#{lng}&key=#{MotionGeocoder::GoogleApi.key}"
27
+ end
28
+
29
+ end
30
+
31
+ end
32
+
33
+ end
@@ -0,0 +1,29 @@
1
+ module MotionGeocoder
2
+
3
+ module GoogleApi
4
+
5
+ module Place
6
+ extend self
7
+
8
+ def auto_complete(input, &block)
9
+ url = auto_complete_url input
10
+
11
+ AFMotion::JSON.get(url) do |res|
12
+ pres = res.object['predictions'].map do |pre|
13
+ { address: pre['description'], place_id: pre['place_id'] }
14
+ end
15
+
16
+ block.call pres
17
+ end
18
+ end
19
+
20
+ def auto_complete_url(input, params={})
21
+ "#{MotionGeocoder::GoogleApi.root}place/autocomplete/json?key=#{MotionGeocoder::GoogleApi.key}&input=#{input}"
22
+ end
23
+
24
+ end
25
+
26
+
27
+ end
28
+
29
+ end
@@ -0,0 +1,3 @@
1
+ module MotionGeocoder
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,60 @@
1
+ describe "Google Geocode API " do
2
+ extend WebStub::SpecHelpers
3
+
4
+ before do
5
+ disable_network_access!
6
+ MotionGeocoder::GoogleApi.register "key"
7
+
8
+ @res_zero = File.read NSBundle.mainBundle.resourcePath.stringByAppendingPathComponent("geo_address_zero.json")
9
+ @res_denied = File.read NSBundle.mainBundle.resourcePath.stringByAppendingPathComponent("geo_address_denied.json")
10
+ @res_success = File.read NSBundle.mainBundle.resourcePath.stringByAppendingPathComponent("geo_address.json")
11
+ @url = MotionGeocoder::GoogleApi::Geocode.reverse_geo_url(0,0)
12
+ end
13
+
14
+ it "generates reverse geo url" do
15
+ @url.should == "https://maps.googleapis.com/maps/api/geocode/json?latlng=0,0&key=key"
16
+ end
17
+
18
+ it "denied when using invalid API key" do
19
+ stub_request(:get, @url).to_return(json: @res_denied)
20
+
21
+ MotionGeocoder::GoogleApi::Geocode.(0,0) do |res|
22
+ @res = res
23
+ resume
24
+ end
25
+
26
+ wait_max 1.0 do
27
+ @res.should == nil
28
+ end
29
+
30
+ end
31
+
32
+ it "returns zero addresses" do
33
+ stub_request(:get, @url).to_return(json: @res_zero)
34
+
35
+ MotionGeocoder::GoogleApi::Geocode.(0,0) do |res|
36
+ @res = res
37
+ resume
38
+ end
39
+
40
+ wait_max 1.0 do
41
+ @res.should == nil
42
+ end
43
+
44
+ end
45
+
46
+ it "returns real address" do
47
+ stub_request(:get, @url).to_return(json: @res_success)
48
+
49
+ MotionGeocoder::GoogleApi::Geocode.(0,0) do |res|
50
+ @res = res
51
+ resume
52
+ end
53
+
54
+ wait_max 1.0 do
55
+ @res.should == "1800 Ellis Street, San Francisco, CA 94115, USA"
56
+ end
57
+
58
+ end
59
+
60
+ end
@@ -0,0 +1,56 @@
1
+ describe "Google Place API " do
2
+ extend WebStub::SpecHelpers
3
+
4
+ before do
5
+ disable_network_access!
6
+ MotionGeocoder::GoogleApi.register "key"
7
+
8
+ @res_zero = File.read NSBundle.mainBundle.resourcePath.stringByAppendingPathComponent("geo_place_zero.json")
9
+ @res_denied = File.read NSBundle.mainBundle.resourcePath.stringByAppendingPathComponent("geo_place_denied.json")
10
+ @res_success = File.read NSBundle.mainBundle.resourcePath.stringByAppendingPathComponent("geo_place.json")
11
+ @url = MotionGeocoder::GoogleApi::Place.auto_complete_url(1)
12
+ end
13
+
14
+ it "denied when using invalid API key" do
15
+ stub_request(:get, @url).to_return(json: @res_denied)
16
+
17
+ MotionGeocoder::GoogleApi::Place.auto_complete(1) do |res|
18
+ @res = res
19
+ resume
20
+ end
21
+
22
+ wait_max 1.0 do
23
+ @res.should == []
24
+ end
25
+
26
+ end
27
+
28
+ it "returns zero addresses" do
29
+ stub_request(:get, @url).to_return(json: @res_zero)
30
+
31
+ MotionGeocoder::GoogleApi::Place.auto_complete(1) do |res|
32
+ @res = res
33
+ resume
34
+ end
35
+
36
+ wait_max 1.0 do
37
+ @res.should == []
38
+ end
39
+
40
+ end
41
+
42
+ it "returns real address" do
43
+ stub_request(:get, @url).to_return(json: @res_success)
44
+
45
+ MotionGeocoder::GoogleApi::Place.auto_complete(1) do |res|
46
+ @res = res
47
+ resume
48
+ end
49
+
50
+ wait_max 1.0 do
51
+ @res[0][:address].should == "1155 College View Drive, Monterey Park, CA, United States"
52
+ end
53
+
54
+ end
55
+
56
+ end
@@ -0,0 +1,8 @@
1
+ describe MotionGeocoder::GoogleApi do
2
+
3
+ it "should register key" do
4
+ MotionGeocoder::GoogleApi.register("key")
5
+ MotionGeocoder::GoogleApi.key.should == "key"
6
+ end
7
+
8
+ end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: motion-geocoder
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Qi He
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-09-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: afmotion
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 2.4.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 2.4.1
27
+ description: |2
28
+ Yet another rubymotion geocoder wrapper.
29
+ email: qhe@heyook.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - lib/motion-geocoder/apple_api/geocode.rb
35
+ - lib/motion-geocoder/apple_api/place.rb
36
+ - lib/motion-geocoder/google_api/geocode.rb
37
+ - lib/motion-geocoder/google_api/place.rb
38
+ - lib/motion-geocoder/google_api.rb
39
+ - lib/motion-geocoder/version.rb
40
+ - lib/motion-geocoder.rb
41
+ - spec/motion-geocoder/google_api/geocode_spec.rb
42
+ - spec/motion-geocoder/google_api/place_spec.rb
43
+ - spec/motion-geocoder/google_api_spec.rb
44
+ homepage: http://github.com/he9qi/motion-geocoder
45
+ licenses:
46
+ - MIT
47
+ metadata: {}
48
+ post_install_message:
49
+ rdoc_options: []
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - '>='
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ requirements: []
63
+ rubyforge_project:
64
+ rubygems_version: 2.0.14
65
+ signing_key:
66
+ specification_version: 4
67
+ summary: Geocoder wrapper for rubymotion.
68
+ test_files:
69
+ - spec/motion-geocoder/google_api/geocode_spec.rb
70
+ - spec/motion-geocoder/google_api/place_spec.rb
71
+ - spec/motion-geocoder/google_api_spec.rb