This repository was archived by the owner on Mar 7, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathapp.rb
More file actions
120 lines (95 loc) · 2.87 KB
/
app.rb
File metadata and controls
120 lines (95 loc) · 2.87 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
require 'airbrake'
require 'sinatra/base'
require 'tracker_api'
require 'rack/ssl-enforcer'
require 'redcarpet'
renderer = Redcarpet::Render::HTML.new(hard_wrap: true)
markdown = Redcarpet::Markdown.new(renderer)
class CardOMatic < Sinatra::Base
configure :production do
use Rack::SslEnforcer
if ENV["USE_AIRBRAKE"]
Airbrake.configure do |config|
config.api_key = ENV["AIRBRAKE_API_KEY"]
config.host = ENV["AIRBRAKE_HOST"]
config.port = 443
config.secure = true
config.params_filters << "api_key"
end
use Airbrake::Rack
enable :raise_errors
end
end
get '/' do
@intro = true
erb :start
end
post '/projects' do
client = setup_api_key
begin
@projects = client.projects
rescue TrackerApi::Error => e
render_previous_step_with_error(:start, "We couldn't connect with your API key.")
end
erb :projects
end
post '/iterations' do
client = setup_api_key
setup_project(client)
@iterations = fetch_iterations(@project)
erb :iterations
end
post '/render' do
client = setup_api_key
setup_project(client)
if params[:iteration].nil? || params[:iteration].empty?
@iterations = fetch_iterations(@project)
render_previous_step_with_error(:iterations, 'Please choose an iteration.')
end
@stories = case params[:iteration]
when 'icebox'
@project.stories(with_state: "unscheduled", fields: ':default,owners')
when 'backlog'
@project.iterations(scope: 'backlog', fields: ':default,stories(:default,owners)').first.stories
when /\d+/
iteration = params[:iteration].to_i
options = { limit: 1 }
options.merge!(fields: ':default,stories(:default,owners)')
options.merge!(offset: iteration-1) if iteration > 1
@project.iterations(options).first.stories
end
@with_qr_codes = params[:with_qr_codes] == 'true'
if @stories.any?
erb :cards, :layout => false
else
erb :no_cards
end
end
def setup_project(client)
begin
@project = client.project(params[:project_id].to_i)
raise InvalidProjectId unless @project
rescue TrackerApi::Error
raise InvalidProjectId
end
rescue InvalidProjectId
@projects = client.projects
render_previous_step_with_error(:projects, 'Please choose a project to print cards for.')
end
def setup_api_key
@api_key = params[:api_key]
if @api_key.nil? || @api_key.empty?
render_previous_step_with_error(:start, 'Please enter an API key')
end
TrackerApi::Client.new(token: @api_key)
end
def fetch_iterations(project)
start = [1, project.current_iteration_number-4].max
(start..project.current_iteration_number)
end
def render_previous_step_with_error(view, error)
@error = error
halt(400, erb(view))
end
class InvalidProjectId < StandardError; end
end