Class: Restspec::Endpoints::Endpoint

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

Constant Summary

Constants included from HasSchemas

HasSchemas::DEFAULT_ROLES, HasSchemas::ROLES

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from HasSchemas

#add_schema, #all_schemas, #remove_schemas, #schema_for, #schema_roles

Instance Attribute Details

#last_requestObject

Returns the value of attribute last_request.



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

def last_request
  @last_request
end

#last_responseObject

Returns the value of attribute last_response.



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

def last_response
  @last_response
end

#methodObject

Returns the value of attribute method.



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

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.



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

def namespace
  @namespace
end

#pathObject

Returns the value of attribute path.



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

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.



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

def raw_url_params=(value)
  @raw_url_params = value
end

Instance Method Details

#add_url_param_block(param, &block) ⇒ Object



52
53
54
# File 'lib/restspec/endpoints/endpoint.rb', line 52

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

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



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

def execute(body: {}, url_params: {}, query_params: {})
  url = URLBuilder.new(full_path, self.url_params.merge(url_params), query_params).full_url
  request = Request.new(method, url, full_headers, body || payload)

  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



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

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



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

def executed_url
  last_request.url
end

#full_nameObject



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

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

#full_pathObject



32
33
34
35
36
37
38
# File 'lib/restspec/endpoints/endpoint.rb', line 32

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

#headersObject



40
41
42
# File 'lib/restspec/endpoints/endpoint.rb', line 40

def headers
  @headers ||= {}
end

#payloadObject



44
45
46
# File 'lib/restspec/endpoints/endpoint.rb', line 44

def payload
  @payload ||= internal_payload
end

#url_paramsObject



48
49
50
# File 'lib/restspec/endpoints/endpoint.rb', line 48

def url_params
  @url_params ||= URLBuilder.new(full_path, raw_url_params).url_params
end