Class: RightResource::Connection
- Defined in:
- lib/right_resource/connection.rb
Instance Attribute Summary collapse
-
#account ⇒ Object
Returns the value of attribute account.
-
#api ⇒ Object
Returns the value of attribute api.
-
#api_version ⇒ Object
Returns the value of attribute api_version.
-
#format ⇒ Object
Returns the value of attribute format.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#log ⇒ Object
Returns the value of attribute log.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#password ⇒ Object
Returns the value of attribute password.
-
#reraise ⇒ Object
Returns the value of attribute reraise.
-
#resource_id ⇒ Object
readonly
Returns the value of attribute resource_id.
-
#response ⇒ Object
readonly
Returns the value of attribute response.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
-
#username ⇒ Object
Returns the value of attribute username.
Instance Method Summary collapse
-
#clear ⇒ Object
Resource clear.
-
#delete(path, headers = {}) ⇒ Object
destory.
-
#get(path, headers = {}) ⇒ Object
HTTP methods show|index.
-
#initialize(params = {}) {|_self| ... } ⇒ Connection
constructor
Set RestFul Client Parameters.
-
#login(params = {}) ⇒ Object
RestClient authentication === Examples params = => “foo”, :password => “bar”, :account => “1” conn = Connection.new conn.login(params).
- #login? ⇒ Boolean
-
#post(path, headers = {}) ⇒ Object
create.
-
#put(path, headers = {}) ⇒ Object
update.
-
#request(path, method = "get", headers = {}) ⇒ Object
Send HTTP Request.
Constructor Details
#initialize(params = {}) {|_self| ... } ⇒ Connection
Set RestFul Client Parameters
9 10 11 12 13 14 15 16 |
# File 'lib/right_resource/connection.rb', line 9 def initialize(params={}) @api_version = API_VERSION @api = "https://my.rightscale.com/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
#account ⇒ Object
Returns the value of attribute account.
5 6 7 |
# File 'lib/right_resource/connection.rb', line 5 def account @account end |
#api ⇒ Object
Returns the value of attribute api.
5 6 7 |
# File 'lib/right_resource/connection.rb', line 5 def api @api end |
#api_version ⇒ Object
Returns the value of attribute api_version.
5 6 7 |
# File 'lib/right_resource/connection.rb', line 5 def api_version @api_version end |
#format ⇒ Object
Returns the value of attribute format.
5 6 7 |
# File 'lib/right_resource/connection.rb', line 5 def format @format end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
6 7 8 |
# File 'lib/right_resource/connection.rb', line 6 def headers @headers end |
#log ⇒ Object
Returns the value of attribute log.
5 6 7 |
# File 'lib/right_resource/connection.rb', line 5 def log @log end |
#logger ⇒ Object
Returns the value of attribute logger.
5 6 7 |
# File 'lib/right_resource/connection.rb', line 5 def logger @logger end |
#password ⇒ Object
Returns the value of attribute password.
5 6 7 |
# File 'lib/right_resource/connection.rb', line 5 def password @password end |
#reraise ⇒ Object
Returns the value of attribute reraise.
5 6 7 |
# File 'lib/right_resource/connection.rb', line 5 def reraise @reraise end |
#resource_id ⇒ Object (readonly)
Returns the value of attribute resource_id.
6 7 8 |
# File 'lib/right_resource/connection.rb', line 6 def resource_id @resource_id end |
#response ⇒ Object (readonly)
Returns the value of attribute response.
6 7 8 |
# File 'lib/right_resource/connection.rb', line 6 def response @response end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
6 7 8 |
# File 'lib/right_resource/connection.rb', line 6 def status @status end |
#username ⇒ Object
Returns the value of attribute username.
5 6 7 |
# File 'lib/right_resource/connection.rb', line 5 def username @username end |
Instance Method Details
#clear ⇒ Object
Resource clear
70 71 72 73 74 |
# File 'lib/right_resource/connection.rb', line 70 def clear @response = @headers = @resource_id = @status = nil ensure @logger.debug {"#{__FILE__} #{__LINE__}: #{self.class}\n#{self.pretty_inspect}\n"} end |
#delete(path, headers = {}) ⇒ Object
destory
93 94 95 |
# File 'lib/right_resource/connection.rb', line 93 def delete(path, headers={}) self.request(path, "delete", headers) end |
#get(path, headers = {}) ⇒ Object
HTTP methods show|index
78 79 80 |
# File 'lib/right_resource/connection.rb', line 78 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.login(params)
23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/right_resource/connection.rb', line 23 def login(params={}) @username = params[:username] unless params[:username].nil? || params[:username].empty? @password = params[:password] unless params[:password].nil? || params[:password].empty? @account = params[:account] unless params[:account].nil? || params[:account].empty? @api_object = RestClient::Resource.new("#{@api}#{@account}", @username, @password) rescue => e @logger.error("#{e.class}: #{e.pretty_inspect}") @logger.debug {"Backtrace:\n#{e.backtrace.pretty_inspect}"} raise if self.reraise ensure @logger.debug {"#{__FILE__} #{__LINE__}: #{self.class}\n#{self.pretty_inspect}\n"} end |
#login? ⇒ Boolean
36 37 38 |
# File 'lib/right_resource/connection.rb', line 36 def login? @api_object ? true : false end |
#post(path, headers = {}) ⇒ Object
create
83 84 85 |
# File 'lib/right_resource/connection.rb', line 83 def post(path, headers={}) self.request(path, "post", headers) end |
#put(path, headers = {}) ⇒ Object
update
88 89 90 |
# File 'lib/right_resource/connection.rb', line 88 def put(path, headers={}) self.request(path, "put", headers) end |
#request(path, method = "get", headers = {}) ⇒ Object
Send HTTP Request
41 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 |
# File 'lib/right_resource/connection.rb', line 41 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 = @response.code @headers = @response.headers @resource_id = @headers[:location].match(/\d+$/).to_s unless @headers[:location].nil? @response.body rescue => e @status = e.http_code @logger.error("#{e.class}: #{e.pretty_inspect}") @logger.debug {"Backtrace:\n#{e.backtrace.pretty_inspect}"} raise if self.reraise ensure @logger.debug {"#{__FILE__} #{__LINE__}: #{self.class}\n#{self.pretty_inspect}\n"} end |