class ApiSketch::Model::Resource

Attributes

action[RW]
format[RW]
headers[RW]
http_method[RW]
namespace[RW]
parameters[RW]
path[RW]
responses[RW]
sample_call[RW]
sample_response[RW]

Public Class Methods

add(resource) click to toggle source
# File lib/api_sketch/model/resource.rb, line 19
def add(resource)
  @resources ||= []
  @resources << resource
end
all() click to toggle source
# File lib/api_sketch/model/resource.rb, line 34
def all
  @resources ||= []
end
count() click to toggle source
# File lib/api_sketch/model/resource.rb, line 54
def count
  self.all.count
end
create(attributes) click to toggle source
# File lib/api_sketch/model/resource.rb, line 12
def create(attributes)
  res = self.new(attributes)
  res.send(:run_validations!)
  self.add(res)
  res
end
find(id) click to toggle source
# File lib/api_sketch/model/resource.rb, line 38
def find(id)
  self.all.find { |res| res.id == id }
end
find_by_http_method_and_path(http_method, path) click to toggle source
# File lib/api_sketch/model/resource.rb, line 42
def find_by_http_method_and_path(http_method, path)
  self.all.find { |res| res.http_method == http_method && res.path == path }
end
first() click to toggle source
# File lib/api_sketch/model/resource.rb, line 46
def first
  self.all.first
end
last() click to toggle source
# File lib/api_sketch/model/resource.rb, line 50
def last
  self.all.last
end
reload!(definitions_dir) click to toggle source
# File lib/api_sketch/model/resource.rb, line 28
def reload!(definitions_dir)
  ApiSketch::Model::SharedBlock.reset!
  self.reset!
  ApiSketch::DSL.new(definitions_dir).init!
end
reset!() click to toggle source
# File lib/api_sketch/model/resource.rb, line 24
def reset!
  @resources = []
end

Public Instance Methods

id() click to toggle source

TODO: update this method to provide better id that is used as part of filename

# File lib/api_sketch/model/resource.rb, line 6
def id
  [self.namespace, self.action].reject { |v| v.nil? || v == "" }.join("/")
end

Private Instance Methods

default_values_hash() click to toggle source
# File lib/api_sketch/model/resource.rb, line 61
def default_values_hash
  {
    http_method: "GET",
    format: "json",
    headers: [],
    parameters: ::ApiSketch::Model::Parameters.new,
    responses: []
  }
end
error_message(message) click to toggle source
# File lib/api_sketch/model/resource.rb, line 71
def error_message(message)
  # puts_error(message)
  raise ::ApiSketch::Error, message
end
run_validations!() click to toggle source
# File lib/api_sketch/model/resource.rb, line 76
def run_validations!
  unless self.action =~ /\A\w*\z/
    error_message("'#{self.action}' is invalid action value")
  end

  if self.class.find(self.id)
    error_message("'#{self.id}' is not unique id. Change values of 'namespace' and/or 'action' attributes")
  end

  if self.http_method.nil? || self.http_method.empty?
    error_message("request http_method can't be blank")
  end

  if self.path.nil? || self.path.empty?
    error_message("request path can't be blank")
  end

  if self.class.find_by_http_method_and_path(self.http_method, self.path)
    error_message("Route '#{self.http_method} #{self.path}' should be unique")
  end
end