Skip to content
This repository has been archived by the owner on Aug 29, 2018. It is now read-only.

Commit

Permalink
[development] Create basic gem struct.
Browse files Browse the repository at this point in the history
-- Warnning: Broken specs
  • Loading branch information
Paulo Patto committed Feb 9, 2015
1 parent a6a56c6 commit 61363b3
Show file tree
Hide file tree
Showing 18 changed files with 329 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Expand Up @@ -12,3 +12,7 @@
*.o
*.a
mkmf.log
.*.sw?
.ruby-version
.rbenv-gemsets
.rvmrc
2 changes: 2 additions & 0 deletions .rspec
@@ -0,0 +1,2 @@
--color
--require spec_helper
12 changes: 12 additions & 0 deletions .travis.yml
@@ -0,0 +1,12 @@
language: ruby

rvm:
- 1.9.3
- 2.0.0
- 2.1.1
- 2.1.2
- 2.1.5
- jruby

notifications:
- paulopatto@gmail.com
7 changes: 7 additions & 0 deletions footstats.gemspec
Expand Up @@ -12,12 +12,19 @@ Gem::Specification.new do |spec|
spec.description = %q{TODO: Write a longer description. Optional.}
spec.homepage = ""
spec.license = "MIT"
spec.required_ruby_version = ">= 1.9.3"

spec.files = `git ls-files -z`.split("\x0")
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ["lib"]

spec.add_dependency "json"
spec.add_dependency "httparty"

spec.add_development_dependency "bundler", "~> 1.7"
spec.add_development_dependency "rake", "~> 10.0"
spec.add_development_dependency "rspec", " ~> 3.1"
spec.add_development_dependency "simplecov"
spec.add_development_dependency "pry-nav"
end
5 changes: 5 additions & 0 deletions lib/footstats.rb
@@ -1,4 +1,9 @@
require 'ostruct'
require 'json'
require 'csv'

require "footstats/version"
require 'footstats/request/request_racing'

module Footstats
# Your code goes here...
Expand Down
6 changes: 6 additions & 0 deletions lib/footstats/racing/championship.rb
@@ -0,0 +1,6 @@
require './lib/footstats/resource'

class Footstats::Racing::Championship < Footstats::Resource
field :drivers, :Pilotos
field :teams, :Escuderias
end
20 changes: 20 additions & 0 deletions lib/footstats/racing/driver.rb
@@ -0,0 +1,20 @@
require './lib/footstats/resource'
module Footstats
module Racing
class Driver < Footstats::Resource
field :id, :Id
field :name, :Nome
field :brith, :Nascimento
field :points, :PontosConquistados
field :podiums, :Podios
field :first_gp, :PrimeiroGP
field :nickname, :Apelido
field :brith_place, :PaisOrigem
field :nationality, :Nacionalidade
field :fastest_laps, :VoltasRapidas
field :word_championships, :Titulos
field :highest_grid_position, :Vitorias
field :highest_race_position, :PolesPositions
end
end
end
17 changes: 17 additions & 0 deletions lib/footstats/racing/gp.rb
@@ -0,0 +1,17 @@
require './lib/footstats/resource'

class Footstats::Racing::GP < Footstats::Resource
field :id, :Id
field :track_name, :AutodromoName
field :track_nickname, :AutodromoApelido
field :distance, :DisatnciaPista
field :location, :Local
field :brazilian_time, :DataHoraBrasil
field :local_time, :DataHoraLocal
field :laps, :QtdVoltas
field :status, :Status
field :atual_lap, :VoltaAtual
field :active, :Ativo
field :city, :Cidade
field :country, :Pais
end
9 changes: 9 additions & 0 deletions lib/footstats/racing/narration.rb
@@ -0,0 +1,9 @@
require './lib/footstats/resource'

class Footstats::Racing::Narration < Footstats::Resource
field :id, :Id
field :status, :status
field :current_lap, :VoltaAtual
field :datetime, :DataHora
field :comment, :Comentario
end
11 changes: 11 additions & 0 deletions lib/footstats/racing/ranking.rb
@@ -0,0 +1,11 @@
require './lib/footstats/resource'

class Footstats::Racing::Ranking < Footstats::Resource
field :position, :Posicao
field :driver_id, :IdPiloto
field :driver_name, :NomePiloto
field :best_lap, :MelhorVolta
field :team, :Escuderia
field :acronym, :Sigla
field :type, :Tipo
end
9 changes: 9 additions & 0 deletions lib/footstats/racing/team.rb
@@ -0,0 +1,9 @@
require './lib/footstats/resource'

class Footstats::Racing::Team < Footstats::Resource
field :id, :Id
field :name, :Nome
field :nickname, :Apelido
field :site, :Site
field :country, :SiglaPais
end
17 changes: 17 additions & 0 deletions lib/footstats/request/base.rb
@@ -0,0 +1,17 @@
require 'httparty'

module Footstats
module Request
class Base
include HTTParty
TOKEN = ENV['FOOTSTATS_TOKEN']

protected
def self.request( method = "", api = "", params={} )
params.merge({ token: TOKEN })

JSON.parse(get("#{api}/#{method}", { query: params }).body)
end
end
end
end
72 changes: 72 additions & 0 deletions lib/footstats/request/request_racing.rb
@@ -0,0 +1,72 @@
require './lib/footstats/request/base'
require './lib/footstats/racing/driver.rb'
require './lib/footstats/racing/team.rb'

module Footstats
module Request
class RequestRacing < Base
API = "http://apicorrida.footstats.com.br/api"

def self.drivers
request("Piloto/ListaPilotos").map { |driver| Footstats::Racing::Driver.new(driver) }
end

def self.teams
request("Escuderia/ListaEscuderias").map { |team| Footstats::Racing::Team.new(team) }
end

def self.gps
request("GP/Calendario").map { |gp| Footstats::Racing::GP.new(gp) }
end

# Retorna uma lista com os comentários sobre o GP.
# :offset (MaxId) é o Id do comentário, quando um valor é passado ele
# só retorna os comentários posteriores.
# Quando o valor passado for 0 ele retorna todos os comentários do GP.
#
# Latência de chamada:
# --------------------
# 1 Minuto enquanto o status do GP for:
#
# - Treino Livre 1
# - Treino Livre 2
# - Treino Livre 3
# - Q1
# - Q2
# - Q3
# - Corrida
def self.narration(gp_id, offset = 0)
request("GP/Narracao", {idGP: gp_id, MaxId: offset}).map do |narration|
Footstats::Racing::Narration.new(narration)
end
end

# Lista as classificações dos
# - Treinos Livres
# - Qualify
# - Corrida
#
# Latência de chamada:
# --------------------
# 1 Minuto enquanto o status do GP for
#
# - Treino Livre 1
# - Treino Livre 2
# - Treino Livre 3
# - Q1
# - Q2
# - Q3
# - Corrida
def self.rankings(gp_id)
request("GP/ClassificacaoGP").map do |fase|
binding.pry
end
end

protected
def self.request(method, params = {})
super(method, API, params)
end
end
end
end
20 changes: 20 additions & 0 deletions lib/footstats/resource.rb
@@ -0,0 +1,20 @@
module Footstats
class Resource
@@fields = {}

def initialize(attributes={})
attributes.each do |attr_name, value|
if @@fields.include? attr_name.to_sym
send("#{@@fields[attr_name.to_sym]}=", value)
end
end
end

def self.field(internal_key, external_key)
@@fields[external_key] = internal_key

attr_accessor internal_key
end

end
end
39 changes: 39 additions & 0 deletions spec/footstats/resource_spec.rb
@@ -0,0 +1,39 @@
require 'spec_helper'
require './lib/footstats/resource'

describe Footstats::Resource do
subject { described_class }

describe ".field" do
before do
subject.field(:test, 'Test')
end

it "adds given attribute a reader" do
expect(described_class.new).to respond_to :test
end

it "adds given attribute a writer" do
expect(described_class.new).to respond_to :test=
end
end

describe ".new" do
context "when sends pt-br attributes" do
let(:name) { "Bruce Wayne" }
let(:resource) { subject.new({"Nome" => name}) }

before do
subject.field :name, :Nome
end

it "transforms hash key Name in attr name" do
expect(resource).to be_respond_to(:name)
end

it "puts value of key Name in attr name" do
expect(resource.name).to eq(name)
end
end
end
end
27 changes: 27 additions & 0 deletions spec/racing/driver_spec.rb
@@ -0,0 +1,27 @@
require 'spec_helper'
require './lib/footstats/racing/driver'

describe Footstats::Racing::Driver do
subject { described_class }

describe ".new" do
let (:attributes) do
{
Nome: "Lewis Hamilton",
Nascimento: "08/03/1987",
Vitorias: "38"
}
end

let(:driver) { subject.new(attributes) }

it "sets correct name attribute" do
exepct(driver.name).to_not be_nil
expect(driver.name).to eq("Lewis Hamilton")
end

it "doesn't set incorrect attr" do
expect(driver).to_not be_respond_to(:Nome)
end
end
end
23 changes: 23 additions & 0 deletions spec/request/request_racing_spec.rb
@@ -0,0 +1,23 @@
require './lib/footstats/request/request_racing'

describe Footstats::Request::RequestRacing do
describe ".drivers" do
subject { described_class.drivers }

it "list all drivers as collection :array" do
expect(subject).to be_kind_of(Array)
end

it "list all drivers as collection Array<Driver>" do
expect(subject.first).to be_kind_of(Footstats::Racing::Driver)
end
end

describe ".teams" do
subject { described_class.teams }

it "list all teams as collection Array<Team>" do
expect(subject.first).to be_kind_of(Footstats::Racing::Team)
end
end
end
29 changes: 29 additions & 0 deletions spec/spec_helper.rb
@@ -0,0 +1,29 @@
require 'pry'
require 'footstats'

RSpec.configure do |config|
config.color = true
config.default_formatter = 'doc'
config.order = :random
config.filter_run :focus
config.expect_with :rspec do |expectations|
# This option will default to `true` in RSpec 4. It makes the `description`
# and `failure_message` of custom matchers include text for helper methods
# defined using `chain`, e.g.:
# be_bigger_than(2).and_smaller_than(4).description
# # => "be bigger than 2 and smaller than 4"
# ...rather than:
# # => "be bigger than 2"
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end
config.warnings = true

# rspec-mocks config goes here. You can use an alternate test double
# library (such as bogus or mocha) by changing the `mock_with` option here.
config.mock_with :rspec do |mocks|
# Prevents you from mocking or stubbing a method that does not exist on
# a real object. This is generally recommended, and will default to
# `true` in RSpec 4.
mocks.verify_partial_doubles = true
end
end

0 comments on commit 61363b3

Please sign in to comment.