public
Description: Everything I know about X, I learned from Y. (a simple Camping app)
Homepage: http://learning.kicks-ass.org/
Clone URL: git://github.com/beppu/learning.git
Click here to lend your support to: learning and make a donation at www.pledgie.com !
learning / learning / controllers.rb
100644 155 lines (148 sloc) 4.287 kb
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
module Learning::Controllers
  # http://code.whytheluckystiff.net/camping/wiki/ServingStaticFiles
  class Static < R '/static/(.+)'
    MIME_TYPES = {
      '.html' => 'text/html',
      '.css' => 'text/css',
      '.js' => 'text/javascript',
      '.jpg' => 'image/jpeg',
      '.gif' => 'image/gif'
    }
    PATH = File.expand_path(File.dirname(File.dirname(__FILE__)))
 
    def get(path)
      @headers['Content-Type'] = MIME_TYPES[path[/\.\w+$/, 0]] || "text/plain"
      unless path.include? ".." # prevent directory traversal attacks
        @headers['X-Sendfile'] = "#{PATH}/static/#{path}"
      else
        @status = "403"
        "403 - Invalid path"
      end
    end
  end
 
  class Home < R '/'
    def get
      @topic = @teacher = ""
      @pairs = L.Pair.find(
        :all,
        :order => 'updated_at DESC, created_at DESC',
        :limit => 100
      )
      render :home
    end
  end
 
  class Env < R '/env'
    def get
      render :env
    end
  end
 
  class Feed < R '/(\w+).xml'
    def get(format='atom')
      @forum = OpenStruct.new(
        :name => 'Everything I know about X, I learned from Y',
        :url => "http://#{@env['HTTP_HOST']}/",
        :feed_url => "http://#{@env['HTTP_HOST']}" + R(Feed, format)
      )
      @pairs = L.Pair.find(
        :all,
        :order => 'updated_at DESC, created_at DESC',
        :limit => 400
      )
      @threads = [ ]
      @pairs.each do |p|
        thread = OpenStruct.new(
          :author => 'Anonymous',
          :title => "#{p.topic.word} and #{p.teacher.word}",
          :url => "http://#{@env['HTTP_HOST']}" + R(Pair, p.id, p.slug),
          :unique_id => "tag:#{@env['HTTP_HOST']},#{p.created_at.strftime('%F')}:#{R(Pair, p.id, p.slug)}",
          :updated_at => p.updated_at,
          :content => "<h1>Everything I know about #{p.topic.word},</h1><h1>I learned from #{p.teacher.word}.</h1>"
        )
        @threads << thread
      end
      template = "_#{format}_feed"
      render template.to_sym
    end
  end
 
  class Random < R '/random(\.\w+)?'
    def get(format = nil)
      topic_count = L.Topic.count
      teacher_count = L.Teacher.count
      topic = nil
      teacher = nil
      if topic_count > 0
        while topic.nil?
          begin
            topic = L.Topic.find(rand(topic_count) + 1)
          rescue
          end
        end
        @topic = topic.word
      else
        @topic = ""
      end
      if teacher_count > 0
        while teacher.nil?
          begin
            teacher = L.Teacher.find(rand(teacher_count) + 1)
          rescue
          end
        end
        @teacher = teacher.word
      else
        @teacher = ""
      end
      if format == '.json'
        return { :topic => @topic, :teacher => @teacher }.to_json
      end
      @pairs = L.Pair.find(
        :all,
        :order => 'updated_at DESC, created_at DESC',
        :limit => 100
      )
      render :home
    end
  end
 
  class Pair < R '/proverb', '/proverb/(\d+)', '/proverb/(\d+)_.oOo._(.*)'
    def get(pair_id, slug=nil)
      @pair = L.Pair.find(pair_id)
      render :pair
    end
    def post(pair_id=nil, slug=nil)
      @state.errors = [ ]
      if input.submit == "RANDOM"
        redirect R(Random, nil)
        return
      end
      if input.topic.empty?
        @state.errors.push "You need to have learned something."
      end
      if input.teacher.empty?
        @state.errors.push "You must've learned this somehow."
      end
      if @state.errors.length == 0
        topic = L.Topic.find_or_create_by_word(input.topic.strip)
        teacher = L.Teacher.find_or_create_by_word(input.teacher.strip)
        pair = L.Pair.find(
          :first,
          :conditions => [ 'topic_id = ? AND teacher_id = ?', topic.id, teacher.id ]
        )
        if pair.nil?
          L.Pair.create(:topic_id => topic.id, :teacher_id => teacher.id)
        else
          pair.hits += 1
          pair.save
        end
      end
      @cgi_cookies[topic.word] = {
        :value => topic.word,
        :secure => true
      }
      @cgi_cookies[teacher.word] = {
        :value => teacher.word,
        :secure => false,
        :expires => Time.now + 4.years
      }
      redirect R(Home)
    end
  end
end