Module: Termapinator

Defined in:
lib/termapinator.rb,
lib/termapinator/version.rb

Constant Summary collapse

VERSION =
"0.0.1"

Class Method Summary collapse

Class Method Details

.colored_square(color) ⇒ Object



45
46
47
# File 'lib/termapinator.rb', line 45

def self.colored_square(color)
  ''.color(ChunkyPNG::Color.r(color), ChunkyPNG::Color.g(color), ChunkyPNG::Color.b(color))
end

.google_maps_request(query) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/termapinator.rb', line 15

def self.google_maps_request(query)
  params = {
    center: query,
    size: '640x640',
    sensor: true,
    style: 'element:labels|visibility:off',
    zoom: 15
  }

  uri = URI.parse("http://maps.googleapis.com/maps/api/staticmap")
  uri.query = params.map { |k, v| "#{k}=#{URI.escape(v.to_s)}" }.join('&')
  uri
end

.image_to_ascii(image) ⇒ Object



39
40
41
42
43
# File 'lib/termapinator.rb', line 39

def self.image_to_ascii(image)
  image.resample_nearest_neighbor!(80, 30)
  squares = image.pixels.map { |pixel| colored_square(pixel) }
  squares.each_slice(80).map { |square| square.join }.join("\n")
end

.load_image(url) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/termapinator.rb', line 29

def self.load_image(url)
  image = nil

  open(url) do |f|
    image = ChunkyPNG::Image.from_blob(f.read)
  end

  image
end

.run(query) ⇒ Object



9
10
11
12
13
# File 'lib/termapinator.rb', line 9

def self.run(query)
  image = load_image(google_maps_request(query))
  ascii = image_to_ascii(image)
  puts ascii
end