Class: ProblemChild::App

Inherits:
Sinatra::Base
  • Object
show all
Defined in:
lib/problem_child.rb

Instance Method Summary collapse

Instance Method Details

#anonymous_submissions?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/problem_child.rb', line 58

def anonymous_submissions?
  ENV["GITHUB_TOKEN"] && !ENV["GITHUB_TOKEN"].empty?
end

#auth!Object



91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/problem_child.rb', line 91

def auth!
  if anonymous_submissions?
    true
  elsif ENV['GITHUB_TEAM_ID']
    github_team_authenticate!(ENV['GITHUB_TEAM_ID'])
  elsif ENV['GITHUB_ORG_ID']
    github_organization_authenticate!(ENV['GITHUB_ORG_ID'])
  else
    raise "Must define GITHUB_TEAM_ID, GITHUB_ORG_ID, OR GITHUB_TOKEN"
    halt 401
  end
end

#clientObject



70
71
72
# File 'lib/problem_child.rb', line 70

def client
  @client ||= Octokit::Client.new :access_token => token
end

#create_issue(data = params) ⇒ Object



87
88
89
# File 'lib/problem_child.rb', line 87

def create_issue(data=params)
  client.create_issue(repo, form_data["title"], issue_body)
end

#form_dataObject

abstraction to allow cached form data to be used in place of default params



83
84
85
# File 'lib/problem_child.rb', line 83

def form_data
  session["form_data"].nil? ? params : JSON.parse(session["form_data"])
end

#issue_bodyObject



78
79
80
# File 'lib/problem_child.rb', line 78

def issue_body
  form_data.reject { |key, value| key == "title" }.map { |key,value| "* **#{key.humanize}**: #{value}"}.join("\n")
end

#render_template(template, locals = {}) ⇒ Object



74
75
76
# File 'lib/problem_child.rb', line 74

def render_template(template, locals={})
  halt erb template, :layout => :layout, :locals => locals.merge({ :template => template })
end

#repoObject



50
51
52
# File 'lib/problem_child.rb', line 50

def repo
  ENV["GITHUB_REPO"]
end

#tokenObject



62
63
64
65
66
67
68
# File 'lib/problem_child.rb', line 62

def token
  if anonymous_submissions?
    ENV["GITHUB_TOKEN"]
  elsif !user.nil?
    user.token
  end
end

#userObject



54
55
56
# File 'lib/problem_child.rb', line 54

def user
  env['warden'].user unless env['warden'].nil?
end