class PlayWhe::Get
Constants
- VALID_ORDERS
Attributes
fetcher[R]
parser[R]
Public Class Methods
new(fetcher:, parser:)
click to toggle source
# File lib/playwhe/get.rb, line 29 def initialize(fetcher:, parser:) @fetcher = fetcher @parser = parser end
Public Instance Methods
least_recent(limit: nil, order: :asc, skip_validation: false)
click to toggle source
# File lib/playwhe/get.rb, line 92 def least_recent(limit: nil, order: :asc, skip_validation: false) unless !!skip_validation limit = clean_and_validate_limit(limit) order = clean_and_validate_order(order) end start_year = BIRTHDAY.year end_year = Date.today.year delta = 1 results_for_range(start_year, end_year, delta, limit, order) end
most_recent(limit: nil, order: :desc, skip_validation: false)
click to toggle source
# File lib/playwhe/get.rb, line 79 def most_recent(limit: nil, order: :desc, skip_validation: false) unless !!skip_validation limit = clean_and_validate_limit(limit) order = clean_and_validate_order(order) end start_year = Date.today.year end_year = BIRTHDAY.year delta = -1 results_for_range(start_year, end_year, delta, limit, order) end
results_for_day(date, order: :asc, skip_validation: false)
click to toggle source
# File lib/playwhe/get.rb, line 64 def results_for_day(date, order: :asc, skip_validation: false) unless !!skip_validation date = clean_and_validate_date(date) order = clean_and_validate_order(order) end sort \ partition( parse(fetcher.get(year: date.year, month: date.month)).select { |r| r.date == date } ), order end
results_for_month(date, order: :asc, skip_validation: false)
click to toggle source
# File lib/playwhe/get.rb, line 50 def results_for_month(date, order: :asc, skip_validation: false) unless !!skip_validation date = clean_and_validate_date(date) order = clean_and_validate_order(order) end sort \ partition( parse(fetcher.get(year: date.year, month: date.month)), validation_context(date.year, date.month) ), order end
results_for_year(date, order: :asc, skip_validation: false)
click to toggle source
# File lib/playwhe/get.rb, line 36 def results_for_year(date, order: :asc, skip_validation: false) unless !!skip_validation date = clean_and_validate_date(date) order = clean_and_validate_order(order) end sort \ partition( parse(fetcher.get(year: date.year)), validation_context(date.year) ), order end
Private Instance Methods
clean_and_validate_date(date)
click to toggle source
# File lib/playwhe/get.rb, line 168 def clean_and_validate_date(date) if date.nil? raise ArgumentError, "date is required" else unless date.instance_of?(Date) begin date = Date.parse(date.to_s) rescue ArgumentError raise ArgumentError, "date must be a date" end end if date < BIRTHDAY || date > Date.today raise ArgumentError, "date must be between " \ "#{format_date(BIRTHDAY)} and " \ "#{format_date(Date.today)} inclusive" end end date end
clean_and_validate_limit(limit)
click to toggle source
# File lib/playwhe/get.rb, line 200 def clean_and_validate_limit(limit) limit = limit.to_i if limit < 0 raise ArgumentError, "limit must be a non-negative integer" end limit end
clean_and_validate_order(order)
click to toggle source
# File lib/playwhe/get.rb, line 190 def clean_and_validate_order(order) order = order.to_s.to_sym unless VALID_ORDERS.include?(order) raise ArgumentError, "order must be one of #{VALID_ORDERS.join(', ')}" end order end
format_date(date)
click to toggle source
# File lib/playwhe/get.rb, line 210 def format_date(date) date.strftime("%Y-%m-%d") end
parse(html_results)
click to toggle source
# File lib/playwhe/get.rb, line 160 def parse(html_results) parser.parse(html_results) end
partition(results, context = nil)
click to toggle source
# File lib/playwhe/get.rb, line 156 def partition(results, context = nil) results.partition { |r| r.is_valid?(context) } end
results_for_range(start_year, end_year, delta, limit, order)
click to toggle source
# File lib/playwhe/get.rb, line 107 def results_for_range(start_year, end_year, delta, limit, order) if limit.zero? sort \ partition( parse(fetcher.get(year: start_year)), validation_context(start_year) ), order else year = start_year all = [] while all.length < limit all.concat(parse(fetcher.get(year: year))) break if year == end_year year += delta end all = sort_desc(all) if start_year > end_year all = sort_asc(all) if start_year < end_year sort \ partition(all[0, limit]), order end end
sort(partitioned_results, order)
click to toggle source
# File lib/playwhe/get.rb, line 134 def sort(partitioned_results, order) [ send(:"sort_#{order}", partitioned_results.first), partitioned_results.last ] end
sort_asc(results)
click to toggle source
# File lib/playwhe/get.rb, line 141 def sort_asc(results) default_date = Date.today default_period_index = PERIODS.length results.sort_by do |r| [ r.date || default_date, PERIODS.index(r.period) || default_period_index ] end end
sort_desc(results)
click to toggle source
# File lib/playwhe/get.rb, line 152 def sort_desc(results) sort_asc(results).reverse end
validation_context(year, month = nil)
click to toggle source
# File lib/playwhe/get.rb, line 164 def validation_context(year, month = nil) OpenStruct.new(year: year, month: month) end