Module: Amnesia::Routes

Included in:
Application
Defined in:
lib/amnesia/routes.rb

Class Method Summary collapse

Class Method Details

.included(app) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
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
# File 'lib/amnesia/routes.rb', line 3

def self.included app
  app.class_eval do
    get '/' do
      haml :index
    end

    get '/pie' do
      data = params[:d].to_s.split(",").map(&:to_i)

      r = 25
      c = (2*Math::PI*r).ceil
      v = data.first.to_f / data.last * 100 * c / 100
      v = v.nan? ? 0 : v.ceil

      content_type "image/svg+xml"
      "        <svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 100 100\"\n             style=\"transform:rotate(-90deg);\">\n          <circle r=\"\#{r*2}\" cx=\"50\" cy=\"50\" fill=\"#FFEBCC\" />\n          <circle r=\"\#{r}\" cx=\"50\" cy=\"50\"\n            fill=\"transparent\" stroke=\"#FF9901\"\n            stroke-width=\"\#{r*2}\" stroke-dasharray=\"\#{v} \#{c}\"\n          />\n        </svg>\n      BODY\n    end\n\n    get '/:address' do\n      @host = find_host params[:address]\n      @host ? haml(:host) : halt(404)\n    end\n\n    def find_host address\n      @hosts.find { |h| h.address == address }\n    end\n  end\nend\n"