Class: Machiawase::Place
- Inherits:
-
Object
- Object
- Machiawase::Place
- Defined in:
- lib/machiawase/place.rb
Instance Attribute Summary collapse
-
#address ⇒ String
readonly
The address.
-
#google_map_url ⇒ String
readonly
The url of Google Map.
-
#lat ⇒ Object
The latitude.
-
#lon ⇒ Object
The longitude.
-
#near_station ⇒ String
readonly
The nearest station.
Class Method Summary collapse
-
.geocode(address) ⇒ Hash
Geocode of given address.
- .parse_proxy(proxy) ⇒ Object
Instance Method Summary collapse
-
#initialize(lat, lon) ⇒ Place
constructor
A new instance of Place.
-
#to_h ⇒ Hash
Attributes with Hash format.
-
#to_json ⇒ JSON
Attributes with JSON format.
- #to_msgpack ⇒ Object
Constructor Details
#initialize(lat, lon) ⇒ Place
Returns a new instance of Place.
18 19 20 21 22 23 24 25 26 |
# File 'lib/machiawase/place.rb', line 18 def initialize(lat, lon) @lat = lat @lon = lon @doc = nil @address = nil @near_station = nil @google_map_url = "http://maps.google.co.jp/maps?q=#{@lat},#{@lon}&ll=#{@lat},#{@lon}&z=14&t=m&brcurrent=3,0x0:0x0,1" @proxy = Place.parse_proxy(ENV["http_proxy"]) end |
Instance Attribute Details
#address ⇒ String (readonly)
Returns the address.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/machiawase/place.rb', line 14 class Place attr_reader :address, :near_station, :google_map_url attr_accessor :lat, :lon def initialize(lat, lon) @lat = lat @lon = lon @doc = nil @address = nil @near_station = nil @google_map_url = "http://maps.google.co.jp/maps?q=#{@lat},#{@lon}&ll=#{@lat},#{@lon}&z=14&t=m&brcurrent=3,0x0:0x0,1" @proxy = Place.parse_proxy(ENV["http_proxy"]) end def lat=(val) @lat = val @doc = nil @address = nil @near_station = nil end def lon=(val) @lon = val @doc = nil @address = nil @near_station = nil end # @return [Hash] geocode of given address. def self.geocode(address) baseUrl = "http://maps.google.com/maps/api/geocode/json" reqUrl = "#{baseUrl}?address=#{URI.encode(address)}&sensor=false&language=ja" proxy = parse_proxy(ENV["http_proxy"]) response = open(URI.parse(reqUrl), :proxy_http_basic_authentication => [proxy.server, proxy.user, proxy.pass]) status = JSON.parse(response.string) lat = status['results'][0]['geometry']['location']['lat'] lon = status['results'][0]['geometry']['location']['lng'] {"lat" => lat, "lon" => lon} end def self.parse_proxy(proxy) # http://user:pass@host:port のように書かれていることを想定 # パスワードに@とか入ってる場合があるので一番後ろの@でだけsplitする rserver, raccount = (proxy || '').sub(/http:\/\//, '').reverse.split("@", 2) server = rserver.nil? ? "" : rserver.reverse host, port = server.split(":") account = raccount.nil? ? "" : raccount.reverse.split(":") user, pass = account proxy = OpenStruct.new({ "server" => server.empty? ? nil : "http://#{server}", "user" => user.nil? ? "" : user, "pass" => pass.nil? ? "" : pass }) end def address @doc ||= Nokogiri::HTML(open("http://geocode.didit.jp/reverse/?lat=#{@lat}&lon=#{@lon}", :proxy_http_basic_authentication => [@proxy.server, @proxy.user, @proxy.pass])) @address ||= @doc.xpath('//address')[0].content rescue => exc p exc end def near_station @doc ||= Nokogiri::HTML(open("http://geocode.didit.jp/reverse/?lat=#{@lat}&lon=#{@lon}", :proxy_http_basic_authentication => [@proxy.server, @proxy.user, @proxy.pass])) @near_station ||= @doc.xpath('//station1')[0].content rescue => exc p exc end # @return [Hash] attributes with Hash format. def to_h { "latitude" => @lat, "longitude" => @lon, "address" => address, "near_station" => near_station, "google_map" => @google_map_url } end # @return [JSON] attributes with JSON format. def to_json JSON.pretty_generate(to_h) end def to_msgpack to_h.to_msgpack end end |
#google_map_url ⇒ String (readonly)
Returns the url of Google Map.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/machiawase/place.rb', line 14 class Place attr_reader :address, :near_station, :google_map_url attr_accessor :lat, :lon def initialize(lat, lon) @lat = lat @lon = lon @doc = nil @address = nil @near_station = nil @google_map_url = "http://maps.google.co.jp/maps?q=#{@lat},#{@lon}&ll=#{@lat},#{@lon}&z=14&t=m&brcurrent=3,0x0:0x0,1" @proxy = Place.parse_proxy(ENV["http_proxy"]) end def lat=(val) @lat = val @doc = nil @address = nil @near_station = nil end def lon=(val) @lon = val @doc = nil @address = nil @near_station = nil end # @return [Hash] geocode of given address. def self.geocode(address) baseUrl = "http://maps.google.com/maps/api/geocode/json" reqUrl = "#{baseUrl}?address=#{URI.encode(address)}&sensor=false&language=ja" proxy = parse_proxy(ENV["http_proxy"]) response = open(URI.parse(reqUrl), :proxy_http_basic_authentication => [proxy.server, proxy.user, proxy.pass]) status = JSON.parse(response.string) lat = status['results'][0]['geometry']['location']['lat'] lon = status['results'][0]['geometry']['location']['lng'] {"lat" => lat, "lon" => lon} end def self.parse_proxy(proxy) # http://user:pass@host:port のように書かれていることを想定 # パスワードに@とか入ってる場合があるので一番後ろの@でだけsplitする rserver, raccount = (proxy || '').sub(/http:\/\//, '').reverse.split("@", 2) server = rserver.nil? ? "" : rserver.reverse host, port = server.split(":") account = raccount.nil? ? "" : raccount.reverse.split(":") user, pass = account proxy = OpenStruct.new({ "server" => server.empty? ? nil : "http://#{server}", "user" => user.nil? ? "" : user, "pass" => pass.nil? ? "" : pass }) end def address @doc ||= Nokogiri::HTML(open("http://geocode.didit.jp/reverse/?lat=#{@lat}&lon=#{@lon}", :proxy_http_basic_authentication => [@proxy.server, @proxy.user, @proxy.pass])) @address ||= @doc.xpath('//address')[0].content rescue => exc p exc end def near_station @doc ||= Nokogiri::HTML(open("http://geocode.didit.jp/reverse/?lat=#{@lat}&lon=#{@lon}", :proxy_http_basic_authentication => [@proxy.server, @proxy.user, @proxy.pass])) @near_station ||= @doc.xpath('//station1')[0].content rescue => exc p exc end # @return [Hash] attributes with Hash format. def to_h { "latitude" => @lat, "longitude" => @lon, "address" => address, "near_station" => near_station, "google_map" => @google_map_url } end # @return [JSON] attributes with JSON format. def to_json JSON.pretty_generate(to_h) end def to_msgpack to_h.to_msgpack end end |
#lat ⇒ Object
Returns the latitude.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/machiawase/place.rb', line 14 class Place attr_reader :address, :near_station, :google_map_url attr_accessor :lat, :lon def initialize(lat, lon) @lat = lat @lon = lon @doc = nil @address = nil @near_station = nil @google_map_url = "http://maps.google.co.jp/maps?q=#{@lat},#{@lon}&ll=#{@lat},#{@lon}&z=14&t=m&brcurrent=3,0x0:0x0,1" @proxy = Place.parse_proxy(ENV["http_proxy"]) end def lat=(val) @lat = val @doc = nil @address = nil @near_station = nil end def lon=(val) @lon = val @doc = nil @address = nil @near_station = nil end # @return [Hash] geocode of given address. def self.geocode(address) baseUrl = "http://maps.google.com/maps/api/geocode/json" reqUrl = "#{baseUrl}?address=#{URI.encode(address)}&sensor=false&language=ja" proxy = parse_proxy(ENV["http_proxy"]) response = open(URI.parse(reqUrl), :proxy_http_basic_authentication => [proxy.server, proxy.user, proxy.pass]) status = JSON.parse(response.string) lat = status['results'][0]['geometry']['location']['lat'] lon = status['results'][0]['geometry']['location']['lng'] {"lat" => lat, "lon" => lon} end def self.parse_proxy(proxy) # http://user:pass@host:port のように書かれていることを想定 # パスワードに@とか入ってる場合があるので一番後ろの@でだけsplitする rserver, raccount = (proxy || '').sub(/http:\/\//, '').reverse.split("@", 2) server = rserver.nil? ? "" : rserver.reverse host, port = server.split(":") account = raccount.nil? ? "" : raccount.reverse.split(":") user, pass = account proxy = OpenStruct.new({ "server" => server.empty? ? nil : "http://#{server}", "user" => user.nil? ? "" : user, "pass" => pass.nil? ? "" : pass }) end def address @doc ||= Nokogiri::HTML(open("http://geocode.didit.jp/reverse/?lat=#{@lat}&lon=#{@lon}", :proxy_http_basic_authentication => [@proxy.server, @proxy.user, @proxy.pass])) @address ||= @doc.xpath('//address')[0].content rescue => exc p exc end def near_station @doc ||= Nokogiri::HTML(open("http://geocode.didit.jp/reverse/?lat=#{@lat}&lon=#{@lon}", :proxy_http_basic_authentication => [@proxy.server, @proxy.user, @proxy.pass])) @near_station ||= @doc.xpath('//station1')[0].content rescue => exc p exc end # @return [Hash] attributes with Hash format. def to_h { "latitude" => @lat, "longitude" => @lon, "address" => address, "near_station" => near_station, "google_map" => @google_map_url } end # @return [JSON] attributes with JSON format. def to_json JSON.pretty_generate(to_h) end def to_msgpack to_h.to_msgpack end end |
#lon ⇒ Object
Returns the longitude.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/machiawase/place.rb', line 14 class Place attr_reader :address, :near_station, :google_map_url attr_accessor :lat, :lon def initialize(lat, lon) @lat = lat @lon = lon @doc = nil @address = nil @near_station = nil @google_map_url = "http://maps.google.co.jp/maps?q=#{@lat},#{@lon}&ll=#{@lat},#{@lon}&z=14&t=m&brcurrent=3,0x0:0x0,1" @proxy = Place.parse_proxy(ENV["http_proxy"]) end def lat=(val) @lat = val @doc = nil @address = nil @near_station = nil end def lon=(val) @lon = val @doc = nil @address = nil @near_station = nil end # @return [Hash] geocode of given address. def self.geocode(address) baseUrl = "http://maps.google.com/maps/api/geocode/json" reqUrl = "#{baseUrl}?address=#{URI.encode(address)}&sensor=false&language=ja" proxy = parse_proxy(ENV["http_proxy"]) response = open(URI.parse(reqUrl), :proxy_http_basic_authentication => [proxy.server, proxy.user, proxy.pass]) status = JSON.parse(response.string) lat = status['results'][0]['geometry']['location']['lat'] lon = status['results'][0]['geometry']['location']['lng'] {"lat" => lat, "lon" => lon} end def self.parse_proxy(proxy) # http://user:pass@host:port のように書かれていることを想定 # パスワードに@とか入ってる場合があるので一番後ろの@でだけsplitする rserver, raccount = (proxy || '').sub(/http:\/\//, '').reverse.split("@", 2) server = rserver.nil? ? "" : rserver.reverse host, port = server.split(":") account = raccount.nil? ? "" : raccount.reverse.split(":") user, pass = account proxy = OpenStruct.new({ "server" => server.empty? ? nil : "http://#{server}", "user" => user.nil? ? "" : user, "pass" => pass.nil? ? "" : pass }) end def address @doc ||= Nokogiri::HTML(open("http://geocode.didit.jp/reverse/?lat=#{@lat}&lon=#{@lon}", :proxy_http_basic_authentication => [@proxy.server, @proxy.user, @proxy.pass])) @address ||= @doc.xpath('//address')[0].content rescue => exc p exc end def near_station @doc ||= Nokogiri::HTML(open("http://geocode.didit.jp/reverse/?lat=#{@lat}&lon=#{@lon}", :proxy_http_basic_authentication => [@proxy.server, @proxy.user, @proxy.pass])) @near_station ||= @doc.xpath('//station1')[0].content rescue => exc p exc end # @return [Hash] attributes with Hash format. def to_h { "latitude" => @lat, "longitude" => @lon, "address" => address, "near_station" => near_station, "google_map" => @google_map_url } end # @return [JSON] attributes with JSON format. def to_json JSON.pretty_generate(to_h) end def to_msgpack to_h.to_msgpack end end |
#near_station ⇒ String (readonly)
Returns the nearest station.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/machiawase/place.rb', line 14 class Place attr_reader :address, :near_station, :google_map_url attr_accessor :lat, :lon def initialize(lat, lon) @lat = lat @lon = lon @doc = nil @address = nil @near_station = nil @google_map_url = "http://maps.google.co.jp/maps?q=#{@lat},#{@lon}&ll=#{@lat},#{@lon}&z=14&t=m&brcurrent=3,0x0:0x0,1" @proxy = Place.parse_proxy(ENV["http_proxy"]) end def lat=(val) @lat = val @doc = nil @address = nil @near_station = nil end def lon=(val) @lon = val @doc = nil @address = nil @near_station = nil end # @return [Hash] geocode of given address. def self.geocode(address) baseUrl = "http://maps.google.com/maps/api/geocode/json" reqUrl = "#{baseUrl}?address=#{URI.encode(address)}&sensor=false&language=ja" proxy = parse_proxy(ENV["http_proxy"]) response = open(URI.parse(reqUrl), :proxy_http_basic_authentication => [proxy.server, proxy.user, proxy.pass]) status = JSON.parse(response.string) lat = status['results'][0]['geometry']['location']['lat'] lon = status['results'][0]['geometry']['location']['lng'] {"lat" => lat, "lon" => lon} end def self.parse_proxy(proxy) # http://user:pass@host:port のように書かれていることを想定 # パスワードに@とか入ってる場合があるので一番後ろの@でだけsplitする rserver, raccount = (proxy || '').sub(/http:\/\//, '').reverse.split("@", 2) server = rserver.nil? ? "" : rserver.reverse host, port = server.split(":") account = raccount.nil? ? "" : raccount.reverse.split(":") user, pass = account proxy = OpenStruct.new({ "server" => server.empty? ? nil : "http://#{server}", "user" => user.nil? ? "" : user, "pass" => pass.nil? ? "" : pass }) end def address @doc ||= Nokogiri::HTML(open("http://geocode.didit.jp/reverse/?lat=#{@lat}&lon=#{@lon}", :proxy_http_basic_authentication => [@proxy.server, @proxy.user, @proxy.pass])) @address ||= @doc.xpath('//address')[0].content rescue => exc p exc end def near_station @doc ||= Nokogiri::HTML(open("http://geocode.didit.jp/reverse/?lat=#{@lat}&lon=#{@lon}", :proxy_http_basic_authentication => [@proxy.server, @proxy.user, @proxy.pass])) @near_station ||= @doc.xpath('//station1')[0].content rescue => exc p exc end # @return [Hash] attributes with Hash format. def to_h { "latitude" => @lat, "longitude" => @lon, "address" => address, "near_station" => near_station, "google_map" => @google_map_url } end # @return [JSON] attributes with JSON format. def to_json JSON.pretty_generate(to_h) end def to_msgpack to_h.to_msgpack end end |
Class Method Details
.geocode(address) ⇒ Hash
Returns geocode of given address.
43 44 45 46 47 48 49 50 51 52 |
# File 'lib/machiawase/place.rb', line 43 def self.geocode(address) baseUrl = "http://maps.google.com/maps/api/geocode/json" reqUrl = "#{baseUrl}?address=#{URI.encode(address)}&sensor=false&language=ja" proxy = parse_proxy(ENV["http_proxy"]) response = open(URI.parse(reqUrl), :proxy_http_basic_authentication => [proxy.server, proxy.user, proxy.pass]) status = JSON.parse(response.string) lat = status['results'][0]['geometry']['location']['lat'] lon = status['results'][0]['geometry']['location']['lng'] {"lat" => lat, "lon" => lon} end |
.parse_proxy(proxy) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/machiawase/place.rb', line 54 def self.parse_proxy(proxy) # http://user:pass@host:port のように書かれていることを想定 # パスワードに@とか入ってる場合があるので一番後ろの@でだけsplitする rserver, raccount = (proxy || '').sub(/http:\/\//, '').reverse.split("@", 2) server = rserver.nil? ? "" : rserver.reverse host, port = server.split(":") account = raccount.nil? ? "" : raccount.reverse.split(":") user, pass = account proxy = OpenStruct.new({ "server" => server.empty? ? nil : "http://#{server}", "user" => user.nil? ? "" : user, "pass" => pass.nil? ? "" : pass }) end |
Instance Method Details
#to_h ⇒ Hash
Returns attributes with Hash format.
85 86 87 88 89 90 91 92 93 |
# File 'lib/machiawase/place.rb', line 85 def to_h { "latitude" => @lat, "longitude" => @lon, "address" => address, "near_station" => near_station, "google_map" => @google_map_url } end |
#to_json ⇒ JSON
Returns attributes with JSON format.
96 97 98 |
# File 'lib/machiawase/place.rb', line 96 def to_json JSON.pretty_generate(to_h) end |
#to_msgpack ⇒ Object
100 101 102 |
# File 'lib/machiawase/place.rb', line 100 def to_msgpack to_h.to_msgpack end |