Class: Rubyku::Request
- Inherits:
-
Object
- Object
- Rubyku::Request
- Defined in:
- lib/rubyku/request.rb
Overview
This is a utility class that handles communication with the Jaiku server.
Class Method Summary collapse
-
.retrieve_json_object(url_path, jaiku_credentials = Rubyku::Settings.jaiku_credentials) ⇒ Object
This method uses the given url and credentials to retrieve JSON data from the Jaiku servers.
-
.update(message, jaiku_credentials = Rubyku::Settings.jaiku_credentials) ⇒ Object
This method will update the presence for the specified user credentials.
Class Method Details
.retrieve_json_object(url_path, jaiku_credentials = Rubyku::Settings.jaiku_credentials) ⇒ Object
This method uses the given url and credentials to retrieve JSON data from the Jaiku servers. returns: Hash containing the JSON data.
raises:
-
Rubyku::Error if there is a problem communicating with the servers.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/rubyku/request.rb', line 16 def retrieve_json_object(url_path, jaiku_credentials = Rubyku::Settings.jaiku_credentials) log = Rubyku::Settings.logger log.info("About to request JSON from #{url_path}") url_path += jaiku_credentials.request_string log.debug("Using full URL to fetch JSON: #{url_path}") url = URI.parse(url_path) req = Net::HTTP::Get.new(url.request_uri) res = Net::HTTP::Proxy(Rubyku::Settings.proxy_url, Rubyku::Settings.proxy_port) \ .start(url.host, url.port) {|http| http.request(req) } log.info("Jaiku JSON response returned HTTP code #{res.code}") log.debug("About to return JSON: " + res.body) JSON(res.body) end |
.update(message, jaiku_credentials = Rubyku::Settings.jaiku_credentials) ⇒ Object
This method will update the presence for the specified user credentials.
returns: nil
raises:
-
Rubyku::Error if there was a problem communicating with the Jaiku servers.
-
Rubyku::UpdateError if there was an error within Jaiku attempting to parse the update.
TODO: Still need to add support for generated posts, icons, and locations.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/rubyku/request.rb', line 45 def update(, jaiku_credentials = Rubyku::Settings.jaiku_credentials) log = Rubyku::Settings.logger log.info("Attempting to update with message #{}") url = URI.parse('http://api.jaiku.com/json') res = Net::HTTP.post_form(url, {'method'=> 'presence.send', 'message'=> , 'user' => jaiku_credentials.username, 'personal_key' => jaiku_credentials.api_key}) unless res.code == "200" raise Rubyku::Error, "Problem communicating with Jaiku servers." end response = JSON(res.body) unless response['status'] == "ok" raise Rubyku::UpdateError, "Error updating presence, #{response.status}" end end |