class CommunityZero::Endpoint

The base class for any endpoint.

@author Seth Vargo <sethvargo@gmail.com>

Constants

METHODS

Attributes

server[R]

Public Class Methods

new(server) click to toggle source

Create a new endpoint.

@param [CommunityZero::Server] server

the server to respond to this endpoint
# File lib/community_zero/endpoint.rb, line 37
def initialize(server)
  @server = server
end

Public Instance Methods

call(request) click to toggle source

Call the request.

@param [CommunityZero::Request] request

the request object
# File lib/community_zero/endpoint.rb, line 76
def call(request)
  m = request.method.downcase.to_sym

  # Only respond to listed methods
  unless respond_to?(m)
    allowed = METHODS.select { |m| respond_to?(m) }.map(&:upcase).join(', ')
    return [
      405,
      { 'Content-Type' => 'text/plain', 'Allow' => allowed },
      "Method not allowed: '#{request.env['REQUEST_METHOD']}'"
    ]
  end

  begin
    send(m, request)
  rescue RestError => e
    error(e.response_code, e.error)
  end
end
store() click to toggle source

The data store for these endpoints

@return [CommunityZero::Store]

# File lib/community_zero/endpoint.rb, line 44
def store
  server.store
end
url_for(cookbook) click to toggle source

Generate the URL for the given cookbook.

@param [CommunityZero::Cookbook] cookbook

the coookbook to generate the URL for

@return [String]

the URL
# File lib/community_zero/endpoint.rb, line 55
def url_for(cookbook)
  "#{server.url}/cookbooks/#{cookbook.name}"
end
version_url_for(cookbook, version) click to toggle source

Generate the version URL for the given cookbook and version.

@param [CommunityZero::Cookbook] cookbook

the coookbook to generate the URL for

@param [String] version

the version to generate a string for

@return [String]

the URL
# File lib/community_zero/endpoint.rb, line 68
def version_url_for(cookbook, version)
  "#{server.url}/cookbooks/#{cookbook.name}/versions/#{version.gsub('.', '_')}"
end

Private Instance Methods

error(response_code, error) click to toggle source
# File lib/community_zero/endpoint.rb, line 97
def error(response_code, error)
  respond(response_code, { 'error' => error })
end
respond(response_code = 200, content) click to toggle source
# File lib/community_zero/endpoint.rb, line 101
def respond(response_code = 200, content)
  [
    response_code,
    { 'Content-Type' => 'application/json' },
    JSON.pretty_generate(content)
  ]
end