Class: Overlay::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/overlay/response.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ Response

Returns a new instance of Response.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/overlay/response.rb', line 7

def initialize(hash)
  @success = hash["success"]
  @message = hash["message"]
  @data = hash["data"] || {}

  # data 配下のキーを最上位でもアクセス可能にする
  @data.each do |k, v|
    define_singleton_method(k) { v }
  end

  # top-level keys もメソッド化
  (hash.keys - ["data"]).each do |k|
    define_singleton_method(k) { hash[k] }
  end
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



5
6
7
# File 'lib/overlay/response.rb', line 5

def data
  @data
end

#messageObject (readonly)

Returns the value of attribute message.



5
6
7
# File 'lib/overlay/response.rb', line 5

def message
  @message
end

#successObject (readonly)

Returns the value of attribute success.



5
6
7
# File 'lib/overlay/response.rb', line 5

def success
  @success
end

Instance Method Details

#inspectObject

inspect と to_s を JSON 表示にオーバーライド



34
35
36
# File 'lib/overlay/response.rb', line 34

def inspect
  to_json_pretty
end

#to_hObject

JSON 表示用



24
25
26
# File 'lib/overlay/response.rb', line 24

def to_h
  { success: success, message: message }.merge(data)
end

#to_json_prettyObject

JSON pretty print 用



29
30
31
# File 'lib/overlay/response.rb', line 29

def to_json_pretty
  JSON.pretty_generate(to_h)
end

#to_sObject



38
39
40
# File 'lib/overlay/response.rb', line 38

def to_s
  to_json_pretty
end