-
Notifications
You must be signed in to change notification settings - Fork 1
/
application.rb
192 lines (168 loc) · 5.27 KB
/
application.rb
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# encoding: UTF-8
require 'rubygems'
require 'bundler/setup'
require_relative './environment'
require 'barista'
require 'sinatra/partial'
require 'haml'
require 'will_paginate'
require 'will_paginate/data_mapper'
require "will_paginate-bootstrap"
require 'sinatra/json'
require 'sinatra/config_file'
# TODO: memcache? for Rack::Cache?
WillPaginate.per_page = 20
class TexAppOrg < Sinatra::Base
register Sinatra::Partial
register Sinatra::ConfigFile
config_file 'config/credentials.yml'
helpers WillPaginate::Sinatra::Helpers
# automatic CoffeeScript compilation
register Barista::Integration::Sinatra
Barista.configure do |b|
b.root = File.join(root, 'coffeescripts')
end
set :haml, { :format => :html5 }
# citation links
get %r{^/(\d\d-\d\d-\d\d\d\d.+)} do
docket_number = params[:captures].first
md5sum = params[:md5]
if md5sum && md5sum.length == 32
filename = Opinion.filename_for docket_number, md5sum
container = $cloudfiles.container(CONTAINER)
object = container.object(filename)
redirect object.public_url
else
court_case = Case.first :docket_number => docket_number
opinions = court_case.opinions
if opinions.count == 1
opinion = opinions.first
filename = opinion.filename
container = $cloudfiles.container(CONTAINER)
object = container.object(filename)
attachment "#{docket_number}.pdf"
redirect object.public_url
elsif opinions.count > 1
redirect "/case/#{docket_number}"
else
haml :bad_citation
end
end
end
get "/case/:docket" do
docket_number = params[:docket]
@the_case = Case.first :docket_number => docket_number
haml :case
end
FORMAT = "%m-%d-%Y"
DATA_FORMAT = "yyyy-mm-dd"
VALUE_FORMAT = "%Y-%m-%d"
get "/court/:court" do
@court = params[:court].to_i
@date = Date.strptime(params[:date], FORMAT).strftime(FORMAT) rescue nil if params[:date]
@today = Date.today.strftime FORMAT
conditions = {
:case => { :court => @court },
:page => params[:page],
:order => [:date.desc]
}
conditions[:date] = Date.strptime(@date, FORMAT) if @date
@opinions = Opinion.paginate(conditions)
haml :simple, :locals => {
:heading => cardinal_name(@court),
:subheading => CITIES[@court]
}
end
get "/recent" do
@date = Date.strptime(params[:date], FORMAT).strftime(FORMAT) rescue nil if params[:date]
@today = Date.today.strftime FORMAT
conditions = {
:page => params[:page],
:order => [:date.desc]
}
conditions[:date] = Date.strptime(@date, FORMAT) if @date
@opinions = Opinion.paginate(conditions)
haml :simple, :locals => { :heading => 'Recent Opinions' }
end
get '/' do haml :index end
get '/about' do
haml :markdown, :locals => {
:heading => 'About',
:file => 'about.md'
}
end
get '/citation' do
haml :markdown, :locals => {
:heading => 'Citation',
:file => 'citation.md'
}
end
get '/technology' do
haml :markdown, :locals => {
:heading => 'Technology',
:file => 'technology.md'
}
end
get '/search' do
@query = params[:query]
redirect "/" unless @query
@opinions = Opinion.paginate(
:order => [:date.desc]
)
haml :simle, :locals => {
:heading => 'Search Results',
:subheading => "“#{@query}”"
}
end
get '/statistics' do
@from = Date.parse(params[:from], VALUE_FORMAT) rescue Date.today - 30
@through = Date.parse(params[:through], VALUE_FORMAT) rescue Date.today
@courts = params[:courts].map(&:to_i) rescue 1..14
haml :statistics, :locals => {:d3 => true, :js => 'coffeescripts/statistics.js' }
end
get '/statistics.json' do
@from = Date.parse(params[:from], VALUE_FORMAT) rescue Date.today - 30
@through = Date.parse(params[:through], VALUE_FORMAT) rescue Date.today
@courts = params[:courts].map(&:to_i) rescue 1..14
opinions = Opinion.all(
:case => {:court => @courts },
:date.gte => @from,
:date.lte => @through
)
result = @courts.map do |court|
{
:name => "#{CARDINAL_DIGITS[court]} #{CITIES[court]}",
:data => (@from..@through).map { |day|
count =
{
:x => day.strftime('%s').to_i,
:y => opinions.inject(0) { |mem, opinion|
if opinion.case.court == court && opinion.date == day
mem + 1
else
mem
end
}
}
}
}
end
json result
end
helpers Sinatra::JSON
helpers do
CARDINALS = %w{_ First Second Third Fourth Fifth Sixth Seventh Eigth Ninth Tenth Eleventh Twelfth Thirteenth Fourteenth}
CARDINAL_DIGITS = %w{_ 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th}
CITIES = %w{_ Houston Fort\ Worth Austin San\ Antonio Dallas Texarkana Amarillo El\ Paso Beaumont Waco Eastland Tyler Corpus\ Christi/Edinburg Houston}
def court_name(number)
cardinal_name(number) + "—#{CITIES[number]}"
end
def cardinal_name(number)
"#{CARDINALS[number]} Court of Appeals"
end
MONTHS = %w{_ Jan. Feb. Mar. Apr. May June July Aug. Sept. Oct. Nov. Dec.}
def bluebook_date(date)
"#{MONTHS[date.month]} #{date.mday}, #{date.year}"
end
end
end