Skip to content

Commit

Permalink
Update with rubocop auto-corrections (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
JonJagger committed Dec 2, 2023
1 parent e9424af commit 026ed4a
Show file tree
Hide file tree
Showing 36 changed files with 582 additions and 662 deletions.
69 changes: 32 additions & 37 deletions app/app.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# frozen_string_literal: true

require_relative 'app_base'
require_relative 'prober'
require_relative 'helpers/app_helpers'

class App < AppBase

def initialize(externals)
super(externals)
@externals = externals
Expand All @@ -18,52 +18,52 @@ def initialize(externals)

# - - - - - - - - - - - - - - -

get '/show/:id', provides:[:html] do
get '/show/:id', provides: [:html] do
@id = params[:id]
respond_to { |wants|
wants.html {
respond_to do |wants|
wants.html do
erb :show
}
}
end
end
end

# - - - - - - - - - - - - - - -

get '/heartbeat/:id', provides:[:json] do
get '/heartbeat/:id', provides: [:json] do
# Process all traffic-lights into minute columns here in Ruby
# which can easily handle integers (unlike JS).
# Then let browser do all rendering in JS.
respond_to { |wants|
wants.json {
respond_to do |wants|
wants.json do
gather
time_ticks = modified(@time_ticks)
avatars = altered(@all_indexes, @gapped)
json({time_ticks:time_ticks, avatars:avatars})
}
}
json({ time_ticks: time_ticks, avatars: avatars })
end
end
end

# - - - - - - - - - - - - - - -

get '/progress/:id', provides:[:json] do
respond_to { |wants|
wants.json {
json(katas:avatars_progress)
}
}
get '/progress/:id', provides: [:json] do
respond_to do |wants|
wants.json do
json(katas: avatars_progress)
end
end
end

private

helpers AppHelpers

def altered(indexes, gapped)
Hash[indexes.map{|kata_id,group_index|
Hash[indexes.map do |kata_id, group_index|
[group_index, {
'kata_id':kata_id,
'lights':lights_json(gapped[kata_id])
'kata_id': kata_id,
'lights': lights_json(gapped[kata_id])
}]
}]
end]
end

def lights_json(minutes)
Expand All @@ -72,12 +72,12 @@ def lights_json(minutes)
# "1": { "collapsed":525 },
# "526": [ L,L ]
# }
Hash[minutes.map{|key,value| [key,minute_json(value)] }]
minutes.transform_values { |value| minute_json(value) }
end

def minute_json(minute)
if !collapsed?(minute)
minute.map{|light| light_json(light)}
minute.map { |light| light_json(light) }
else
minute
end
Expand All @@ -89,25 +89,21 @@ def collapsed?(section)

def light_json(light)
element = {
'index':light.index,
'colour':light.colour
'index': light.index,
'colour': light.colour
}
if light.predicted && light.predicted != 'none'
element['predicted'] = light.predicted
end
if light.revert
element['revert'] = light.revert
end
if light.checkout
element['checkout'] = light.checkout
end
element['predicted'] = light.predicted if light.predicted && light.predicted != 'none'
element['revert'] = light.revert if light.revert
element['checkout'] = light.checkout if light.checkout
element
end

# - - - - - - - - - - - - - - -

def modified(ticks)
ticks.inject({}) { |h, (k, v)| h[k] = dhm(v); h }
ticks.transform_values do |v|
dhm(v)
end
end

def dhm(value)
Expand All @@ -125,5 +121,4 @@ def time_tick(seconds)
days = (seconds / 60 / 60 / 24)
[days, hours, minutes]
end

end
46 changes: 23 additions & 23 deletions app/app_base.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# frozen_string_literal: true

require 'English'

require_relative 'silently'
require 'sinatra/base'
silently { require 'sinatra/contrib' } # N x "warning: method redefined"
Expand All @@ -8,7 +11,6 @@
require 'uglifier'

class AppBase < Sinatra::Base

def initialize(externals)
@externals = externals
super(nil)
Expand All @@ -23,7 +25,7 @@ def initialize(externals)
environment.append_path('app/assets/images')

def self.jquery_dialog_image(name)
get "/assets/images/#{name}", provides:[:png] do
get "/assets/images/#{name}", provides: [:png] do
env['PATH_INFO'].sub!('/assets/images', '')
settings.environment.call(env)
end
Expand All @@ -38,7 +40,7 @@ def self.jquery_dialog_image(name)
environment.append_path('app/assets/stylesheets')
environment.css_compressor = :sassc

get '/assets/app.css', provides:[:css] do
get '/assets/app.css', provides: [:css] do
respond_to do |format|
format.css do
env['PATH_INFO'].sub!('/assets', '')
Expand All @@ -50,9 +52,9 @@ def self.jquery_dialog_image(name)
# - - - - - - - - - - - - - - - - - - - - - -

environment.append_path('app/assets/javascripts')
environment.js_compressor = Uglifier.new(harmony: true)
environment.js_compressor = Uglifier.new(harmony: true)

get '/assets/app.js', provides:[:js] do
get '/assets/app.js', provides: [:js] do
respond_to do |format|
format.js do
env['PATH_INFO'].sub!('/assets', '')
Expand All @@ -64,13 +66,13 @@ def self.jquery_dialog_image(name)
private

def self.get_delegate(klass, name)
get "/#{name}", provides:[:json] do
get "/#{name}", provides: [:json] do
respond_to do |format|
format.json {
format.json do
target = klass.new(@externals)
result = target.public_send(name, params)
json({ name => result })
}
end
end
end
end
Expand All @@ -83,48 +85,47 @@ def json_args

def symbolized(h)
# named-args require symbolization
Hash[h.map{ |key,value| [key.to_sym, value] }]
h.transform_keys(&:to_sym)
end

def json_payload
json_hash_parse(request.body.read)
end

def json_hash_parse(body)
json = (body === '') ? {} : JSON.parse!(body)
unless json.instance_of?(Hash)
fail 'body is not JSON Hash'
end
json = body === '' ? {} : JSON.parse!(body)
raise 'body is not JSON Hash' unless json.instance_of?(Hash)

json
rescue JSON::ParserError
fail 'body is not JSON'
raise 'body is not JSON'
end

# - - - - - - - - - - - - - - - - - - - - - -

set :show_exceptions, false

error do
error = $!
error = $ERROR_INFO
status(500)
content_type('application/json')
info = {
exception: {
request: {
path:request.path,
body:request.body.read
path: request.path,
body: request.body.read
},
backtrace: error.backtrace
}
}
exception = info[:exception]
if error.instance_of?(::HttpJsonHash::ServiceError)
exception[:http_service] = {
path:error.path,
args:error.args,
name:error.name,
body:error.body,
message:error.message
path: error.path,
args: error.args,
name: error.name,
body: error.body,
message: error.message
}
else
exception[:message] = error.message
Expand All @@ -133,5 +134,4 @@ def json_hash_parse(body)
puts diagnostic
body diagnostic
end

end
5 changes: 1 addition & 4 deletions app/external_http.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# frozen_string_literal: true

require 'net/http'

class ExternalHttp

def get(uri)
KLASS::Get.new(uri)
end
Expand All @@ -13,8 +13,5 @@ def start(hostname, port, req)
end
end

private

KLASS = Net::HTTP

end
19 changes: 7 additions & 12 deletions app/external_saver.rb
Original file line number Diff line number Diff line change
@@ -1,34 +1,29 @@
# frozen_string_literal: true

require_relative 'http_json_hash/service'

class ExternalSaver

def initialize(http)
hostname = ENV['CYBER_DOJO_SAVER_HOSTNAME']
if hostname.nil?
hostname = 'saver'
end
hostname = 'saver' if hostname.nil?
port = ENV['CYBER_DOJO_SAVER_PORT']
if port.nil?
port = 4537
end
@http = HttpJsonHash::service(self.class.name, http, hostname, port)
port = 4537 if port.nil?
@http = HttpJsonHash.service(self.class.name, http, hostname, port)
end

def ready?
@http.get(__method__, {})
end

def group_manifest(id)
@http.get(__method__, { id:id })
@http.get(__method__, { id: id })
end

def group_joined(id)
@http.get(__method__, { id:id })
@http.get(__method__, { id: id })
end

def katas_events(ids, indexes)
@http.get(__method__, { ids:ids, indexes:indexes })
@http.get(__method__, { ids: ids, indexes: indexes })
end

end
2 changes: 0 additions & 2 deletions app/external_time.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
# frozen_string_literal: true

class ExternalTime

def now
t = Time.now
[t.year, t.month, t.day, t.hour, t.min, t.sec, t.usec]
end

end
4 changes: 2 additions & 2 deletions app/externals.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
# frozen_string_literal: true

require_relative 'external_http'
require_relative 'external_saver'
require_relative 'external_time'

class Externals

def saver
@saver ||= ExternalSaver.new(saver_http)
end

def saver_http
@saver_http ||= ExternalHttp.new
end

def time
@time ||= ExternalTime.new
end

end
3 changes: 1 addition & 2 deletions app/helpers/app_helpers.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
# frozen_string_literal: true

require_relative 'gatherer'
require_relative 'avatars_progress'

module AppHelpers

def time
externals.time
end

end

0 comments on commit 026ed4a

Please sign in to comment.