Class: Restspec::Endpoints::Endpoint

Inherits:
Struct
  • Object
show all
Defined in:
lib/restspec/endpoints/endpoint.rb

Constant Summary collapse

PARAM_INTERPOLATION_REGEX =
/:([\w]+)/

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#last_requestObject

Returns the value of attribute last_request.



8
9
10
# File 'lib/restspec/endpoints/endpoint.rb', line 8

def last_request
  @last_request
end

#last_responseObject

Returns the value of attribute last_response.



8
9
10
# File 'lib/restspec/endpoints/endpoint.rb', line 8

def last_response
  @last_response
end

#methodObject

Returns the value of attribute method.



6
7
8
# File 'lib/restspec/endpoints/endpoint.rb', line 6

def method
  @method
end

#nameObject

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



5
6
7
# File 'lib/restspec/endpoints/endpoint.rb', line 5

def name
  @name
end

#namespaceObject

Returns the value of attribute namespace.



6
7
8
# File 'lib/restspec/endpoints/endpoint.rb', line 6

def namespace
  @namespace
end

#pathObject

Returns the value of attribute path.



6
7
8
# File 'lib/restspec/endpoints/endpoint.rb', line 6

def path
  @path
end

#raw_url_params=(value) ⇒ Object

Sets the attribute raw_url_params

Parameters:

  • value

    the value to set the attribute raw_url_params to.



6
7
8
# File 'lib/restspec/endpoints/endpoint.rb', line 6

def raw_url_params=(value)
  @raw_url_params = value
end

#schema_extensionsObject

Returns the value of attribute schema_extensions.



6
7
8
# File 'lib/restspec/endpoints/endpoint.rb', line 6

def schema_extensions
  @schema_extensions
end

#schema_nameObject



33
34
35
# File 'lib/restspec/endpoints/endpoint.rb', line 33

def schema_name
  @schema_name || namespace.try(:schema_name)
end

Instance Method Details

#add_url_param_block(param, &block) ⇒ Object



64
65
66
# File 'lib/restspec/endpoints/endpoint.rb', line 64

def add_url_param_block(param, &block)
  raw_url_params[param] = Proc.new(&block)
end

#execute(body: {}, url_params: {}, query_params: {}) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/restspec/endpoints/endpoint.rb', line 12

def execute(body: {}, url_params: {}, query_params: {})
  full_url = build_full_url(url_params, query_params)
  request = Request.new(method, full_url, full_headers, body)

  Network.request(request).tap do |response|
    self.last_request = inject_self_into(response, :endpoint)
    self.last_request = inject_self_into(request, :endpoint)
  end
end

#execute_once(body: {}, url_params: {}, query_params: {}, before: ->{ }) ⇒ Object



22
23
24
25
26
27
# File 'lib/restspec/endpoints/endpoint.rb', line 22

def execute_once(body: {}, url_params: {}, query_params: {}, before: ->{ })
  @executed_response ||= begin
    before.call
    execute(body: body, url_params: url_params, query_params: query_params)
  end
end

#executed_urlObject



68
69
70
# File 'lib/restspec/endpoints/endpoint.rb', line 68

def executed_url
  last_request.url
end

#full_nameObject



29
30
31
# File 'lib/restspec/endpoints/endpoint.rb', line 29

def full_name
  [namespace.try(:name), name].compact.join("/")
end

#full_pathObject



48
49
50
51
52
53
54
# File 'lib/restspec/endpoints/endpoint.rb', line 48

def full_path
  if namespace && in_member_or_collection?
    "#{namespace.full_base_path}#{path}"
  else
    path
  end
end

#headersObject



56
57
58
# File 'lib/restspec/endpoints/endpoint.rb', line 56

def headers
  @headers ||= {}
end

#schemaObject



37
38
39
40
41
42
43
44
45
46
# File 'lib/restspec/endpoints/endpoint.rb', line 37

def schema
  @schema ||= begin
    found_schema = schema_from_store
    if found_schema.present?
      found_schema.clone.extend_with(schema_extensions || {})
    else
      nil
    end
  end
end

#url_paramsObject



60
61
62
# File 'lib/restspec/endpoints/endpoint.rb', line 60

def url_params
  @url_params ||= Restspec::Values::SuperHash.new(calculate_url_params)
end