Class: ApiDoc::Resource

Inherits:
Object
  • Object
show all
Includes:
Rack::Test::Methods
Defined in:
lib/apidoc.rb

Defined Under Namespace

Classes: Blueprint

Constant Summary collapse

PATH_CAPTURE_REGEXP =
/:[^\.\/]+/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(runner, path, &block) ⇒ Resource

Returns a new instance of Resource.



127
128
129
130
131
132
133
134
# File 'lib/apidoc.rb', line 127

def initialize(runner, path, &block)
  @runner = runner
  @path = path
  @params = nil
  @before_blocks = []
  @after_blocks = []
  Blueprint.new self, &block
end

Instance Attribute Details

#acceptObject



176
177
178
# File 'lib/apidoc.rb', line 176

def accept
  @accept || @runner.accept
end

#after_blocksObject

Returns the value of attribute after_blocks.



136
137
138
# File 'lib/apidoc.rb', line 136

def after_blocks
  @after_blocks
end

#before_blocksObject

Returns the value of attribute before_blocks.



136
137
138
# File 'lib/apidoc.rb', line 136

def before_blocks
  @before_blocks
end

#content_typeObject



172
173
174
# File 'lib/apidoc.rb', line 172

def content_type
  @content_type || @runner.content_type
end

#descObject

Returns the value of attribute desc.



136
137
138
# File 'lib/apidoc.rb', line 136

def desc
  @desc
end

#methodObject

Returns the value of attribute method.



136
137
138
# File 'lib/apidoc.rb', line 136

def method
  @method
end

#paramsObject

Returns the value of attribute params.



136
137
138
# File 'lib/apidoc.rb', line 136

def params
  @params
end

#pathObject

Returns the value of attribute path.



136
137
138
# File 'lib/apidoc.rb', line 136

def path
  @path
end

Instance Method Details

#appObject



180
181
182
# File 'lib/apidoc.rb', line 180

def app
  ApiDoc.app
end

#filtered_paramsObject



191
192
193
# File 'lib/apidoc.rb', line 191

def filtered_params
  params.reject {|k, v| path_captures.include?(k) or path_captures.include?(k.to_s)}
end

#formatted_paramsObject



195
196
197
198
199
200
201
202
203
204
205
# File 'lib/apidoc.rb', line 195

def formatted_params
  return '' unless params
  return filtered_params unless accept

  case accept.intern
  when :json
    JSON.pretty_generate(filtered_params)
  else
    filtered_params
  end
end

#formatted_responseObject



207
208
209
210
211
212
213
214
215
216
# File 'lib/apidoc.rb', line 207

def formatted_response
  return response_body unless content_type

  case content_type.intern
  when :json
    JSON.pretty_generate(JSON.parse(response_body))
  else
    response_body
  end
end

#interpolated_pathObject



228
229
230
231
232
# File 'lib/apidoc.rb', line 228

def interpolated_path
  @path.gsub(PATH_CAPTURE_REGEXP) do |match|
    params[match.gsub(':', '').intern]
  end
end

#make_requestObject



234
235
236
# File 'lib/apidoc.rb', line 234

def make_request
  self.send(@method.downcase.intern, interpolated_path, formatted_params)
end

#path_capturesObject



224
225
226
# File 'lib/apidoc.rb', line 224

def path_captures
  @path.match(PATH_CAPTURE_REGEXP).to_a.map {|m| m.gsub(':', '').intern }
end

#response_bodyObject



218
219
220
# File 'lib/apidoc.rb', line 218

def response_body
  @response.body
end

#runObject



184
185
186
187
188
189
# File 'lib/apidoc.rb', line 184

def run
  @before_blocks.each {|block| instance_eval &block }
  @params = @params.call if @params.is_a?(Proc)
  @response = make_request
  @after_blocks.each {|block| instance_eval &block }
end