aboutsummaryrefslogtreecommitdiffstats
path: root/app/controllers/application_controller.rb
blob: 50d176120aa42afaf228bb7c9a42b71dc272e68e (plain)
1
2
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
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# -*- coding: utf-8 -*-
class ApplicationController < ActionController::Base
  protect_from_forgery
  before_filter :set_format, :get_include_user, :get_include_user_stats
  after_filter :xhtml

  def set_format
    unless request.format == :json || request.format == :html
      request.format = :html
    end
  end

  def xhtml
    if request.format == :html
      response.content_type = "application/xhtml+xml"

      # remove invalid charactors
      response.body = response.body.gsub(/[\x0-\x8\xb\xc\xe-\x1f]/, "")
    end
  end

  def get_include_user
    @include_user ||= get_bool(params[:include_user])
  end

  def get_include_user_stats
    if @include_user_stats ||= get_bool(params[:include_user_stats])
      @include_user = true
    end
  end

  def render_page(a = nil, &blk)
    @items = (a || blk.call).page(page || 1).per(count)
    @page_param = true

    render "shared/tweets"
  end

  def render_timeline(a = nil, &blk)
    @items = a || blk.call

    if max_id
      @items = @items.where("tweets.id <= ?", max_id)
    end

    if since_id
      @items = @items.where("tweets.id > ?", since_id)
    end

    @items = @items.page(1).per(count)

    render "shared/tweets"
  end

  def page; get_int(params[:page], nil){|i| i > 0} end

  def count; get_int(params[:count], 10){|i| (1..100) === i} end

  def max_id; get_int(params[:max_id], nil) end

  def since_id; get_int(params[:since_id], nil) end

  def order
    case params[:order]
    when /^fav/
      :favorite
    when /^re?t/
      :retweet
    else
      :default
    end
  end

  def all; get_bool(params[:all]) end

  def full; get_bool(params[:full]) end

  private
  def get_bool(str)
    if /^(t.*|1)$/ =~ str
      true
    else
      false
    end
  end

  def get_int(str, default = 0, &blk)
    if str =~ /^\d+$/
      i = str.to_i
      if !block_given? || blk.call(i)
        return i
      end
    end
    default
  end
end