Class: EY::CloudClient::Connection

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

Constant Summary collapse

BASE_USER_AGENT =
"EngineYardCloudClient/#{EY::CloudClient::VERSION}".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



19
20
21
22
23
24
25
26
27
28
# File 'lib/engineyard-cloud-client/connection.rb', line 19

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)
  self.token  = options[:token]

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

Instance Attribute Details

#endpointObject (readonly)

Returns the value of attribute endpoint.



8
9
10
# File 'lib/engineyard-cloud-client/connection.rb', line 8

def endpoint
  @endpoint
end

#outputObject (readonly)

Returns the value of attribute output.



8
9
10
# File 'lib/engineyard-cloud-client/connection.rb', line 8

def output
  @output
end

#tokenObject

Returns the value of attribute token.



8
9
10
# File 'lib/engineyard-cloud-client/connection.rb', line 8

def token
  @token
end

#user_agentObject (readonly)

Returns the value of attribute user_agent.



8
9
10
# File 'lib/engineyard-cloud-client/connection.rb', line 8

def user_agent
  @user_agent
end

Instance Method Details

#==(other) ⇒ Object



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

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

#authenticate!(email, password) ⇒ Object



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

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

#authenticated?Boolean

Returns:

  • (Boolean)


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

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

#debug(name, value) ⇒ Object



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

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.gsub!(/[\r\n]./, "\n" + ' ' * (indent + 2)) # 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



59
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
# File 'lib/engineyard-cloud-client/connection.rb', line 59

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