Class: RubyJisho::Results

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/ruby_jisho/results.rb

Overview

Enumberable to store metadata and wrap Result objects cleanly

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(payload) ⇒ Results

Returns a new instance of Results.



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

def initialize(payload)
  @payload = JSON.parse(payload, symbolize_names: true)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



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

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

Instance Attribute Details

#payloadObject (readonly)

Returns the value of attribute payload.



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

def payload
  @payload
end

Instance Method Details

#eachObject



22
23
24
# File 'lib/ruby_jisho/results.rb', line 22

def each
  data.each { |r| yield r }
end

#keysObject



18
19
20
# File 'lib/ruby_jisho/results.rb', line 18

def keys
  payload.keys
end

#metaObject



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

def meta
  payload.fetch(:meta, {})
end

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

Returns:

  • (Boolean)


34
35
36
# File 'lib/ruby_jisho/results.rb', line 34

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