Class: CommunityZero::Endpoint

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

Overview

The base class for any endpoint.

Author:

Constant Summary collapse

METHODS =
[:get, :put, :post, :delete].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(server) ⇒ Endpoint

Create a new endpoint.

Parameters:



37
38
39
# File 'lib/community_zero/endpoint.rb', line 37

def initialize(server)
  @server = server
end

Instance Attribute Details

#serverObject (readonly)

Returns the value of attribute server.



31
32
33
# File 'lib/community_zero/endpoint.rb', line 31

def server
  @server
end

Instance Method Details

#call(request) ⇒ Object

Call the request.

Parameters:



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# 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

#storeCommunityZero::Store

The data store for these endpoints



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

def store
  server.store
end

#url_for(cookbook) ⇒ String

Generate the URL for the given cookbook.

Parameters:

Returns:

  • (String)

    the URL



55
56
57
# File 'lib/community_zero/endpoint.rb', line 55

def url_for(cookbook)
  "#{server.url}/cookbooks/#{cookbook.name}"
end

#version_url_for(cookbook, version) ⇒ String

Generate the version URL for the given cookbook and version.

Parameters:

  • cookbook (CommunityZero::Cookbook)

    the coookbook to generate the URL for

  • version (String)

    the version to generate a string for

Returns:

  • (String)

    the URL



68
69
70
# File 'lib/community_zero/endpoint.rb', line 68

def version_url_for(cookbook, version)
  "#{server.url}/cookbooks/#{cookbook.name}/versions/#{version.gsub('.', '_')}"
end