Sha256: 2c1e1ea8998a996f9b29a178af12587d5ff789d1a783ff0d9abc9cb87c654bfe

Contents?: true

Size: 1.35 KB

Versions: 2

Compression:

Stored size: 1.35 KB

Contents

require "syobocalite/version"
require "syobocalite/program"
require "active_support/all"
require "multi_xml"
require "open-uri"

module Syobocalite
  # Search programs between `start_at` and `end_at`
  #
  # @param start_at [Time]
  # @param end_at [Time]
  #
  # @return [Array<Program>]
  #
  # @see https://sites.google.com/site/syobocal/spec/cal_chk-php
  def self.search(start_at:, end_at:)
    xml = fetch(start_at: start_at, end_at: end_at)
    response = MultiXml.parse(xml)
    prog_items = response["syobocal"]["ProgItems"]["ProgItem"]

    programs = prog_items.map { |prog_item| Syobocalite::Program.from_prog_item(prog_item) }

    programs.select do |program|
      (start_at...end_at).cover?(program.st_time)
    end
  end

  def self.fetch(start_at:, end_at:)
    params = {}

    if start_at.hour >= 5
      params[:days]  = (end_at.to_date - start_at.to_date).to_i + 1
      params[:start] = start_at.strftime("%Y-%m-%d")
    else
      # NOTE: If start_at is 2018/4/7, returns 2018/4/7 05:00 - 28:59(2018/4/8 04:59)
      params[:days]  = (end_at.to_date - start_at.to_date).to_i + 2
      params[:start] = (start_at - 1.day).strftime("%Y-%m-%d")
    end

    headers = {
      "User-Agent" => "Syobocalite v#{Syobocalite::VERSION}",
    }

    URI.open("http://cal.syoboi.jp/cal_chk.php?#{params.to_param}", headers).read
  end
  private_class_method :fetch
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
syobocalite-1.0.1 lib/syobocalite.rb
syobocalite-1.0.0 lib/syobocalite.rb