Class: DevStructure::API

Inherits:
Net::HTTP
  • Object
show all
Defined in:
lib/devstructure/api.rb

Overview

We subclass Net::HTTP so as not to take away a user’s ability to shoot themselves in the foot.

Defined Under Namespace

Classes: Blueprints

Instance Method Summary collapse

Constructor Details

#initialize(token = nil) ⇒ API

No one really has access to a DevStructure API token these days, save for the one that’s installed on each DevStructure server. The default token is the one created by concatenating /etc/token and ‘~/.token`.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/devstructure/api.rb', line 32

def initialize(token=nil)
  @newimpl = true
  uri = URI.parse("https://api.devstructure.com")
  super uri.host, uri.port
  if self.use_ssl = "https" == uri.scheme
    self.verify_mode = OpenSSL::SSL::VERIFY_NONE
  end
  @token = if token
    token
  else
    "#{
      File.read("/etc/token").chomp
    }#{
      File.read(File.expand_path("~/.token")).chomp
    }"
  end
end

Instance Method Details

#blueprints(username = nil) ⇒ Object

Grab a particular user’s blueprint collection.



158
159
160
# File 'lib/devstructure/api.rb', line 158

def blueprints(username=nil)
  Blueprints.new(self, username)
end

#headersObject

These are the only two headers that are required when accessing the API. Others such as Content-Type and Content-Length will be added as necessary, either explicitly or by Net::HTTP.



59
60
61
62
63
64
# File 'lib/devstructure/api.rb', line 59

def headers
  {
    "Authorization" => "Token token=\"#{@token}\"",
    "Host" => "api.devstructure.com",
  }
end

#path(*args) ⇒ Object

This is the lazy man’s way of building the URI for a request.



51
52
53
54
# File 'lib/devstructure/api.rb', line 51

def path(*args)
  args.reject! { |arg| arg.nil? }
  "/#{args.join("/")}"
end