Skip to content

Commit

Permalink
Adapt to some Rubocop conventions
Browse files Browse the repository at this point in the history
  • Loading branch information
glacials committed Apr 9, 2016
1 parent 4bdf2fe commit bae9fc3
Show file tree
Hide file tree
Showing 19 changed files with 302 additions and 339 deletions.
1 change: 1 addition & 0 deletions app/models/run.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class Run < ActiveRecord::Base

has_secure_token :claim_token

after_create -> { parse(fast: true) }
after_create :refresh_game
after_create :discover_runner

Expand Down
55 changes: 24 additions & 31 deletions lib/speedrundotcom.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,41 +8,35 @@ def self.runner_id(id)
res = get(id)
body = JSON.parse(res.body)

raise MalformedResponse unless body.respond_to?(:[])
raise MalformedResponse unless body['data'].respond_to?(:[])
raise MalformedResponse unless body['data']['players'].respond_to?(:[])
raise MalformedResponse unless body['data']['players'][0].respond_to?(:[])
raise MalformedResponse unless body['data']['players'][0]['id'].present?

body['data']['players'][0]['id']
end

def self.id_from_url(url)
if url.blank?
return nil
end
return nil if url.blank?

uri = URI.parse(url)
unless uri.host =~ /^(www\.)?(speedrun.com)$/ && uri.path =~ /^\/run\/(.*)$/
unless uri.host =~ /^(www\.)?(speedrun.com)$/ && uri.path =~ %r{^/run/(.*)$}
return false
end

/^\/run\/(.*)$/.match(uri.path)[1]
%r{^/run/(.*)$}.match(uri.path)[1]
end

def self.url_from_id(id)
return nil if id.blank?
"http://www.speedrun.com/run/#{id}"
end

private
class << self
private

def self.get(id)
route(id).get
end
def get(id)
route(id).get
end

def self.route(id)
SpeedrunDotCom.route["/runs/#{id}"]
def route(id)
SpeedrunDotCom.route["/runs/#{id}"]
end
end
end

Expand All @@ -51,28 +45,27 @@ def self.twitch_login(id)
res = get(id)
body = JSON.parse(res.body)

raise MalformedResponse unless body.respond_to?(:[])
raise MalformedResponse unless body['data'].respond_to?(:[])
raise MalformedResponse.new(body) unless body['data']['twitch'].respond_to?(:[])
raise MalformedResponse unless body['data']['twitch']['uri'].present?

Twitch::User.login_from_url(body['data']['twitch']['uri'])
end

private
class << self
private

def self.get(id)
route(id).get
end
def get(id)
route(id).get
end

def self.route(id)
SpeedrunDotCom.route["/users/#{id}"]
def route(id)
SpeedrunDotCom.route["/users/#{id}"]
end
end
end

private
class << self
private

def self.route
RestClient::Resource.new('http://speedrun.com/api/v1')
def route
RestClient::Resource.new('http://speedrun.com/api/v1')
end
end
end
2 changes: 1 addition & 1 deletion lib/speedrunslive.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class SpeedRunsLive
def self.game(name)
(HTTParty.get(URI::parse("http://api.speedrunslive.com/games").tap do |uri|
(HTTParty.get(URI.parse('http://api.speedrunslive.com/games').tap do |uri|
uri.query = {search: name}.to_param
end) || {'games' => []}
)['games'].select { |game| game['name'] == name }[0]
Expand Down
14 changes: 8 additions & 6 deletions lib/tugnut.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ def self.parse(string)
e.response
end

private
class << self
private

def self.req(string)
client.post(splits: string, multipart: true)
end
def req(string)
client.post(splits: string, multipart: true)
end

def self.client
RestClient::Resource.new('https://tugnut.splits.io/parse/livesplit')
def client
RestClient::Resource.new('https://tugnut.splits.io/parse/livesplit')
end
end
end
38 changes: 22 additions & 16 deletions lib/twitch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,19 @@ class NotFound < Error; end

module User
def self.login_from_url(twitch_url)
/^https?:\/\/(?:www\.)?twitch\.tv\/([^\/]+)(?:.*)$/.match(twitch_url)[1]
%r{^https?://(?:www\.)?twitch\.tv/([^/]+)(?:.*)$}.match(twitch_url)[1]
end

private
class << self
private

def self.get(login)
route(login).get
end
def get(login)
route(login).get
end

def self.route(login)
Twitch.route["/users/#{login}"]
def route(login)
Twitch.route["/users/#{login}"]
end
end
end

Expand All @@ -31,20 +33,24 @@ def self.followed_ids(login)
end
end

private
class << self
private

def self.get(login)
route(login).get
end
def get(login)
route(login).get
end

def self.route(login)
Twitch::User.route(login)["/follows/channels?limit=500"]
def route(login)
Twitch::User.route(login)['/follows/channels?limit=500']
end
end
end

private
class << self
private

def self.route
RestClient::Resource.new('https://api.twitch.tv/kraken')
def route
RestClient::Resource.new('https://api.twitch.tv/kraken')
end
end
end
5 changes: 4 additions & 1 deletion spec/controllers/api/v3/runs_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@

it 'returns the correct run' do
returned_attributes.each do |attribute|
expect(body[attribute.to_s]).to eq(run.send(attribute))
expect(body[attribute.to_s]).to(
eq(run.send(attribute)),
"expected #{attribute} to be #{run.send(attribute)}, got #{body[attribute.to_s]}"
)
end
end
end
Expand Down
26 changes: 12 additions & 14 deletions spec/controllers/api/v4/converts_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -1,37 +1,36 @@
require 'rails_helper'

describe Api::V4::ConvertsController do

describe '#create' do
context 'from Llanfair' do
let(:file) do
fixture_file_upload('files/llanfair')
end
context 'supported format' do
subject { post :create, params: {file: file, format: "json", historic: "1"} }
subject { post :create, params: {file: file, format: 'json', historic: '1'} }
let(:body) { JSON.parse(subject.body) }

it "returns a 200" do
it 'returns a 200' do
expect(subject).to have_http_status 200
end

it "doesn't include id" do
expect(body['id']).to be_nil
end

it "has the correct splits" do
expect(body["splits"].map { |s| [s["name"], s["duration"]] }).to eq([
["Spiral Mountain", 211.23],
it 'has the correct splits' do
expect(body['splits'].map { |s| [s['name'], s['duration']] }).to eq [
['Spiral Mountain', 211.23],
["Mumbo's Mountain", 808.2]
])
]
end
end

context 'unsupported format' do
subject { post :create, params: {file: file, format: "llanfair" } }
subject { post :create, params: {file: file, format: 'llanfair'} }
let(:body) { JSON.parse(subject.body) }

it "returns a 400" do
it 'returns a 400' do
expect(subject).to have_http_status 400
end

Expand All @@ -41,11 +40,11 @@
end
end

context "missing parameter" do
context 'missing parameter' do
subject { post :create, params: {file: file} }
let(:body) { JSON.parse(subject.body) }

it "returns a 400" do
it 'returns a 400' do
expect(subject).to have_http_status 400
end

Expand All @@ -60,10 +59,10 @@
let(:file) do
fixture_file_upload('files/malformed')
end
subject { post :create, params: {file: file, format: "json"} }
subject { post :create, params: {file: file, format: 'json'} }
let(:body) { JSON.parse(subject.body) }

it "returns a 400" do
it 'returns a 400' do
expect(subject).to have_http_status 400
end

Expand All @@ -73,5 +72,4 @@
end
end
end

end
29 changes: 16 additions & 13 deletions spec/controllers/api/v4/runs/splits_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,55 @@

describe Api::V4::Runs::SplitsController do
describe '#index' do
context "for a nonexistent run" do
context 'for a nonexistent run' do
subject { get :index, params: {run_id: '0'} }

it "returns a 404" do
it 'returns a 404' do
expect(subject).to have_http_status 404
end

it "returns no body" do
it 'returns no body' do
expect(response.body).to be_blank
end
end

context "for a bogus ID" do
context 'for a bogus ID' do
subject { get :index, params: {run_id: '/@??$@;[1;?'} }

it "returns a 404" do
it 'returns a 404' do
expect(subject).to have_http_status 404
end

it "returns no body" do
it 'returns no body' do
expect(response.body).to be_blank
end
end

context "for an existing run" do
context 'for an existing run' do
let(:run) { create(:run) }
subject { get :index, params: {run_id: run.id36} }
let(:body) { JSON.parse(subject.body) }

it "returns a 200" do
it 'returns a 200' do
expect(subject).to have_http_status 200
end

it "returns a body with no root node" do
expect(body[0]['name']).to eq "Tron City"
it 'returns a body with no root node' do
expect(body[0]['name']).to eq 'Tron City'
end

it "doesn't include history" do
expect(body[0]['history']).to be_nil
end

context "with a historic=1 parameter" do
context 'with a historic=1 parameter' do
subject { get :index, params: {run_id: run.id36, historic: '1'} }

it "includes history" do
expect(body[0]['history']).to eq [54.279395, 57.5447017, 57.234414, 53.2568428, 50.3245258, 55.0088332, 50.736531, 53.44994, 53.739547, 53.4956392, 55.2457862, 49.7325244, 56.2357967, 54.4453876, 52.9656984, 53.9219256]
it 'includes history' do
expect(body[0]['history']).to eq [
54.279395, 57.5447017, 57.234414, 53.2568428, 50.3245258, 55.0088332, 50.736531, 53.44994, 53.739547,
53.4956392, 55.2457862, 49.7325244, 56.2357967, 54.4453876, 52.9656984, 53.9219256
]
end
end
end
Expand Down
Loading

0 comments on commit bae9fc3

Please sign in to comment.