Class: RubyJisho::Sense

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

Overview

Wrapper for sense hashes from the Jisho API

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response_hash) ⇒ Sense

Returns a new instance of Sense.



8
9
10
# File 'lib/ruby_jisho/sense.rb', line 8

def initialize(response_hash)
  @response = response_hash
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



25
26
27
28
29
30
31
# File 'lib/ruby_jisho/sense.rb', line 25

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.



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

def response
  @response
end

Instance Method Details

#keysObject



21
22
23
# File 'lib/ruby_jisho/sense.rb', line 21

def keys
  response.keys
end

#parts_of_speechObject



12
13
14
15
# File 'lib/ruby_jisho/sense.rb', line 12

def parts_of_speech
  response.fetch(:parts_of_speech, [''])
          .map { |p| p.downcase.to_sym }
end

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

Returns:

  • (Boolean)


33
34
35
# File 'lib/ruby_jisho/sense.rb', line 33

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

#to_hObject



17
18
19
# File 'lib/ruby_jisho/sense.rb', line 17

def to_h
  response
end