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.
-
#open_timeout ⇒ Object
readonly
Returns the value of attribute open_timeout.
-
#password ⇒ Object
Returns the value of attribute password.
-
#resource_id ⇒ Object
readonly
Returns the value of attribute resource_id.
-
#response ⇒ Object
readonly
Returns the value of attribute response.
-
#status_code ⇒ Object
readonly
Returns the value of attribute status_code.
-
#timeout ⇒ Object
readonly
Returns the value of attribute timeout.
-
#url ⇒ Object
Returns the value of attribute url.
-
#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
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
#account ⇒ Object
Returns the value of attribute account.
3 4 5 |
# File 'lib/right_resource/connection.rb', line 3 def account @account end |
#api ⇒ Object
Returns the value of attribute api.
3 4 5 |
# File 'lib/right_resource/connection.rb', line 3 def api @api end |
#api_version ⇒ Object
Returns the value of attribute api_version.
3 4 5 |
# File 'lib/right_resource/connection.rb', line 3 def api_version @api_version end |
#format ⇒ Object
Returns the value of attribute format.
3 4 5 |
# File 'lib/right_resource/connection.rb', line 3 def format @format end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
4 5 6 |
# File 'lib/right_resource/connection.rb', line 4 def headers @headers end |
#log ⇒ Object
Returns the value of attribute log.
3 4 5 |
# File 'lib/right_resource/connection.rb', line 3 def log @log end |
#logger ⇒ Object
Returns the value of attribute logger.
3 4 5 |
# File 'lib/right_resource/connection.rb', line 3 def logger @logger end |
#open_timeout ⇒ Object (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 |
#password ⇒ Object
Returns the value of attribute password.
3 4 5 |
# File 'lib/right_resource/connection.rb', line 3 def password @password end |
#resource_id ⇒ Object (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 |
#response ⇒ Object (readonly)
Returns the value of attribute response.
4 5 6 |
# File 'lib/right_resource/connection.rb', line 4 def response @response end |
#status_code ⇒ Object (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 |
#timeout ⇒ Object (readonly)
Returns the value of attribute timeout.
4 5 6 |
# File 'lib/right_resource/connection.rb', line 4 def timeout @timeout end |
#url ⇒ Object
Returns the value of attribute url.
3 4 5 |
# File 'lib/right_resource/connection.rb', line 3 def url @url end |
#username ⇒ Object
Returns the value of attribute username.
3 4 5 |
# File 'lib/right_resource/connection.rb', line 3 def username @username end |
Instance Method Details
#clear ⇒ Object
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.login(params)
22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/right_resource/connection.rb', line 22 def login(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
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.) 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 |