Class: Perkins::Server

Inherits:
Sinatra::Application
  • Object
show all
Defined in:
lib/perkins/server.rb

Constant Summary collapse

@@conns =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Server

Returns a new instance of Server.



211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/perkins/server.rb', line 211

def initialize(args={})
  super
  #return if args.blank?
  app = Perkins::Application.instance
  
  puts "PerkinsCI #{settings.environment} environment loaded on #{app.host}:#{app.port}".green
  
  self.class.set :perkins_application, app
  self.class.set :github_options, {
    :scopes => "admin:repo_hook,repo,user:email",
    :secret => app.github_client_secret,
    :client_id => app.github_client_id,
  }
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



19
20
21
# File 'lib/perkins/server.rb', line 19

def app
  @app
end

Class Method Details

.start(app, options) ⇒ Object

TODO: deprecate this



257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/perkins/server.rb', line 257

def self.start(app, options)
  options = options.dup
  ENV['RACK_ENV'] = options.delete("e")

  register Sinatra::ActiveRecordExtension
  app = app.is_a?(String) ? eval(File.open(app).read) : app
  self.start_listener(app)

  configure :development do
    #register Sinatra::Reloader
    #set :database, 'sqlite:///db/development.sqlite3'
  end

  configure :production do
    #set :database, 'sqlite:///db/productions.sqlite3'
  end

  configure :test do
    #set :database, 'sqlite:///db/test.sqlite3'
  end

end

.start_listener(app_config) ⇒ Object



280
281
282
283
284
285
286
# File 'lib/perkins/server.rb', line 280

def self.start_listener(app_config)
  fork do
    listener = ::Perkins::Listener.new
    listener.app = app_config
    listener.run!
  end
end

Instance Method Details

#build_dataObject



230
231
232
233
# File 'lib/perkins/server.rb', line 230

def build_data
  Repo.sync_github_repos(github_user)
  redirect "/me"
end

#default_dataObject



289
290
291
# File 'lib/perkins/server.rb', line 289

def default_data
  { year: Time.now.year, app: @app}
end

#find_repo(params) ⇒ Object



239
240
241
242
243
# File 'lib/perkins/server.rb', line 239

def find_repo(params)
  id = "#{params[:login]}/#{params[:name]}"
  repo = Repo.find_by(name: id)
  repo
end

#github_reposObject



226
227
228
# File 'lib/perkins/server.rb', line 226

def github_repos
  @github_repos ||= persisted_repos.any? ? persisted_repos : build_data
end

#persisted_reposObject



235
236
237
# File 'lib/perkins/server.rb', line 235

def persisted_repos
  Repo.synced_records
end

#request_badge(opts = {}) ⇒ Object



245
246
247
248
249
250
251
252
253
# File 'lib/perkins/server.rb', line 245

def request_badge(opts={})
  begin
    image_path = "https://img.shields.io/badge/build-#{opts[:status]}-#{opts[:color]}.svg?style=flat-square"
    url = URI.parse(image_path)
    result = Net::HTTP.get(url)
  rescue => e
    e
  end
end