Last active
July 9, 2016 06:55
-
-
Save sankichi92/4841aedce0cd8a1922a7f4f2009e53d8 to your computer and use it in GitHub Desktop.
Service Computing Assignment 4 - Practice of Information Systems
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'net/http' | |
| require 'uri' | |
| require 'json' | |
| raise ArgumentError, "required argument 'zipcode' not found" if ARGV.empty? | |
| ZIPCODE = ARGV[0] | |
| API_KEY = ''.freeze # TODO: fill in your Google Maps Geocoding API key | |
| def get_hash_from_json_web_api(uri, **params) | |
| uri.query = URI.encode_www_form(params) | |
| # puts uri | |
| res = Net::HTTP.get_response(uri) | |
| # puts res.body | |
| JSON.parse(res.body) | |
| end | |
| uri1 = URI('http://zipcloud.ibsnet.co.jp/api/search') | |
| hash1 = get_hash_from_json_web_api(uri1, zipcode: ZIPCODE) | |
| result = hash1['results'][0] | |
| address = result['address1'] + result['address2'] + result['address3'] | |
| puts "address: #{address}" | |
| uri2 = URI('https://maps.googleapis.com/maps/api/geocode/json') | |
| hash2 = get_hash_from_json_web_api(uri2, address: address, key: API_KEY) | |
| location = hash2['results'][0]['geometry']['location'] | |
| puts "latitude: #{location['lat']}" | |
| puts "longitude: #{location['lng']}" |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage
Give a zipcode as an argument.
Then you can get following results.