Class: Daylight::Mock::Handler

Inherits:
Object
  • Object
show all
Defined in:
lib/daylight/mock.rb

Overview

Represents a single mocked request-response pair.

Defined Under Namespace

Classes: PathParts

Constant Summary collapse

PATH_PARTS_REGEX =
%r{^/([^/]+)/([^/]+)(?:/(\d+))?(?:/([^/]+))?\.json$}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request) ⇒ Handler

Returns a new instance of Handler.



38
39
40
# File 'lib/daylight/mock.rb', line 38

def initialize(request)
  @request = request
end

Instance Attribute Details

#requestObject (readonly)

Returns the value of attribute request.



34
35
36
# File 'lib/daylight/mock.rb', line 34

def request
  @request
end

#responseObject (readonly)

Returns the value of attribute response.



34
35
36
# File 'lib/daylight/mock.rb', line 34

def response
  @response
end

#statusObject (readonly)

Returns the value of attribute status.



34
35
36
# File 'lib/daylight/mock.rb', line 34

def status
  @status
end

#target_objectObject (readonly)

Returns the value of attribute target_object.



34
35
36
# File 'lib/daylight/mock.rb', line 34

def target_object
  @target_object
end

Instance Method Details

#actionObject

The action to perform (based on the request path and method).

Returns :associated, :shown, :indexed, :created, :updated or :deleted



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/daylight/mock.rb', line 78

def action
  @action ||=
    case request.method
    when :get
      if path_parts.associated.present?
        :associated
      elsif path_parts.id.present?
        :shown
      else
        :indexed
      end
    when :post
      :created
    when :put
      :updated
    when :patch
      :patched
    when :delete
      :deleted
    end
end

#paramsObject

The request’s query params



70
71
72
# File 'lib/daylight/mock.rb', line 70

def params
  @params ||= Rack::Utils.parse_nested_query(request.uri.query)
end

#pathObject

The request’s path



52
53
54
# File 'lib/daylight/mock.rb', line 52

def path
  request.uri.path
end

#path_partsObject

The request path split into logical parts: version, resource, id, and assocatied.

Returns a PathParts Struct



46
47
48
# File 'lib/daylight/mock.rb', line 46

def path_parts
  @path_parts ||= PathParts.new(*path.match(PATH_PARTS_REGEX).captures)
end

#post_dataObject

The request’s POST data



64
65
66
# File 'lib/daylight/mock.rb', line 64

def post_data
  @post_data ||= JSON.parse(request.body)
end

#response_bodyObject

The mock respose body



58
59
60
# File 'lib/daylight/mock.rb', line 58

def response_body
  @response_body ||= handle_request
end