Sha256: e2f6d9dd66bd4189e2b029b94c16b1708e04424439a6ba51027b748bb0980fa0

Contents?: true

Size: 804 Bytes

Versions: 2

Compression:

Stored size: 804 Bytes

Contents

module RubyJisho
  # A single result from the API response
  class Result
    attr_reader :response

    def initialize(response)
      @response = response
    end

    def keys
      response.keys
    end

    def senses
      response.fetch(:senses)
              .map { |s| Sense.new(s) }
    end

    def to_h
      keys.each_with_object({}) do |k, hash|
        hash[k] = begin
                    send(k)
                  rescue NoMethodError
                    response.fetch(k, nil)
                  end
      end
    end

    def method_missing(sym, *args, &block)
      if keys.include?(sym)
        response.fetch(sym)
      else
        super
      end
    end

    def respond_to_missing?(method_name, include_private = false)
      keys.include?(method_name) || super
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ruby_jisho-0.1.1 lib/ruby_jisho/result.rb
ruby_jisho-0.1.0 lib/ruby_jisho/result.rb