Class: Murlsh::UrlServer

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

Overview

Build responses for HTTP requests.

Instance Attribute Summary

Attributes inherited from Server

#config

Instance Method Summary collapse

Methods inherited from Server

#initialize

Constructor Details

This class inherits a constructor from Murlsh::Server

Instance Method Details

#auth_from_req(req) ⇒ Object

Authorize a user from a request.



99
100
101
102
103
104
# File 'lib/murlsh/url_server.rb', line 99

def auth_from_req(req)
  secret = req['auth']
 
  secret.to_s.empty? ? nil : Murlsh::Auth.new(
    config.fetch('auth_file')).auth(secret)
end

#delete(req) ⇒ Object

Delete a url.



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/murlsh/url_server.rb', line 78

def delete(req)
  response_body, response_code = '', 403

  url_id = req.path[%r{/url/(\d+)$}, 1]
  begin
    url = Murlsh::Url.find(url_id)
    if user = auth_from_req(req)
      if url[:email] == user[:email] or url[:name] == user[:name]
        url.destroy
        response_body, response_code = url.to_json, 200
      end
    end
  rescue ActiveRecord::RecordNotFound
    response_code = 404
  end

  Rack::Response.new response_body, response_code,
    'Content-Type' => 'application/json'
end

#get(req) ⇒ Object

Respond to a GET request. Return a page of urls based on the query string parameters.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/murlsh/url_server.rb', line 11

def get(req)
  page = [req['p'].to_i, 1].max
  per_page = req['pp'] ? req['pp'].to_i :
    config.fetch('num_posts_page', 25)

  content_type = 'text/html; charset=utf-8'
  result_set = Murlsh::UrlResultSet.new(req['q'], page, per_page)

  body = Murlsh::UrlBody.new(config, req, result_set, content_type)

  resp = Rack::Response.new
  resp.write(body.build)
  resp['Content-Type'] = content_type

  resp
end

#post(req) ⇒ Object

Respond to a POST request. Add the new url and return json.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/murlsh/url_server.rb', line 29

def post(req)
  if user = auth_from_req(req)
    mu = Murlsh::Url.new do |u|
      u.url = req['url']
      u.email = user[:email]
      u.name = user[:name]

      # optional parameters
      unless req['thumbnail'].to_s.empty?
          u.thumbnail_url = req['thumbnail']
      end

      u.time = if req['time']
        Time.at(req['time'].to_f).utc
      else
        Time.now.utc
      end

      unless req['title'].to_s.empty?
        u.title = req['title']
        u.user_supplied_title = true
      end

      u.via = req['via']  unless req['via'].to_s.empty?
    end

    begin
      # validate before add_pre plugins have run and also after (in save!)
      raise ActiveRecord::RecordInvalid.new(mu)  unless mu.valid?
      Murlsh::Plugin.hooks('add_pre') { |p| p.run mu, config }
      mu.save!
      Murlsh::Plugin.hooks('add_post') { |p| p.run mu, config }
      response_body, response_code = [mu], 200
    rescue ActiveRecord::RecordInvalid => error
      response_body = {
        'url' => error.record,
        'errors' => error.record.errors,
        }
      response_code = 500
    end
  else
    response_body, response_code = '', 403
  end

  Rack::Response.new(response_body.to_json, response_code, {
    'Content-Type' => 'application/json' })
end