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`.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/devstructure/api.rb', line 26

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
    "#{
      begin
        File.read("/etc/token").chomp
      rescue Errno::ENOENT
        ""
      end
    }#{
      File.read(File.expand_path("~/.token")).chomp
    }"
  end
end

Instance Method Details

#blueprints(username = nil) ⇒ Object

Grab a particular user’s blueprint collection.



156
157
158
# File 'lib/devstructure/api.rb', line 156

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`.



57
58
59
60
61
62
# File 'lib/devstructure/api.rb', line 57

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.



49
50
51
52
# File 'lib/devstructure/api.rb', line 49

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