lib/socialcastr/api.rb in socialcastr-0.0.1 vs lib/socialcastr/api.rb in socialcastr-0.1.0

- old
+ new

@@ -3,12 +3,10 @@ require 'openssl' require 'uri' require 'digest/md5' require 'cgi' - - module Socialcastr class API attr_accessor :debug def initialize(username, password, domain, format="xml",debug=false) @@ -17,112 +15,106 @@ @password = password @format = format @endpoint = "https://#{domain}/api/" return self end + + def get(path, args={}) + https_request(:get, path, args) + end - def messages(stream=nil, query={}) - method="messages" - method.insert(0, "streams/#{stream.id}/") if stream - xml = api_get(method, query) - return Socialcastr::MessageList.parse(xml).messages + def put(path, args={}) + https_request(:put, path, args) end - - def search(query) - method="messages/search" - xml = api_get(method, query) - return Socialcastr::MessageList.parse(xml).messages + + def post(path, args={}) + https_request(:post, path, args) end - def groups - method = "group_memberships" - xml = api_get(method) - return Socialcastr::GroupMembershipList.parse(xml).group_memberships + def delete(path, args={}) + https_request(:delete, path, args) end - - def streams - method = "streams" - xml = api_get(method) - return Socialcastr::StreamList.parse(xml).streams - end - - def add_message(message) - xml = api_post("messages", message) - return xml - end - - def add_comment(message_id, comment) - xml = api_post("messages/#{message_id}/comments", comment) - return xml - end - - def like_comment(message_id,comment_id) - xml = api_post("messages/#{message_id.to_s}/comments/#{comment_id.to_s}/likes") - return xml - end - - def unlike_comment(message_id,comment_id,like_id) - xml = api_delete("messages/#{message_id}/comments/#{comment_id}/likes/#{like_id}") - return xml - end - + def https_request(method, path, args) https = setup_https - response = "" - - # HACK - # if path == "messages/search" - # path = "messages" - # end - # data = File.read(File.join('/tmp','fixtures','xml', "#{path}.#{@format}")) - # return data - # # /HACK case method - when 'get' - request_class = Net::HTTP::Get - query=args - when 'post' - request_class = Net::HTTP::Post - form_data = args - when 'put' - request_class = Net::HTTP::Put - form_data = args - when 'delete' - request_class = Net::HTTP::Delete + when :get + request_class = Net::HTTP::Get + query=args + when :post + request_class = Net::HTTP::Post + form_data = args + when :put + request_class = Net::HTTP::Put + form_data = args + when :delete + request_class = Net::HTTP::Delete + else + raise InvalidMethod end + response = nil https.start do |session| - query_string = "/api/#{path}.#{@format}" - query_string += "?" + (query.collect { |k,v| "#{k}=#{CGI::escape(v.to_s)}" }.join('&')) unless query.nil? + query_string = build_query_string(path, query) req = request_class.new(query_string) req.basic_auth @username, @password if form_data req.set_form_data(args, ';') end - response = session.request(req).body + response = session.request(req) end - response + return handle_response(response).body end - def api_get(path, args={}) - https_request('get', path, args) - end - - - def api_post(path, args={}) - https_request('post', path, args) + + # Handles response and error codes from the remote service. + def handle_response(response) + case response.code.to_i + when 301,302 + raise(Redirection.new(response)) + when 200...400 + response + when 400 + raise(BadRequest.new(response)) + when 401 + raise(UnauthorizedAccess.new(response)) + when 403 + raise(ForbiddenAccess.new(response)) + when 404 + raise(ResourceNotFound.new(response)) + when 405 + raise(MethodNotAllowed.new(response)) + when 409 + raise(ResourceConflict.new(response)) + when 410 + raise(ResourceGone.new(response)) + when 422 + raise(ResourceInvalid.new(response)) + when 401...500 + raise(ClientError.new(response)) + when 500...600 + raise(ServerError.new(response)) + else + raise(ConnectionError.new(response, "Unknown response code: #{response.code}")) + end end - def api_delete(path, args={}) - https_request('delete', path, args) - end - def setup_https url = URI.parse(@endpoint) https = Net::HTTP.new(url.host, url.port) https.verify_mode = OpenSSL::SSL::VERIFY_NONE https.use_ssl = true return https + end + + def build_query_string(path, query=nil) + params = [] + unless query.nil? + params = query.collect do |k,v| + "#{k.to_s}=#{CGI::escape(v.to_s)}" + end + end + "/api#{path.to_s =~ /\// ? path.to_s : "/" + path.to_s }.#{@format}" + (params.any? ? "?" + params.join('&') : "") end end end