Class: GrapeDocs::Endpoint

Inherits:
Object
  • Object
show all
Defined in:
lib/grape_docs/endpoint.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api, endpoint) ⇒ Endpoint

Returns a new instance of Endpoint.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/grape_docs/endpoint.rb', line 5

def initialize(api, endpoint)
  options = endpoint.options[:route_options]
  @title = options[:description] || endpoint.options[:path].first
  @detail = options[:detail]
  parent_path = File.join(endpoint.inheritable_setting.namespace_stackable[:mount_path])
  @path = File.join(parent_path, endpoint.options[:path].first.to_s)
  @host = GrapeDocs.config[:api_host]
  @url = File.join(@host, @path)
  @params = options[:params].map { |p| p[1].merge(name: p[0]) }
  @method = endpoint.options[:method].first
  @status_code = 200
  if options[:entity]
    entity = options[:entity].kind_of?(Array) ? options[:entity][1] : options[:entity]
    @status_code = options[:entity].kind_of?(Array) ? options[:entity][0] : 200
    unless entity.nil?
      @model = entity.documentation.map { |p| p[1].merge(name: p[0]) }
      @response = build_response(@model)
    end
  end
end

Instance Attribute Details

#detailObject (readonly)

Returns the value of attribute detail.



3
4
5
# File 'lib/grape_docs/endpoint.rb', line 3

def detail
  @detail
end

#hostObject (readonly)

Returns the value of attribute host.



3
4
5
# File 'lib/grape_docs/endpoint.rb', line 3

def host
  @host
end

#methodObject (readonly)

Returns the value of attribute method.



3
4
5
# File 'lib/grape_docs/endpoint.rb', line 3

def method
  @method
end

#modelObject (readonly)

Returns the value of attribute model.



3
4
5
# File 'lib/grape_docs/endpoint.rb', line 3

def model
  @model
end

#paramsObject (readonly)

Returns the value of attribute params.



3
4
5
# File 'lib/grape_docs/endpoint.rb', line 3

def params
  @params
end

#pathObject (readonly)

Returns the value of attribute path.



3
4
5
# File 'lib/grape_docs/endpoint.rb', line 3

def path
  @path
end

#responseObject (readonly)

Returns the value of attribute response.



3
4
5
# File 'lib/grape_docs/endpoint.rb', line 3

def response
  @response
end

#status_codeObject (readonly)

Returns the value of attribute status_code.



3
4
5
# File 'lib/grape_docs/endpoint.rb', line 3

def status_code
  @status_code
end

#titleObject (readonly)

Returns the value of attribute title.



3
4
5
# File 'lib/grape_docs/endpoint.rb', line 3

def title
  @title
end

#urlObject (readonly)

Returns the value of attribute url.



3
4
5
# File 'lib/grape_docs/endpoint.rb', line 3

def url
  @url
end

Instance Method Details

#body_paramsObject



33
34
35
36
37
38
39
# File 'lib/grape_docs/endpoint.rb', line 33

def body_params
  return [] if method == "GET"
  names = path.scan(/:([a-z0-9_]+)/).map(&:first)
  params.reject do |param|
    names.include?(param[:name])
  end
end

#path_paramsObject



26
27
28
29
30
31
# File 'lib/grape_docs/endpoint.rb', line 26

def path_params
  names = path.scan(/:([a-z0-9_]+)/).map(&:first)
  params.select do |param|
    names.include?(param[:name])
  end
end

#query_paramsObject



41
42
43
44
45
46
47
# File 'lib/grape_docs/endpoint.rb', line 41

def query_params
  return [] if method != "GET"
  names = path.scan(/:([a-z0-9_]+)/).map(&:first)
  params.reject do |param|
    names.include?(param[:name])
  end
end