Class: CultomePlayer::Objects::Response

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, data) ⇒ Response

Returns a new instance of Response.



6
7
8
9
10
11
12
13
14
15
# File 'lib/cultome_player/objects/response.rb', line 6

def initialize(type, data)
  @success = type == :success
  @data = data

  @data.each do |k,v|
    self.singleton_class.send(:define_method, k) do
      v
    end
  end
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



4
5
6
# File 'lib/cultome_player/objects/response.rb', line 4

def data
  @data
end

Instance Method Details

#+(response) ⇒ Response

Join two response together. The response type makes an OR and parameter response’s data is merged into.

Parameters:

  • response (Response)

    The response to join.

Returns:

  • (Response)

    The calculated new response.



35
36
37
38
39
# File 'lib/cultome_player/objects/response.rb', line 35

def +(response)
  type = success? && response.success? ? :success : :failure
  data = @data.merge response.data
  return Response.new(type, data)
end

#failure?Boolean

Check if the success data associated to the response is false.

Returns:

  • (Boolean)

    True if success data is false, False otherwise.



20
21
22
# File 'lib/cultome_player/objects/response.rb', line 20

def failure?
  !@success
end

#success?Boolean

Check if the success data associated to the response is true.

Returns:

  • (Boolean)

    True if success data is true, False otherwise.



27
28
29
# File 'lib/cultome_player/objects/response.rb', line 27

def success?
  @success
end

#to_sObject



41
42
43
# File 'lib/cultome_player/objects/response.rb', line 41

def to_s
  "Response #{success? ? 'successful' : 'failed'} => #{@data}"
end