Skip to content

Commit

Permalink
Fix some Rubocop complaints
Browse files Browse the repository at this point in the history
  • Loading branch information
ferranpm committed Mar 5, 2024
1 parent 976a84b commit 30d139d
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 31 deletions.
20 changes: 18 additions & 2 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
AllCops:
TargetRubyVersion: 2.6
NewCops: enable

Layout/AccessModifierIndentation:
EnforcedStyle: outdent

Layout/LineLength:
Max: 120

Naming/VariableNumber:
EnforcedStyle: snake_case

Style/StringLiterals:
Enabled: true
Expand All @@ -9,5 +19,11 @@ Style/StringLiteralsInInterpolation:
Enabled: true
EnforcedStyle: double_quotes

Layout/LineLength:
Max: 120
Style/TrailingCommaInArguments:
EnforcedStyleForMultiline: consistent_comma

Style/TrailingCommaInArrayLiteral:
EnforcedStyleForMultiline: consistent_comma

Style/TrailingCommaInHashLiteral:
EnforcedStyleForMultiline: consistent_comma
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

source "https://rubygems.org"

ruby "3.0.4"
Expand Down
70 changes: 41 additions & 29 deletions lib/renfe.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# frozen_string_literal: true

require 'nokogiri'
require 'open-uri'
require_relative './renfe/version'
require "nokogiri"
require "open-uri"
require_relative "renfe/version"

module Renfe
class Nucleo
Expand All @@ -26,7 +26,7 @@ def initialize(id, nombre)
{ id: 61, nombre: "San Sebastián" },
{ id: 62, nombre: "Santander" },
{ id: 70, nombre: "Zaragoza" },
]
].freeze

def self.all
NUCLEOS.map do |nucleo|
Expand Down Expand Up @@ -64,7 +64,7 @@ def initialize(origen, destino, hora_inicio: 0, hora_fin: 26, date: Date.today)
@date = date
end

attr_reader :origen, :destino, :hora_inicio, :hora_fin, :date
attr_reader :origen, :destino

def horas
@horas ||= parse_page
Expand Down Expand Up @@ -97,52 +97,64 @@ def parse_page
end

def transbordo?
table.css('tr')[0].css('td').size > 5
table.at_css("tr").css("td").size > 5
end

def parse_con_transbordo
prev = nil
table.css('tr')[4..-1].map do |tr|
row = tr.css('td').map(&:text).map(&:strip)
linea_1 = presence(row[0]) || prev && prev[0]
hora_inicio_1 = presence(row[2]) || prev && prev[2]
hora_fin_1 = presence(row[3]) || prev && prev[3]
linea_2 = presence(row[5]) || prev && prev[5]
hora_inicio_2 = presence(row[4]) || prev && prev[4]
hora_fin_2 = presence(row[7]) || prev && prev[7]
prev = row
ItinerarioDoble.new(linea_1, hora_inicio_1, hora_fin_1, linea_2, hora_inicio_2, hora_fin_2)
table.css("tr")[4..].map do |tr|
row = tr.css("td").map(&:text).map(&:strip)

build_itinerario_doble(row, prev)
end
end

def build_itinerario_doble(row, prev)
ItinerarioDoble.new(
row[0] || prev[0],
row[2] || prev[2],
row[3] || prev[3],
row[5] || prev[5],
row[4] || prev[4],
row[7] || prev[7],
)
end

def parse_sin_transbordo
table.css('tr')[1..-1].map do |tr|
row = tr.css('td').map(&:text).map(&:strip)
linea = row[0]
hora_inicio = row[2]
hora_fin = row[3]
ItinerarioSimple.new(linea, hora_inicio, hora_fin)
table.css("tr")[1..].map do |tr|
row = tr.css("td").map(&:text).map(&:strip)

build_itinerario_simple(row)
end
end

def build_itinerario_simple(row)
ItinerarioSimple.new(row[0], row[2], row[3])
end

def table
page.css('table')[0]
page.at_css("table#tabla")
end

def page
Nokogiri::HTML(uri.read)
end

def uri
URI.parse("https://horarios.renfe.com/#{params}")
uri = URI.parse("https://horarios.renfe.com/cer/hjcer310.jsp")
uri.query = URI.encode_www_form(params)
uri
end

def params
"cer/hjcer310.jsp?&f1=&df=#{date.strftime("%Y%m%d")}&TXTInfo=&hd=#{hora_fin}&d=#{destino.id}&i=s&cp=NO&nucleo=#{origen.nucleo.id}&o=#{origen.id}&ho=#{hora_inicio}"
end

def presence(item)
item.nil? || item.empty? ? nil : item
{
"f1" => "",
"TXTInfo" => "",
"i" => "s",
"cp" => "NO",
"nucleo" => origen.nucleo.id, "o" => origen.id, "d" => destino.id,
"df" => date.strftime("%Y%m%d"), "ho" => hora_inicio, "hd" => hora_fin,
}
end
end

Expand Down

0 comments on commit 30d139d

Please sign in to comment.