Class: AMEE::Connection
- Inherits:
-
Object
- Object
- AMEE::Connection
- Defined in:
- lib/amee/connection.rb
Instance Attribute Summary collapse
-
#format ⇒ Object
readonly
Returns the value of attribute format.
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.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/amee/connection.rb', line 6 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 @auth_token = nil @format = [:format] || (defined?(JSON) ? :json : :xml) 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) @http.read_timeout = 5 @http.set_debug_output($stdout) if [:enable_debug] end |
Instance Attribute Details
#format ⇒ Object (readonly)
Returns the value of attribute format.
28 29 30 |
# File 'lib/amee/connection.rb', line 28 def format @format end |
Instance Method Details
#authenticate ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/amee/connection.rb', line 135 def authenticate response = nil post = Net::HTTP::Post.new("/auth/signIn") post.body = "username=#{@username}&password=#{@password}" post['Accept'] = content_type(:xml) response = @http.request(post) @auth_token = response['authToken'] unless authenticated? raise AMEE::AuthFailed.new("Authentication failed. Please check your username and 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
47 48 49 |
# File 'lib/amee/connection.rb', line 47 def authenticated? !@auth_token.nil? end |
#clear_cache ⇒ Object
226 227 228 229 230 |
# File 'lib/amee/connection.rb', line 226 def clear_cache if @enable_caching $cache = {} end end |
#delete(path) ⇒ Object
127 128 129 130 131 132 133 |
# File 'lib/amee/connection.rb', line 127 def delete(path) clear_cache # Create DELETE request delete = Net::HTTP::Delete.new(path) # Send request do_request(delete) end |
#get(path, data = {}) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/amee/connection.rb', line 51 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
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/amee/connection.rb', line 69 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
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/amee/connection.rb', line 98 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
85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/amee/connection.rb', line 85 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
114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/amee/connection.rb', line 114 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
30 31 32 |
# File 'lib/amee/connection.rb', line 30 def timeout @http.read_timeout end |
#timeout=(t) ⇒ Object
34 35 36 |
# File 'lib/amee/connection.rb', line 34 def timeout=(t) @http.read_timeout = t end |
#valid? ⇒ Boolean
43 44 45 |
# File 'lib/amee/connection.rb', line 43 def valid? @username && @password && @server end |
#version ⇒ Object
38 39 40 41 |
# File 'lib/amee/connection.rb', line 38 def version authenticate if @version.nil? @version end |