Class: EY::CloudClient::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/engineyard-cloud-client/connection.rb

Constant Summary collapse

RUBY_VERSION_NAME =
::RbConfig::CONFIG["RUBY_VERSION_NAME"] || "ruby-#{::RUBY_VERSION}"
BASE_USER_AGENT =
"EngineYardCloudClient/#{EY::CloudClient::VERSION} (#{::RUBY_PLATFORM}; #{RUBY_VERSION_NAME})".freeze
DEFAULT_ENDPOINT =
"https://cloud.engineyard.com/".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Connection

Initialize a new EY::CloudClient::Connection with a hash including:

:token: (optional) Perform authenticated requests with this token :user_agent: (optional) A user agent name/version pair to add to the User-Agent header. (e.g. EngineYardCLI/2.0.0) :output: (optional) Send output to a stream other than $stdout :endpoint: (optional) An alternate Engine Yard Cloud endpoint URI



24
25
26
27
28
29
30
31
32
33
# File 'lib/engineyard-cloud-client/connection.rb', line 24

def initialize(options={})
  @output     = options[:output] || $stdout
  @user_agent = [options[:user_agent], BASE_USER_AGENT].compact.join(' ').strip
  @endpoint   = URI.parse(options[:endpoint] || DEFAULT_ENDPOINT)
  @token      = options[:token]

  unless @endpoint.absolute?
    raise BadEndpointError.new(@endpoint)
  end
end

Instance Attribute Details

#endpointObject (readonly)

Returns the value of attribute endpoint.



11
12
13
# File 'lib/engineyard-cloud-client/connection.rb', line 11

def endpoint
  @endpoint
end

#outputObject (readonly)

Returns the value of attribute output.



11
12
13
# File 'lib/engineyard-cloud-client/connection.rb', line 11

def output
  @output
end

#tokenObject

Returns the value of attribute token.



12
13
14
# File 'lib/engineyard-cloud-client/connection.rb', line 12

def token
  @token
end

#user_agentObject (readonly)

Returns the value of attribute user_agent.



11
12
13
# File 'lib/engineyard-cloud-client/connection.rb', line 11

def user_agent
  @user_agent
end

Instance Method Details

#==(other) ⇒ Object



48
49
50
# File 'lib/engineyard-cloud-client/connection.rb', line 48

def ==(other)
  other.is_a?(self.class) && [other.token, other.user_agent, other.endpoint] == [token, user_agent, endpoint]
end

#authenticate!(email, password) ⇒ Object



89
90
91
92
93
# File 'lib/engineyard-cloud-client/connection.rb', line 89

def authenticate!(email, password)
  response = post("/authenticate", :email => email, :password => password)
  self.token = response["api_token"]
  token
end

#authenticated?Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/engineyard-cloud-client/connection.rb', line 95

def authenticated?
  !token.nil? && !token.empty?
end

#debug(name, value) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/engineyard-cloud-client/connection.rb', line 35

def debug(name, value)
  return unless ENV['DEBUG']

  indent = 12                                           # 12 because that's what Thor used.
  unless String === value
    value = value.pretty_inspect.rstrip                 # remove trailing whitespace
    if value.index("\n")                                # if the inspect is multi-line
      value = value.gsub(/^/, " "*(indent + 2)).lstrip  # indent it
    end
  end
  @output << "#{name.to_s.rjust(indent)}  #{value.rstrip}\n"       # just one newline
end

#request(meth, path, params = nil, extra_headers = nil) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/engineyard-cloud-client/connection.rb', line 60

def request(meth, path, params=nil, extra_headers=nil)
  url    = endpoint + "api/v2#{path}"
  meth   ||= 'get'
  meth   = meth.to_s.downcase.to_sym
  params ||= {}

  headers = {
    "User-Agent" => user_agent,
    "Accept"     => "application/json",
  }

  if token
    headers["X-EY-Cloud-Token"] = token
  end

  if extra_headers
    headers.merge!(extra_headers)
  end

  debug(meth.to_s.upcase, url.to_s)
  debug("Params",  params) if params
  debug("Headers", headers)

  resp = do_request(meth, url, params, headers)
  data = parse_response(resp)

  data
end