Class: AMEE::Connection
- Inherits:
-
Object
- Object
- AMEE::Connection
- Defined in:
- lib/amee/connection.rb
Constant Summary collapse
- RootCA =
File.dirname(__FILE__) + '/../../cacert.pem'
Instance Attribute Summary collapse
-
#format ⇒ Object
readonly
Returns the value of attribute format.
-
#password ⇒ Object
readonly
Returns the value of attribute password.
-
#server ⇒ Object
readonly
Returns the value of attribute server.
-
#username ⇒ Object
readonly
Returns the value of attribute username.
Instance Method Summary collapse
- #authenticate ⇒ Object
- #authenticated? ⇒ Boolean
- #clear_cache ⇒ Object
- #delete(path) ⇒ Object
- #get(path, data = {}) ⇒ Object
-
#initialize(server, username, password, options = {}) ⇒ Connection
constructor
A new instance of Connection.
- #post(path, data = {}) ⇒ Object
- #put(path, data = {}) ⇒ Object
- #raw_post(path, body, options = {}) ⇒ Object
- #raw_put(path, body, options = {}) ⇒ Object
- #timeout ⇒ Object
- #timeout=(t) ⇒ Object
- #valid? ⇒ Boolean
- #version ⇒ Object
Constructor Details
#initialize(server, username, password, options = {}) ⇒ Connection
Returns a new instance of Connection.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/amee/connection.rb', line 9 def initialize(server, username, password, = {}) unless .is_a?(Hash) raise AMEE::ArgumentError.new("Fourth argument must be a hash of options!") end @server = server @username = username @password = password @ssl = ([:ssl] == false) ? false : true @port = @ssl ? 443 : 80 @auth_token = nil @format = [:format] || (defined?(JSON) ? :json : :xml) @amee_source = [:amee_source] if !valid? raise "You must supply connection details - server, username and password are all required!" end @enable_caching = [:enable_caching] if @enable_caching $cache ||= {} end # Make connection to server @http = Net::HTTP.new(@server, @port) if @ssl == true @http.use_ssl = true if File.exists? RootCA @http.ca_file = RootCA @http.verify_mode = OpenSSL::SSL::VERIFY_PEER @http.verify_depth = 5 end end @http.read_timeout = 60 @http.set_debug_output($stdout) if [:enable_debug] @debug = [:enable_debug] end |
Instance Attribute Details
#format ⇒ Object (readonly)
Returns the value of attribute format.
43 44 45 |
# File 'lib/amee/connection.rb', line 43 def format @format end |
#password ⇒ Object (readonly)
Returns the value of attribute password.
46 47 48 |
# File 'lib/amee/connection.rb', line 46 def password @password end |
#server ⇒ Object (readonly)
Returns the value of attribute server.
44 45 46 |
# File 'lib/amee/connection.rb', line 44 def server @server end |
#username ⇒ Object (readonly)
Returns the value of attribute username.
45 46 47 |
# File 'lib/amee/connection.rb', line 45 def username @username end |
Instance Method Details
#authenticate ⇒ Object
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/amee/connection.rb', line 153 def authenticate response = nil post = Net::HTTP::Post.new("/auth/signIn") post.body = "username=#{@username}&password=#{@password}" post['Accept'] = content_type(:xml) post['X-AMEE-Source'] = @amee_source if @amee_source response = @http.request(post) @auth_token = response['authToken'] unless authenticated? raise AMEE::AuthFailed.new("Authentication failed. Please check your username and password. (tried #{@username},#{@password})") end # Detect API version if response.body.is_json? @version = JSON.parse(response.body)["user"]["apiVersion"].to_f elsif response.body.is_xml? @version = REXML::Document.new(response.body).elements['Resources'].elements['SignInResource'].elements['User'].elements['ApiVersion'].text.to_f else @version = 1.0 end end |
#authenticated? ⇒ Boolean
65 66 67 |
# File 'lib/amee/connection.rb', line 65 def authenticated? !@auth_token.nil? end |
#clear_cache ⇒ Object
247 248 249 250 251 |
# File 'lib/amee/connection.rb', line 247 def clear_cache if @enable_caching $cache = {} end end |
#delete(path) ⇒ Object
145 146 147 148 149 150 151 |
# File 'lib/amee/connection.rb', line 145 def delete(path) clear_cache # Create DELETE request delete = Net::HTTP::Delete.new(path) # Send request do_request(delete) end |
#get(path, data = {}) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/amee/connection.rb', line 69 def get(path, data = {}) # Allow format override format = data.delete(:format) || @format # Create URL parameters params = [] data.each_pair do |key, value| params << "#{CGI::escape(key.to_s)}=#{CGI::escape(value.to_s)}" end if params.size > 0 path += "?#{params.join('&')}" end # Send request return $cache[path] if @enable_caching and $cache[path] response = do_request(Net::HTTP::Get.new(path), format) $cache[path] = response if @enable_caching return response end |
#post(path, data = {}) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/amee/connection.rb', line 87 def post(path, data = {}) # Allow format override format = data.delete(:format) || @format # Clear cache clear_cache # Create POST request post = Net::HTTP::Post.new(path) body = [] data.each_pair do |key, value| body << "#{CGI::escape(key.to_s)}=#{CGI::escape(value.to_s)}" end post.body = body.join '&' # Send request do_request(post, format) end |
#put(path, data = {}) ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/amee/connection.rb', line 116 def put(path, data = {}) # Allow format override format = data.delete(:format) || @format # Clear cache clear_cache # Create PUT request put = Net::HTTP::Put.new(path) body = [] data.each_pair do |key, value| body << "#{CGI::escape(key.to_s)}=#{CGI::escape(value.to_s)}" end put.body = body.join '&' # Send request do_request(put, format) end |
#raw_post(path, body, options = {}) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/amee/connection.rb', line 103 def raw_post(path, body, = {}) # Allow format override format = .delete(:format) || @format # Clear cache clear_cache # Create POST request post = Net::HTTP::Post.new(path) post['Content-type'] = [:content_type] || content_type(format) post.body = body # Send request do_request(post, format) end |
#raw_put(path, body, options = {}) ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/amee/connection.rb', line 132 def raw_put(path, body, = {}) # Allow format override format = .delete(:format) || @format # Clear cache clear_cache # Create PUT request put = Net::HTTP::Put.new(path) put['Content-type'] = [:content_type] || content_type(format) put.body = body # Send request do_request(put, format) end |
#timeout ⇒ Object
48 49 50 |
# File 'lib/amee/connection.rb', line 48 def timeout @http.read_timeout end |
#timeout=(t) ⇒ Object
52 53 54 |
# File 'lib/amee/connection.rb', line 52 def timeout=(t) @http.read_timeout = t end |
#valid? ⇒ Boolean
61 62 63 |
# File 'lib/amee/connection.rb', line 61 def valid? @username && @password && @server end |
#version ⇒ Object
56 57 58 59 |
# File 'lib/amee/connection.rb', line 56 def version authenticate if @version.nil? @version end |