Class: Cloudsponge::Utility
- Inherits:
-
Object
- Object
- Cloudsponge::Utility
- Defined in:
- lib/cloudsponge/utility.rb
Class Method Summary collapse
- .decode_response(response) ⇒ Object
- .decode_response_body(response, format = 'json') ⇒ Object
- .get_and_decode_response(full_url, auth) ⇒ Object
- .get_url(url, auth) ⇒ Object
- .object_to_query(object) ⇒ Object
- .open_http(url, params, auth, method) ⇒ Object
- .post_and_decode_response(url, params, auth) ⇒ Object
- .post_url(url, params, auth) ⇒ Object
Class Method Details
.decode_response(response) ⇒ Object
31 32 33 34 35 36 37 38 39 40 |
# File 'lib/cloudsponge/utility.rb', line 31 def self.decode_response(response) if response.code_type == Net::HTTPOK # decode the response into an asscoiative array resp = decode_response_body(response.body, 'json') raise CsException.new(resp['error']['message'], response['code']) if resp['error'] else raise CsException.new(response.body, response.code) end resp end |
.decode_response_body(response, format = 'json') ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/cloudsponge/utility.rb', line 42 def self.decode_response_body(response, format = 'json') # TODO: account for systems that use a different JSON parser. Look for json gem... # TODO: implement alternate formats: XML object = {'error' => {'message' => 'failed to parse data.', 'code' => 1}} begin object = ActiveSupport::JSON.decode(response) rescue begin object = JSON.parse(response) rescue end end object end |
.get_and_decode_response(full_url, auth) ⇒ Object
25 26 27 28 29 |
# File 'lib/cloudsponge/utility.rb', line 25 def self.get_and_decode_response(full_url, auth) # get the response response = get_url(full_url, auth) decode_response(response) end |
.get_url(url, auth) ⇒ Object
57 58 59 60 |
# File 'lib/cloudsponge/utility.rb', line 57 def self.get_url(url, auth) url = URI.parse(url) open_http(url, url.query, auth, :get) end |
.object_to_query(object) ⇒ Object
14 15 16 17 |
# File 'lib/cloudsponge/utility.rb', line 14 def self.object_to_query(object) return object unless object.is_a? Hash object.map{ |k,v| "#{CGI.escape(k.to_s)}=#{CGI.escape(v.to_s)}" }.join('&') end |
.open_http(url, params, auth, method) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/cloudsponge/utility.rb', line 67 def self.open_http(url, params, auth, method) http = Net::HTTP.new(url.host, url.port) if url.port == 443 http.use_ssl = true end request = if method == :post Net::HTTP::Post.new(url.request_uri) else Net::HTTP::Get.new(url.request_uri) end request.set_form_data(params) if method == :post request.basic_auth(auth[:domain_key], auth[:domain_password]) http.request(request) end |
.post_and_decode_response(url, params, auth) ⇒ Object
19 20 21 22 23 |
# File 'lib/cloudsponge/utility.rb', line 19 def self.post_and_decode_response(url, params, auth) # post the response response = post_url(url, params, auth) decode_response(response) end |
.post_url(url, params, auth) ⇒ Object
62 63 64 65 |
# File 'lib/cloudsponge/utility.rb', line 62 def self.post_url(url, params, auth) url = URI.parse(url) open_http(url, params, auth, :post) end |