Class: RubyJisho::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_jisho/result.rb

Overview

A single result from the API response

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ Result

Returns a new instance of Result.



6
7
8
# File 'lib/ruby_jisho/result.rb', line 6

def initialize(response)
  @response = response
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/ruby_jisho/result.rb', line 29

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

Instance Attribute Details

#responseObject (readonly)

Returns the value of attribute response.



4
5
6
# File 'lib/ruby_jisho/result.rb', line 4

def response
  @response
end

Instance Method Details

#keysObject



10
11
12
# File 'lib/ruby_jisho/result.rb', line 10

def keys
  response.keys
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/ruby_jisho/result.rb', line 37

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

#sensesObject



14
15
16
17
# File 'lib/ruby_jisho/result.rb', line 14

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

#to_hObject



19
20
21
22
23
24
25
26
27
# File 'lib/ruby_jisho/result.rb', line 19

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