Class: Cyrax::Response

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource_name, result, options = {}) ⇒ Response

Returns a new instance of Response.



5
6
7
8
9
10
11
12
13
14
# File 'lib/cyrax/response.rb', line 5

def initialize(resource_name, result, options = {})
  @resource_name = resource_name
  @result = result
  @options = options
  @message = nil
  @errors = {}
  @assignments = {}
  @status = nil
  @accessor = options[:as]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



64
65
66
67
# File 'lib/cyrax/response.rb', line 64

def method_missing(method, *args, &block)
  super unless assignments.has_key?(method)
  assignments[method]
end

Instance Attribute Details

#accessorObject

Returns the value of attribute accessor.



2
3
4
# File 'lib/cyrax/response.rb', line 2

def accessor
  @accessor
end

#assignmentsObject

Returns the value of attribute assignments.



2
3
4
# File 'lib/cyrax/response.rb', line 2

def assignments
  @assignments
end

#errorsObject

Returns the value of attribute errors.



2
3
4
# File 'lib/cyrax/response.rb', line 2

def errors
  @errors
end

#messageObject

Returns the value of attribute message.



2
3
4
# File 'lib/cyrax/response.rb', line 2

def message
  @message
end

#optionsObject

Returns the value of attribute options.



2
3
4
# File 'lib/cyrax/response.rb', line 2

def options
  @options
end

#resource_nameObject

Returns the value of attribute resource_name.



2
3
4
# File 'lib/cyrax/response.rb', line 2

def resource_name
  @resource_name
end

#resultObject

Returns the value of attribute result.



2
3
4
# File 'lib/cyrax/response.rb', line 2

def result
  @result
end

#statusObject

Returns the value of attribute status.



2
3
4
# File 'lib/cyrax/response.rb', line 2

def status
  @status
end

Instance Method Details

#as_json(*args) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/cyrax/response.rb', line 54

def as_json(*args)
  if failure?
    {errors: @errors}
  elsif options[:serializer]
    options[:serializer].new(result, options).serialize
  else
    result.as_json
  end
end

#errorObject



44
45
46
47
48
# File 'lib/cyrax/response.rb', line 44

def error
  if failure?
    message || I18n.t("cyrax.errors.default", default: "There was appeared some errors.")
  end
end

#error_messagesObject



38
39
40
41
42
# File 'lib/cyrax/response.rb', line 38

def error_messages
  errors.map do |key, value|
    "#{key}: #{value}"
  end
end

#failure?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/cyrax/response.rb', line 30

def failure?
  !success?
end

#has_error?(error) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/cyrax/response.rb', line 50

def has_error?(error)
  errors && errors.has_key?(error)
end

#noticeObject



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

def notice
  message if success?
end

#success?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/cyrax/response.rb', line 26

def success?
  @errors.nil? || @errors.empty?
end

#with_errors(errors) ⇒ Object



16
17
18
19
# File 'lib/cyrax/response.rb', line 16

def with_errors(errors)
  @errors = errors
  self
end

#with_message(message) ⇒ Object



21
22
23
24
# File 'lib/cyrax/response.rb', line 21

def with_message(message)
  @message = message
  self
end