diff --git a/.gitignore b/.gitignore index ae3fdc2..302ee6d 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,7 @@ *.o *.a mkmf.log +.*.sw? +.ruby-version +.rbenv-gemsets +.rvmrc diff --git a/.rspec b/.rspec new file mode 100644 index 0000000..83e16f8 --- /dev/null +++ b/.rspec @@ -0,0 +1,2 @@ +--color +--require spec_helper diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..588dd60 --- /dev/null +++ b/.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 diff --git a/footstats.gemspec b/footstats.gemspec index 6e69192..8a78633 100644 --- a/footstats.gemspec +++ b/footstats.gemspec @@ -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 diff --git a/lib/footstats.rb b/lib/footstats.rb index 641cd32..8702547 100644 --- a/lib/footstats.rb +++ b/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... diff --git a/lib/footstats/racing/championship.rb b/lib/footstats/racing/championship.rb new file mode 100644 index 0000000..705465a --- /dev/null +++ b/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 diff --git a/lib/footstats/racing/driver.rb b/lib/footstats/racing/driver.rb new file mode 100644 index 0000000..672c560 --- /dev/null +++ b/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 diff --git a/lib/footstats/racing/gp.rb b/lib/footstats/racing/gp.rb new file mode 100644 index 0000000..143fbda --- /dev/null +++ b/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 diff --git a/lib/footstats/racing/narration.rb b/lib/footstats/racing/narration.rb new file mode 100644 index 0000000..4e641c4 --- /dev/null +++ b/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 diff --git a/lib/footstats/racing/ranking.rb b/lib/footstats/racing/ranking.rb new file mode 100644 index 0000000..9e8ce5a --- /dev/null +++ b/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 diff --git a/lib/footstats/racing/team.rb b/lib/footstats/racing/team.rb new file mode 100644 index 0000000..134e289 --- /dev/null +++ b/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 diff --git a/lib/footstats/request/base.rb b/lib/footstats/request/base.rb new file mode 100644 index 0000000..2e11d8b --- /dev/null +++ b/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 diff --git a/lib/footstats/request/request_racing.rb b/lib/footstats/request/request_racing.rb new file mode 100644 index 0000000..cf0e166 --- /dev/null +++ b/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 diff --git a/lib/footstats/resource.rb b/lib/footstats/resource.rb new file mode 100644 index 0000000..48e9753 --- /dev/null +++ b/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 diff --git a/spec/footstats/resource_spec.rb b/spec/footstats/resource_spec.rb new file mode 100644 index 0000000..4d327d1 --- /dev/null +++ b/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 diff --git a/spec/racing/driver_spec.rb b/spec/racing/driver_spec.rb new file mode 100644 index 0000000..b5b068a --- /dev/null +++ b/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 diff --git a/spec/request/request_racing_spec.rb b/spec/request/request_racing_spec.rb new file mode 100644 index 0000000..da5a677 --- /dev/null +++ b/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" 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" do + expect(subject.first).to be_kind_of(Footstats::Racing::Team) + end + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..3f1fd23 --- /dev/null +++ b/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