Sha256: 27c76b015bf1ce62842cf9a5716baed8c2e05aff19231d158b733720d9a262a1

Contents?: true

Size: 1.31 KB

Versions: 13

Compression:

Stored size: 1.31 KB

Contents

require 'mechanize'
require "logger"

module NcodeSyosetu
  class Client
    class NotFound < StandardError
      attr_reader :url

      def initialize(url, error_message = nil)
        super(error_message)
        @url = url
      end
    end

    def initialize(logger: Logger.new(STDOUT), sleep: 0.5)
      @mechanize = Mechanize.new
      @logger = logger
      @sleep = sleep
    end

    def get(ncode)
      toc = get_toc(ncode)

      episodes = toc.episodes.map do |episode|
        if episode[:number]
          get_episode(ncode, episode[:text], episode[:number])
        else
          NcodeSyosetu::Model::Heading.new(episode[:text])
        end
      end

      NcodeSyosetu::Model::Novel.new(toc, episodes)
    end

    def get_toc(ncode)
      url = toc_url(ncode)
      NcodeSyosetu::Model::Toc.new(get_content(toc_url(ncode)))
    end

    def get_episode(ncode, title, number)
      sleep(@sleep)
      NcodeSyosetu::Model::Episode.new(title, number, get_content(episode_url(ncode, number)))
    end

    def toc_url(ncode)
      "http://#{NcodeSyosetu::NCODE_HOST_NAME}/#{ncode}"
    end

    def episode_url(ncode, number)
      "http://#{NcodeSyosetu::NCODE_HOST_NAME}/#{ncode}/#{number}"
    end

    private

    def get_content(url)
      @logger.info("GET #{url}...")
      @mechanize.get(url)
    end
  end
end

Version data entries

13 entries across 13 versions & 2 rubygems

Version Path
ncode-syosetu-core-0.2.3 lib/ncode_syosetu/client.rb
ncode-syosetu-core-0.2.2 lib/ncode_syosetu/client.rb
ncode-syosetu-core-0.2.1 lib/ncode_syosetu/client.rb
ncode-syosetu-core-0.2.0 lib/ncode_syosetu/client.rb
ncode-syosetu-core-0.1.2 lib/ncode_syosetu/client.rb
ncode-syosetu-core-0.1.1 lib/ncode_syosetu/client.rb
ncode-syosetu-core-0.1.0 lib/ncode_syosetu/client.rb
ncode-syosetu-0.0.7 lib/ncode_syosetu/client.rb
ncode-syosetu-0.0.6 lib/ncode_syosetu/client.rb
ncode-syosetu-0.0.5 lib/ncode_syosetu/client.rb
ncode-syosetu-0.0.4 lib/ncode_syosetu/client.rb
ncode-syosetu-0.0.3 lib/ncode_syosetu/client.rb
ncode-syosetu-0.0.2 lib/ncode_syosetu/client.rb