Class: RightResource::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/right_resource/connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) {|_self| ... } ⇒ Connection

Set RestFul Client Parameters

Yields:

  • (_self)

Yield Parameters:



7
8
9
10
11
12
13
14
15
# File 'lib/right_resource/connection.rb', line 7

def initialize(params={})
  @api_version = API_VERSION
  @url = params[:url] || "https://my.rightscale.com"
  @api = "#{@url}/api/acct/"
  @format = params[:format] || RightResource::Formats::JsonFormat
  @logger = params[:logger] || Logger.new(STDERR).tap {|l| l.level = Logger::WARN}
  RestClient.log = STDERR if @logger.level == Logger::DEBUG # HTTP request/response log
  yield self if block_given?  # RightResource::Connection.new {|conn| ...}
end

Instance Attribute Details

#accountObject

Returns the value of attribute account.



3
4
5
# File 'lib/right_resource/connection.rb', line 3

def 
  @account
end

#apiObject

Returns the value of attribute api.



3
4
5
# File 'lib/right_resource/connection.rb', line 3

def api
  @api
end

#api_versionObject

Returns the value of attribute api_version.



3
4
5
# File 'lib/right_resource/connection.rb', line 3

def api_version
  @api_version
end

#formatObject

Returns the value of attribute format.



3
4
5
# File 'lib/right_resource/connection.rb', line 3

def format
  @format
end

#headersObject (readonly)

Returns the value of attribute headers.



4
5
6
# File 'lib/right_resource/connection.rb', line 4

def headers
  @headers
end

#logObject

Returns the value of attribute log.



3
4
5
# File 'lib/right_resource/connection.rb', line 3

def log
  @log
end

#loggerObject

Returns the value of attribute logger.



3
4
5
# File 'lib/right_resource/connection.rb', line 3

def logger
  @logger
end

#open_timeoutObject (readonly)

Returns the value of attribute open_timeout.



4
5
6
# File 'lib/right_resource/connection.rb', line 4

def open_timeout
  @open_timeout
end

#passwordObject

Returns the value of attribute password.



3
4
5
# File 'lib/right_resource/connection.rb', line 3

def password
  @password
end

#resource_idObject (readonly)

Returns the value of attribute resource_id.



4
5
6
# File 'lib/right_resource/connection.rb', line 4

def resource_id
  @resource_id
end

#responseObject (readonly)

Returns the value of attribute response.



4
5
6
# File 'lib/right_resource/connection.rb', line 4

def response
  @response
end

#status_codeObject (readonly)

Returns the value of attribute status_code.



4
5
6
# File 'lib/right_resource/connection.rb', line 4

def status_code
  @status_code
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



4
5
6
# File 'lib/right_resource/connection.rb', line 4

def timeout
  @timeout
end

#urlObject

Returns the value of attribute url.



3
4
5
# File 'lib/right_resource/connection.rb', line 3

def url
  @url
end

#usernameObject

Returns the value of attribute username.



3
4
5
# File 'lib/right_resource/connection.rb', line 3

def username
  @username
end

Instance Method Details

#clearObject

Resource clear



71
72
73
74
75
# File 'lib/right_resource/connection.rb', line 71

def clear
  @response = @headers = @resource_id = @status_code = nil
ensure
  @logger.debug {"#{__FILE__} #{__LINE__}: #{self.class}\n#{self.pretty_inspect}\n"}
end

#delete(path, headers = {}) ⇒ Object

destory



94
95
96
# File 'lib/right_resource/connection.rb', line 94

def delete(path, headers={})
  self.request(path, "delete", headers)
end

#get(path, headers = {}) ⇒ Object

HTTP methods show|index



79
80
81
# File 'lib/right_resource/connection.rb', line 79

def get(path, headers={})
  self.request(path, "get", headers)
end

#login(params = {}) ⇒ Object

RestClient authentication

Examples

params = {:username => "foo", :password => "bar", :account => "1"}
conn = Connection.new
conn.(params)


22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/right_resource/connection.rb', line 22

def (params={})
  @username = params[:username] if params[:username]
  @password = params[:password] if params[:password]
  @account = params[:account] if params[:account]
  @open_timeout = params[:open_timeout] || 10 # Connection.timeout
  @timeout = params[:timeout] || 60           # Response.read.timeout
  req_opts = {:user => @username, :password => @password, :open_timeout => @open_timeout, :timeout => @timeout}
  @api_object = RestClient::Resource.new("#{@api}#{@account}", req_opts)
rescue => e
  @logger.error("#{e.class}: #{e.pretty_inspect}")
  @logger.debug {"Backtrace:\n#{e.backtrace.pretty_inspect}"}
ensure
  @logger.debug {"#{__FILE__} #{__LINE__}: #{self.class}\n#{self.pretty_inspect}\n"}
end

#login?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/right_resource/connection.rb', line 37

def login?
  @api_object ? true : false
end

#post(path, headers = {}) ⇒ Object

create



84
85
86
# File 'lib/right_resource/connection.rb', line 84

def post(path, headers={})
  self.request(path, "post", headers)
end

#put(path, headers = {}) ⇒ Object

update



89
90
91
# File 'lib/right_resource/connection.rb', line 89

def put(path, headers={})
  self.request(path, "put", headers)
end

#request(path, method = "get", headers = {}) ⇒ Object

Send HTTP Request



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/right_resource/connection.rb', line 42

def request(path, method="get", headers={})
  raise "Not Login!!" unless self.login?
#      if /(.xml|.js)/ =~ path
#        path = URI.encode(path)
#      elsif /\?(.*)$/ =~ path
#        path = URI.encode("#{path}&format=#{@format.extension}")
#      else
#        path = URI.encode("#{path}?format=#{@format.extension}")
#      end
  unless method.match(/(get|put|post|delete)/)
    raise "Invalid Action: get|put|post|delete only"
  end
  api_version = {:x_api_version => @api_version, :api_version => @api_version}
  @response = @api_object[path].__send__(method.to_sym, api_version.merge(headers))
  @status_code = @response.code
  @headers = @response.headers
  @resource_id = @headers[:location].match(/\d+$/).to_s unless @headers[:location].nil?
  @response.body
rescue Timeout::Error => e
  raise TimeoutError.new(e.message)
rescue => e
  @status_code = e.http_code
  @logger.error("#{e.class}: #{e.pretty_inspect}")
  @logger.debug {"Backtrace:\n#{e.backtrace.pretty_inspect}"}
ensure
  @logger.debug {"#{__FILE__} #{__LINE__}: #{self.class}\n#{self.pretty_inspect}\n"}
end