Sha256: 86da33bbd1e60369fc9910b3e4133edcd8152122ef112881e4cac078a2637bb8

Contents?: true

Size: 1.35 KB

Versions: 1

Compression:

Stored size: 1.35 KB

Contents

require 'open-uri'
require 'nokogiri'
require 'addressable/uri'
require 'data_validator'

class AnimeNewsNetwork::Encyclopedia
  def initialize(args = {})
    @url = args.has_key?(:url) ? args[:url] : 'http://cdn.animenewsnetwork.com/encyclopedia'
  end

  def get_reports(args = {})
    validator = DataValidator::Validator.new(
      args, {
        id:    { presence: true, numericality: { only_integer: true } },
        type:  { presence: true, inclusion: { in: %w(anime manga) } },
        nskip: { allow_nil: true, numericality: { only_integer: true } },
        nlist: { allow_nil: true, format: { with: /^(\d+|all)$/ } },
      }
    )
    raise ArgumentError, validator.errors unless validator.valid?

    path = '/reports.xml';
    query = args
    Nokogiri::XML(get(path, query))
  end

  def get_details(args = {})
    validator = DataValidator::Validator.new(
      args, {
        id:   { presence: true, numericality: { only_integer: true } },
        type: { presence: true, inclusion: { in: %w(anime manga) } },
      }
    )
    raise ArgumentError, validator.errors unless validator.valid?

    path = '/api.xml';
    query = { args[:type] => args[:id] }
    Nokogiri::XML(get(path, query))
  end

  private

  def get(path = '', query = {})
    uri = Addressable::URI.parse(@url)
    uri.path += path
    uri.query_values = query
    return open(uri)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
animenewsnetwork-0.0.2 lib/animenewsnetwork/encyclopedia.rb