Class: CommunityZero::Endpoint
- Inherits:
-
Object
- Object
- CommunityZero::Endpoint
- Defined in:
- lib/community_zero/endpoint.rb
Overview
The base class for any endpoint.
Direct Known Subclasses
CookbookEndpoint, CookbookVersionsVersionEndpoint, CookbooksEndpoint, NotFoundEndpoint, SearchEndpoint
Constant Summary collapse
- METHODS =
[:get, :put, :post, :delete].freeze
Instance Attribute Summary collapse
-
#server ⇒ Object
readonly
Returns the value of attribute server.
Instance Method Summary collapse
-
#call(request) ⇒ Object
Call the request.
-
#initialize(server) ⇒ Endpoint
constructor
Create a new endpoint.
-
#url_for(cookbook) ⇒ String
Generate the URL for the given cookbook.
-
#version_url_for(cookbook, version) ⇒ String
Generate the version URL for the given cookbook and version.
Constructor Details
#initialize(server) ⇒ Endpoint
Create a new endpoint.
37 38 39 |
# File 'lib/community_zero/endpoint.rb', line 37 def initialize(server) @server = server end |
Instance Attribute Details
#server ⇒ Object (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.
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/community_zero/endpoint.rb', line 69 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 # Must accept JSON unless request.env['HTTP_ACCEPT'].to_s.split(';').include?('application/json') return [ 406, { 'Content-Type' => 'text/plain' }, 'Must accept application/json' ] end begin send(m, request) rescue RestError => e error(e.response_code, e.error) end end |
#url_for(cookbook) ⇒ String
Generate the URL for the given cookbook.
48 49 50 |
# File 'lib/community_zero/endpoint.rb', line 48 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.
61 62 63 |
# File 'lib/community_zero/endpoint.rb', line 61 def version_url_for(cookbook, version) "#{server.url}/cookbooks/#{cookbook.name}/versions/#{version.gsub('.', '_')}" end |