Sha256: 98e3e673e964310b526ec8afd6ba538e7abaecac6e1aec500f0a2c5e60853f25

Contents?: true

Size: 1.06 KB

Versions: 2

Compression:

Stored size: 1.06 KB

Contents

require 'nokogiri'

module Hatenablog
  class Category

    # Create a new blog categories from a XML string.
    # @param [String] xml XML string representation
    # @return [Hatenablog::Category]
    def self.load_xml(xml)
      Hatenablog::Category.new(xml)
    end

    # @return [Array]
    def categories
      @categories.dup
    end

    def each(&block)
      return enum_for unless block_given?

      @categories.each do |category|
        # @type var block: ^(String) -> void
        block.call(category)
      end
    end

    # If fixed, only categories in this categories can be used for a blog entry.
    # @return [Boolean]
    def fixed?
      @fixed == 'yes'
    end


    private

    def initialize(xml)
      @document = Nokogiri::XML(xml)
      parse_document
    end

    def parse_document
      @categories = @document.css('atom|category').inject(Array.new) do |categories, category|
        categories << category['term'].to_s
      end

      @fixed = @document.at_css('app|categories')['fixed'].to_s
      @fixed = 'no' if @fixed.nil?
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
hatenablog-0.9.0 lib/hatenablog/category.rb
hatenablog-0.8.0 lib/hatenablog/category.rb