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:



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

#accountObject

Returns the value of attribute account.



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

def 
  @account
end

#apiObject

Returns the value of attribute api.



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

def api
  @api
end

#api_versionObject

Returns the value of attribute api_version.



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

def api_version
  @api_version
end

#formatObject

Returns the value of attribute format.



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

def format
  @format
end

#headersObject (readonly)

Returns the value of attribute headers.



6
7
8
# File 'lib/right_resource/connection.rb', line 6

def headers
  @headers
end

#logObject

Returns the value of attribute log.



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

def log
  @log
end

#loggerObject

Returns the value of attribute logger.



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

def logger
  @logger
end

#passwordObject

Returns the value of attribute password.



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

def password
  @password
end

#reraiseObject

Returns the value of attribute reraise.



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

def reraise
  @reraise
end

#resource_idObject (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

#responseObject (readonly)

Returns the value of attribute response.



6
7
8
# File 'lib/right_resource/connection.rb', line 6

def response
  @response
end

#statusObject (readonly)

Returns the value of attribute status.



6
7
8
# File 'lib/right_resource/connection.rb', line 6

def status
  @status
end

#usernameObject

Returns the value of attribute username.



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

def username
  @username
end

Instance Method Details

#clearObject

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.(params)


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

def (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

Returns:

  • (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