Permalink
Browse files

Merge branch 'ascii_map'

  • Loading branch information...
2 parents 50d1403 + b876a8f commit 135b9cb3d9c3c113045ae1e9d6f09fcef9db4bc6 @alindeman alindeman committed Mar 19, 2012
View
@@ -76,6 +76,48 @@ zone = ActiveSupport::TimeZone[Zonebie.random_timezone]
puts zone.now
```
+## For the Geographically Impaired
+
+Check out this sweet ascii globe:
+
+```
+ ..... ......
+ ..... ........... .
+ . . .. ........... . .
+ . .. .. ........... . ..
+ .. .. . ........ . .......
+ ..... .... . ... ....... .. . . ...................
+............ ........ ... ... .. ..... ...............................
+ ... ......... . .. ..... ............................ .
+ ........... ... . .. ..................... ......
+ ............. ........ .. ...................
+ ........... .. . ..... ............... .
+ ... . .............. ................
+ ... ............. .. .. ... .
+ ..... .. .......... . .
+ .......... ...... .
+ ....... ..... . .....
+ ..... ... .........
+ .... ..
+ ..
+ .
+
+ .
+ .. .......... ...................
+ . .... .... ..........................................
+ ............... . ............................................
+ .................. . .............................................
+ ..................... .............................................
+ .........................................................................
+ ...........................................................................
+```
+
+To get your own pass in this option:
+
+```ruby
+Zonebie.set_random_timezone(:ascii_map => true)
+```
+
## Reproducing Bugs
When `Zonebie.set_random_timezone` is called, Zonebie assigns a timezone and
View
@@ -1,4 +1,5 @@
require File.expand_path("zonebie/version", File.dirname(__FILE__))
+require File.expand_path("zonebie/extras", File.dirname(__FILE__))
module Zonebie
class << self
@@ -36,10 +37,13 @@ def add_backend(backend)
@backends[backend.name] = backend
end
- def set_random_timezone
+ def set_random_timezone(options = {})
zone = ENV['TZ'] || random_timezone
- $stdout.puts("[Zonebie] Setting timezone to \"#{zone}\"") unless quiet
+ unless quiet
+ $stdout.puts("[Zonebie] Setting timezone to \"#{zone}\"")
+ $stdout.puts(Zonebie::Extras::AsciiMap.new(zone).to_s) if options[:ascii_map]
+ end
backend.zone = zone
end
View
@@ -0,0 +1 @@
+require File.expand_path("extras/ascii_map", File.dirname(__FILE__))
@@ -0,0 +1,72 @@
+require 'net/http'
+require 'open-uri'
+require 'chunky_png'
+require 'rainbow'
+
+Sickill::Rainbow.enabled = true
+
+module Zonebie
+ module Extras
+ class AsciiMap
+ attr_accessor :zone, :image, :ascii, :mark
+
+ def initialize(zone)
+ self.zone = zone
+ self.mark = false
+ self.ascii = map_to_ascii
+ end
+
+ def to_s
+ ascii
+ end
+
+ private
+
+ def disable_webmock
+ if defined? WebMock
+ allow_net_connect_was = WebMock::Config.instance.allow_net_connect
+ WebMock::Config.instance.allow_net_connect = true
+ yield
+ WebMock::Config.instance.allow_net_connect = allow_net_connect_was
+ else
+ yield
+ end
+ end
+
+ def google_maps_request
+ "http://maps.googleapis.com/maps/api/staticmap?format=png8&zoom=1&maptype=roadmap&sensor=false&center=0,0&size=500x500&markers=size:large%7Ccolor:red%7C#{URI.encode(zone)}&style=feature:all%7Celement:labels%7Cvisibility:off&style=feature:all%7Celement:geometry%7Clightness:100&style=feature:water%7Celement:geometry%7Clightness:-100"
+ end
+
+ def map_to_ascii
+ image = nil
+
+ disable_webmock do
+ open google_maps_request do |f|
+ image = ChunkyPNG::Image.from_blob(f.read)
+ end
+ end
+
+ image.resample_nearest_neighbor!(80, 30)
+ dots = image.pixels.map{ |p| colored_dot(p) }
+ dots.each_slice(80).map{ |d| d.join }.join("\n")
+ end
+
+ def colored_dot(color)
+ if ChunkyPNG::Color.grayscale? color
+ if ChunkyPNG::Color.r(color) > (255 / 2)
+ '.'
+ else
+ ' '
+ end
+ else
+ if mark
+ '.'
+ else
+ self.mark = true
+ '.'.color(:red)
+ end
+ end
+ end
+ end
+ end
+end
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@@ -0,0 +1,42 @@
+require "spec_helper"
+
+describe Zonebie::Extras::AsciiMap do
+ describe "#to_s" do
+ it "prints a map of the world" do
+ map = File.open(File.expand_path('../../../fixtures/ascii_map.png', File.dirname(__FILE__)))
+ Zonebie::Extras::AsciiMap.any_instance.expects(:open).yields(map)
+
+ Zonebie::Extras::AsciiMap.new('International Date Line West').to_s.should ==
+%{
+ ..... ......
+ ..... ........... .
+ . . .. ........... . .
+ . .. .. ........... . ..
+ .. .. . ........ . .......
+ ..... .... . ... ....... .. . . ...................
+............ ........ ... ... .. ..... ...............................
+ ... ......... . .. ..... ............................ .
+ ........... \e[31m.\e[0m.. . .. ..................... ......
+ ............ ........ .. ...................
+ ........... .. . ..... ............... .
+ ... . .............. ................
+ ... ............. .. .. ... .
+ ..... .. .......... . .
+ .......... ...... .
+ ....... ..... . .....
+ ..... ... .........
+ .... ..
+ ..
+ .
+
+ .
+ .. .......... ...................
+ . .... .... ..........................................
+ ............... . ............................................
+ .................. . .............................................
+ ..................... .............................................
+ .........................................................................
+ ...........................................................................}
+ end
+ end
+end
View
@@ -58,6 +58,13 @@
Zonebie.set_random_timezone
end
+ it "outputs the ascii map to STDOUT if the option is present" do
+ Zonebie::Extras::AsciiMap.any_instance.stubs(:to_s).returns('ascii map')
+ $stdout.expects(:puts).with("[Zonebie] Setting timezone to \"Eastern Time (US & Canada)\"")
+ $stdout.expects(:puts).with('ascii map')
+ Zonebie.set_random_timezone(:ascii_map => true)
+ end
+
it "does not output the timezone to STDOUT if quiet mode is enabled" do
$stdout.expects(:puts).never
View
@@ -21,4 +21,7 @@ Gem::Specification.new do |gem|
gem.add_development_dependency "activesupport", ">=2.3"
gem.add_development_dependency "tzinfo", ">=0.3"
+
+ gem.add_dependency "chunky_png"
+ gem.add_dependency "rainbow"
end

0 comments on commit 135b9cb

Please sign in to comment.