Class: DevStructure::API::Blueprints

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(api, username = nil) ⇒ Blueprints

This ugly constructor should be eschewed in favor of ‘API#blueprints`.



70
71
72
73
# File 'lib/devstructure/api.rb', line 70

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.



80
81
82
83
84
85
86
87
# File 'lib/devstructure/api.rb', line 80

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.



148
149
150
151
# File 'lib/devstructure/api.rb', line 148

def delete(name, options={})
  @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.



104
105
106
107
108
109
110
111
# File 'lib/devstructure/api.rb', line 104

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

#listObject

The API version of [‘blueprint-list`(1)](devstructure.github.com/contractor/blueprint-list.1.html). It returns an array of `DevStructure::Blueprint` objects.



92
93
94
95
96
97
98
99
# File 'lib/devstructure/api.rb', line 92

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.



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/devstructure/api.rb', line 119

def post(name, options={})
  @api.start unless @api.started?
  params = {}
  options.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