Class: DevStructure::API::Blueprints
- Inherits:
-
Object
- Object
- DevStructure::API::Blueprints
- Includes:
- DevStructure
- Defined in:
- lib/devstructure/api.rb
Overview
‘Blueprints` objects represent a particular user’s blueprint collection but is supremely lazy about fetching it.
Instance Method Summary collapse
-
#delete(name, options = {}) ⇒ Object
Delete a blueprint and all its versions.
-
#get(name, token = nil) ⇒ Object
The API version of [‘blueprint-show`(1)](devstructure.github.com/contractor/blueprint-show.1.html).
-
#initialize(api, username = nil) ⇒ Blueprints
constructor
This ugly constructor should be eschewed in favor of ‘API#blueprints`.
-
#list ⇒ Object
The API version of [‘blueprint-list`(1)](devstructure.github.com/contractor/blueprint-list.1.html).
-
#method_missing(symbol, *args) ⇒ Object
The ‘method_missing` handler allows natural-looking code like `blueprints.foo.bar` that look like the URIs they’re referencing behind the scenes.
-
#post(name, options = {}) ⇒ Object
The API version of [‘blueprint-create`(1)](devstructure.github.com/contractor/blueprint-create.1.html).
Constructor Details
#initialize(api, username = nil) ⇒ Blueprints
This ugly constructor should be eschewed in favor of ‘API#blueprints`.
76 77 78 79 |
# File 'lib/devstructure/api.rb', line 76 def initialize(api, username=nil) @api = api @username = username end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(symbol, *args) ⇒ Object
The ‘method_missing` handler allows natural-looking code like `blueprints.foo.bar` that look like the URIs they’re referencing behind the scenes. These are limited to reads when taken all the way to referencing a specific blueprint. Just referencing a user this way still allows writes.
86 87 88 89 90 91 92 93 |
# File 'lib/devstructure/api.rb', line 86 def method_missing(symbol, *args) if @username get symbol.to_s else @username = symbol.to_s self end end |
Instance Method Details
#delete(name, options = {}) ⇒ Object
Delete a blueprint and all its versions.
154 155 156 157 |
# File 'lib/devstructure/api.rb', line 154 def delete(name, ={}) @api.start unless @api.started? @api.delete(@api.path("blueprints", @username, name), @api.headers) end |
#get(name, token = nil) ⇒ Object
The API version of [‘blueprint-show`(1)](devstructure.github.com/contractor/blueprint-show.1.html). It returns a `DevStructure::Blueprint` object.
110 111 112 113 114 115 116 117 |
# File 'lib/devstructure/api.rb', line 110 def get(name, token=nil) @api.start unless @api.started? response = @api.get(@api.path("blueprints", @username, name, token), @api.headers) return response unless Net::HTTPOK == response.class hash = JSON.parse(response.body) Blueprint.new(hash["name"], hash) end |
#list ⇒ Object
The API version of [‘blueprint-list`(1)](devstructure.github.com/contractor/blueprint-list.1.html). It returns an array of `DevStructure::Blueprint` objects.
98 99 100 101 102 103 104 105 |
# File 'lib/devstructure/api.rb', line 98 def list @api.start unless @api.started? response = @api.get(@api.path("blueprints", @username), @api.headers) return response unless Net::HTTPOK == response.class JSON.parse(response.body).collect do |hash| Blueprint.new(hash["name"], hash) end end |
#post(name, options = {}) ⇒ Object
The API version of [‘blueprint-create`(1)](devstructure.github.com/contractor/blueprint-create.1.html). It encodes and POSTs a blueprint and uploads to S3 any source tarballs referenced. The S3 upload credentials come back as part of the API response. Each set of headers is only valid for that particular upload and even then only for a few minutes.
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/devstructure/api.rb', line 125 def post(name, ={}) @api.start unless @api.started? params = {} .each do |key, value| params[key] = if value.kind_of?(Hash) JSON.generate(value) else value end end request = Net::HTTP::Post.new(@api.path("blueprints", @username, name)) request.set_form_data params @api.headers.each { |key, value| request[key] = value } response = @api.request(request) if Net::HTTPOK === response uri = URI.parse("http://s3.amazonaws.com") http = Net::HTTP.start(uri.host, uri.port) JSON.parse(response.body).each do |filename, headers| request = Net::HTTP::Put.new("/blueprint-sources/#{filename}") headers.each { |key, value| request[key] = value } request["Content-Length"] = File.size(filename) request.body_stream = File.open(filename, "r") http.request(request) end end response end |