Class: Murlsh::Dispatch

Inherits:
Object
  • Object
show all
Defined in:
lib/murlsh/dispatch.rb

Overview

Dispatch requests.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Dispatch

Set up database connection and dispatch table.



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
# File 'lib/murlsh/dispatch.rb', line 14

def initialize(config)
  @config = config

  atom_server = Murlsh::AtomServer.new(config)
  json_server = Murlsh::JsonServer.new(config)
  m3u_server = Murlsh::M3uServer.new(config)
  podcast_server = Murlsh::PodcastServer.new(config)
  pop_server = Murlsh::PopServer.new(config)
  random_server = Murlsh::RandomServer.new(config)
  rss_server = Murlsh::RssServer.new(config)
  url_server = Murlsh::UrlServer.new(config)

  root_path = URI(config.fetch('root_url')).path

  @routes = [
    [%r{^(?:HEAD|GET) #{root_path}atom\.atom$}, atom_server.method(:get)],
    [%r{^(?:HEAD|GET) #{root_path}json\.json$}, json_server.method(:get)],
    [%r{^(?:HEAD|GET) #{root_path}m3u\.m3u$}, m3u_server.method(:get)],
    [%r{^(?:HEAD|GET) #{root_path}podcast\.rss$}, podcast_server.method(:get)],
    [%r{^POST #{root_path}pop$}, pop_server.method(:post)],
    [%r{^(?:HEAD|GET) #{root_path}random$}, random_server.method(:get)],
    [%r{^(?:HEAD|GET) #{root_path}rss\.rss$}, rss_server.method(:get)],
    [%r{^(?:HEAD|GET) #{root_path}(url)?$}, url_server.method(:get)],
    [%r{^GET #{root_path}bookmarklet$}, url_server.method(:post)],
    [%r{^POST #{root_path}(url)?$}, url_server.method(:post)],
    [%r{^DELETE #{root_path}url/\d+$}, url_server.method(:delete)],
  ]

  db_init
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



80
81
82
# File 'lib/murlsh/dispatch.rb', line 80

def config
  @config
end

#routesObject

Returns the value of attribute routes.



81
82
83
# File 'lib/murlsh/dispatch.rb', line 81

def routes
  @routes
end

Instance Method Details

#call(env) ⇒ Object

Rack call.



62
63
64
65
# File 'lib/murlsh/dispatch.rb', line 62

def call(env)
  req = Rack::Request.new(env)
  dispatch(req).call(req).finish
end

#db_initObject



45
46
47
48
49
50
# File 'lib/murlsh/dispatch.rb', line 45

def db_init
  ActiveRecord::Base.establish_connection config.fetch('db')
  ActiveRecord::Base.default_timezone = :utc
  ActiveRecord::Base.include_root_in_json = false
  # ActiveRecord::Base.logger = Logger.new(STDERR)
end

#dispatch(req) ⇒ Object

Figure out which method will handle request.



53
54
55
56
57
58
59
# File 'lib/murlsh/dispatch.rb', line 53

def dispatch(req)
  method_match = routes.find do |rule|
    rule[0].match("#{req.request_method} #{req.path}")
  end

  method_match ? method_match[1] : self.method(:not_found)
end

#not_found(req) ⇒ Object

Called if the request is not found.



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/murlsh/dispatch.rb', line 68

def not_found(req)
  if req.head?
    Rack::Response.new([], 404)
  else
    Rack::Response.new("<p>#{req.url} not found</p>

<p><a href=\"#{config.fetch('root_url')}\">root<a></p>
",
      404, { 'Content-Type' => 'text/html' })
  end
end