Class: CommunityZero::CookbooksEndpoint

Inherits:
Endpoint
  • Object
show all
Defined in:
lib/community_zero/endpoints/cookbooks_endpoint.rb

Overview

The endpoint for all cookbooks.

Constant Summary

Constants inherited from Endpoint

Endpoint::METHODS

Instance Attribute Summary

Attributes inherited from Endpoint

#server

Instance Method Summary collapse

Methods inherited from Endpoint

#call, #initialize, #store, #url_for, #version_url_for

Constructor Details

This class inherits a constructor from CommunityZero::Endpoint

Instance Method Details

#get(request) ⇒ Object

GET /cookbooks



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/community_zero/endpoints/cookbooks_endpoint.rb', line 27

def get(request)
  start = Integer(request.query_params['start'] || 0)
  items = Integer(request.query_params['items'] || 10)
  cookbooks = store.cookbooks[start...items] || []

  respond({
    'items' => cookbooks.collect { |cookbook|
      {
        'cookbook_name'        => cookbook.name,
        'cookbook_description' => cookbook.description,
        'cookbook'             => url_for(cookbook),
        'cookbook_maintainer'  => cookbook.maintainer
      }
    },
    'total' => store.size,
    'start' => start.to_i,
  })
end

#post(request) ⇒ Object

POST /cookbooks



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/community_zero/endpoints/cookbooks_endpoint.rb', line 47

def post(request)
  params = Rack::Utils::Multipart.parse_multipart(request.env)
  cookbook = JSON.parse(params['cookbook'], symbolize_names: true)
  tarball = params['tarball']

   = (tarball)

  if store.find(.name, .version)
    respond(401,
      {
        'error_code' => 'ALREADY_EXISTS',
        'error_messages' => ['Resource already exists'],
      }
    )
  else
    respond(create_cookbook(, cookbook).to_hash)
  end
end