Class: Google::Maps::Static::Map
- Inherits:
-
Object
- Object
- Google::Maps::Static::Map
- Defined in:
- lib/google/maps/static/map.rb
Instance Attribute Summary collapse
-
#timeout ⇒ Object
Returns the value of attribute timeout.
Instance Method Summary collapse
- #<<(item) ⇒ Object (also: #add_item)
- #[]=(key, value) ⇒ Object
-
#initialize(*args) ⇒ Map
constructor
A new instance of Map.
- #save(file) ⇒ Object
- #url ⇒ Object (also: #to_s)
Constructor Details
#initialize(*args) ⇒ Map
Returns a new instance of Map.
10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/google/maps/static/map.rb', line 10 def initialize *args @base = "http://maps.google.com/maps/api/staticmap" @options = { # :zoom => 12, :size => [400, 400], :sensor => false } @options.merge! args. @items = args.select { |item| item.is_a?(Items::Base) }.map(&:to_s) end |
Instance Attribute Details
#timeout ⇒ Object
Returns the value of attribute timeout.
8 9 10 |
# File 'lib/google/maps/static/map.rb', line 8 def timeout @timeout end |
Instance Method Details
#<<(item) ⇒ Object Also known as: add_item
28 29 30 31 32 |
# File 'lib/google/maps/static/map.rb', line 28 def << item raise GMapper::Error, "Please only use Items::Base decendants." unless item.is_a?(Items::Base) @items << item.to_s @url = nil if @url # Object has become dirty, url should be regenerated end |
#[]=(key, value) ⇒ Object
23 24 25 26 |
# File 'lib/google/maps/static/map.rb', line 23 def []= key, value @options[key] = value @url = nil if @url # Object has become dirty, url should be regenerated end |
#save(file) ⇒ Object
35 36 37 38 39 40 41 42 |
# File 'lib/google/maps/static/map.rb', line 35 def save file uri = ::URI.parse( url._encode ) http = ::Net::HTTP.new( uri.host, uri.port ) http.read_timeout = @timeout if @timeout response = http.get( "#{uri.path}?#{uri.query}" ) File.open( file, 'wb' ) { |f| f.write response.body } end |
#url ⇒ Object Also known as: to_s
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/google/maps/static/map.rb', line 44 def url unless @options.key?(:center) || !@items.empty? raise GMapper::Error, "Must specify the center location unless you add one or more map items." end unless @options.key?(:zoom) || !@items.empty? raise GMapper::Error, "Must specify the zoom level unless you add one or more map items." end if @url.nil? url = "#{@base}?#{parameters}" def url._encode gsub(/\|/) { |match| '%' + match.unpack('H2').join.upcase } end @url = url end @url end |