Class: Machiawase::Place

Inherits:
Object
  • Object
show all
Defined in:
lib/machiawase/place.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#addressString (readonly)

Returns the address.

Returns:

  • (String)

    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(":")
     = raccount.nil? ? "" : raccount.reverse.split(":")
    user, pass = 
    
    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_urlString (readonly)

Returns the url of Google Map.

Returns:

  • (String)

    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(":")
     = raccount.nil? ? "" : raccount.reverse.split(":")
    user, pass = 
    
    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

#latObject

Returns the latitude.

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(":")
     = raccount.nil? ? "" : raccount.reverse.split(":")
    user, pass = 
    
    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

#lonObject

Returns the longitude.

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(":")
     = raccount.nil? ? "" : raccount.reverse.split(":")
    user, pass = 
    
    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_stationString (readonly)

Returns the nearest station.

Returns:

  • (String)

    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(":")
     = raccount.nil? ? "" : raccount.reverse.split(":")
    user, pass = 
    
    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.

Returns:

  • (Hash)

    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(":")
   = raccount.nil? ? "" : raccount.reverse.split(":")
  user, pass = 
  
  proxy = OpenStruct.new({      
                           "server" => server.empty? ? nil : "http://#{server}",
                           "user"   => user.nil? ? "" : user,
                           "pass"   => pass.nil? ? "" : pass
                         })
end

Instance Method Details

#to_hHash

Returns attributes with Hash format.

Returns:

  • (Hash)

    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_jsonJSON

Returns attributes with JSON format.

Returns:

  • (JSON)

    attributes with JSON format.



96
97
98
# File 'lib/machiawase/place.rb', line 96

def to_json
  JSON.pretty_generate(to_h)
end

#to_msgpackObject



100
101
102
# File 'lib/machiawase/place.rb', line 100

def to_msgpack
  to_h.to_msgpack
end