From mboxrd@z Thu Jan 1 00:00:00 1970 Received: by 10.142.50.5 with SMTP id x5cs50606wfx; Thu, 23 Jun 2011 23:30:46 -0700 (PDT) Received: by 10.52.100.136 with SMTP id ey8mr3836552vdb.136.1308897045435; Thu, 23 Jun 2011 23:30:45 -0700 (PDT) Return-Path: Received: from rubyforge.org (rubyforge.org [205.234.109.19]) by mx.google.com with ESMTP id o13si2907550vdt.135.2011.06.23.23.30.45; Thu, 23 Jun 2011 23:30:45 -0700 (PDT) Received-SPF: pass (google.com: domain of sup-devel-bounces@rubyforge.org designates 205.234.109.19 as permitted sender) client-ip=205.234.109.19; Authentication-Results: mx.google.com; spf=pass (google.com: domain of sup-devel-bounces@rubyforge.org designates 205.234.109.19 as permitted sender) smtp.mail=sup-devel-bounces@rubyforge.org; dkim=neutral (body hash did not verify) header.i=@gmail.com Received: from rubyforge.org (rubyforge.org [127.0.0.1]) by rubyforge.org (Postfix) with ESMTP id E25951598071; Fri, 24 Jun 2011 02:30:44 -0400 (EDT) Received: from mail-qw0-f50.google.com (mail-qw0-f50.google.com [209.85.216.50]) by rubyforge.org (Postfix) with ESMTP id E8CF41858367 for ; Fri, 24 Jun 2011 01:50:03 -0400 (EDT) Received: by mail-qw0-f50.google.com with SMTP id 5so1835650qwe.23 for ; Thu, 23 Jun 2011 22:50:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:from:to:cc:subject:date:message-id:x-mailer :in-reply-to:references; bh=sMFKlQj6onu369VQSzO698OYoeUJ+O0GBt/gMGgccb4=; b=Hny92TsiRElB9AqXOTyARRTF1t84Eq60aZiMtan7GmjdE/2h4qlm55QKmGZU/RYukO M0WN+eUzsvJRvnp3gw03aMy4KGl/ozNiSW/BP1TsfPUh446EXnLyTy2h3l7kvvmdB2Sx imIR7RtIy66LMMO9BXqcFcDt1+kxVMzw9SUFQ= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references; b=EpHY+FR3oYASXj2GbxxbfaWhtzsqpVZgscIpp1/w1UFFL2+UoEvY6SBlFLr/OqczYk 4Wu/srL8G6J+Lin2HYiKm4zEacBZrQuolMLTT61XJfKFOZ3bcjJYhfQVzh3R+Ziy2GJt KWM2aLCAGviMVsoVy+9qmgwv3DXavAuR8lDmE= Received: by 10.229.34.6 with SMTP id j6mr2214512qcd.188.1308894603258; Thu, 23 Jun 2011 22:50:03 -0700 (PDT) Received: from localhost (vpn.touchtunes.com [207.96.209.229]) by mx.google.com with ESMTPS id t21sm1754608qcs.26.2011.06.23.22.50.01 (version=TLSv1/SSLv3 cipher=OTHER); Thu, 23 Jun 2011 22:50:02 -0700 (PDT) From: Vivien Didelot To: William@rubyforge.org, Morgan@rubyforge.org, wmorgan-sup@masanjin.net Date: Fri, 24 Jun 2011 01:49:27 -0400 Message-Id: <1308894567-7174-2-git-send-email-vivien.didelot@gmail.com> X-Mailer: git-send-email 1.7.5.4 In-Reply-To: <1308894567-7174-1-git-send-email-vivien.didelot@gmail.com> References: <1308894567-7174-1-git-send-email-vivien.didelot@gmail.com> Cc: Sup developer discussion Subject: [sup-devel] [PATCH] put the heliotrope client into the Heliotrope namespace X-BeenThere: sup-devel@rubyforge.org X-Mailman-Version: 2.1.12 Precedence: list Reply-To: Sup developer discussion List-Id: Sup developer discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: sup-devel-bounces@rubyforge.org Errors-To: sup-devel-bounces@rubyforge.org Hi William, To start working on Turnsole,I think putting heliotrope-client under the Heliotrope namespace in a good start, to avoid confusion. This first patch is for the Heliotrope project. It moves lib/heliotrope-client.rb to lib/heliotrope/client.rb Another patch is sent with this email to update the Turnsole client. Signed-off-by: Vivien Didelot --- README | 2 +- lib/heliotrope-client.rb | 102 --------------------------------------------- lib/heliotrope/client.rb | 104 ++++++++++++++++++++++++++++++++++++++++++++++ lib/heliotrope/index.rb | 2 +- 4 files changed, 106 insertions(+), 104 deletions(-) delete mode 100644 lib/heliotrope-client.rb create mode 100644 lib/heliotrope/client.rb diff --git a/README b/README index ff82c1d..639e2c2 100644 --- a/README +++ b/README @@ -92,4 +92,4 @@ procmailrc or whatever. JSON API SPEC ------------- -Coming soon! For now, you can reverse engineer lib/heliotrope-client.rb. +Coming soon! For now, you can reverse engineer lib/heliotrope/client.rb. diff --git a/lib/heliotrope-client.rb b/lib/heliotrope-client.rb deleted file mode 100644 index 5b3a4bd..0000000 --- a/lib/heliotrope-client.rb +++ /dev/null @@ -1,102 +0,0 @@ -require 'rubygems' -require 'rest_client' -require 'json' - -class HeliotropeClient - class Error < StandardError; end - - MESSAGE_MUTABLE_STATE = Set.new %w(starred unread deleted) - MESSAGE_IMMUTABLE_STATE = Set.new %w(attachment signed encrypted draft sent) - MESSAGE_STATE = MESSAGE_MUTABLE_STATE + MESSAGE_IMMUTABLE_STATE - - attr_reader :url - def initialize url - @url = url - @resource = RestClient::Resource.new url - end - - def search query, num=20, start=0 - v = get_json "search", :q => query, :start => start, :num => num - v["results"] - end - - def count query - get_json("count", :q => query)["count"] - end - - def thread id; get_json("thread/#{id}")["messageinfos"] end - def threadinfo id; get_json("thread/#{id}/info") end - - def message id, preferred_mime_type="text/plain" - get_json "message/#{id}", :mime_type_pref => preferred_mime_type - end - - def send_message message, opts={} - post_json "message/send", :message => message, :labels => (opts[:labels] || []).to_json - end - - def message_part message_id, part_id - ## not a json blob, but a binary region - @resource["message/#{message_id}/part/#{part_id}"].get - end - - def raw_message message_id - ## not a json blob, but a binary region - @resource["message/#{message_id}/raw"].get - end - - def labels; get_json("labels")["labels"] end - def info; get_json("info") end - def size; get_json("size")["size"] end - - def prune_labels!; post_json("labels/prune")["labels"] end - - def set_labels! thread_id, labels - post_json "thread/#{thread_id}/labels", :labels => labels.to_json - end - - def set_state! message_id, state - post_json "message/#{message_id}/state", :state => state.to_json - end - - def set_thread_state! thread_id, state - post_json "thread/#{thread_id}/state", :state => state.to_json - end - -private - - def get_json path, params={} - handle_errors do - response = @resource[path + ".json"].get :params => params - response.force_encoding Encoding::UTF_8 if in_ruby19_hell? - JSON.parse response - end - end - - def post_json path, params={ :please => "1" } # you need to have at least one param for RestClient to work... lame - handle_errors do - response = @resource[path + ".json"].post params - response.force_encoding Encoding::UTF_8 if in_ruby19_hell? - JSON.parse response - end - end - - def handle_errors - begin - v = yield - raise Error, "invalid response: #{v.inspect[0..200]}" unless v.is_a?(Hash) - case v["response"] - when "ok"; v - when "error"; raise Error, v["message"] - else raise Error, "invalid response: #{v.inspect[0..200]}" - end - rescue SystemCallError, RestClient::Exception, JSON::ParserError => e - raise Error, "#{e.message} (#{e.class})" - end - end - - def in_ruby19_hell? - @in_ruby19_hell = "".respond_to?(:encoding) if @in_ruby19_hell.nil? - @in_ruby19_hell - end -end diff --git a/lib/heliotrope/client.rb b/lib/heliotrope/client.rb new file mode 100644 index 0000000..386efc9 --- /dev/null +++ b/lib/heliotrope/client.rb @@ -0,0 +1,104 @@ +require 'rubygems' +require 'rest_client' +require 'json' + +module Heliotrope +class Client + class Error < StandardError; end + + MESSAGE_MUTABLE_STATE = Set.new %w(starred unread deleted) + MESSAGE_IMMUTABLE_STATE = Set.new %w(attachment signed encrypted draft sent) + MESSAGE_STATE = MESSAGE_MUTABLE_STATE + MESSAGE_IMMUTABLE_STATE + + attr_reader :url + def initialize url + @url = url + @resource = RestClient::Resource.new url + end + + def search query, num=20, start=0 + v = get_json "search", :q => query, :start => start, :num => num + v["results"] + end + + def count query + get_json("count", :q => query)["count"] + end + + def thread id; get_json("thread/#{id}")["messageinfos"] end + def threadinfo id; get_json("thread/#{id}/info") end + + def message id, preferred_mime_type="text/plain" + get_json "message/#{id}", :mime_type_pref => preferred_mime_type + end + + def send_message message, opts={} + post_json "message/send", :message => message, :labels => (opts[:labels] || []).to_json + end + + def message_part message_id, part_id + ## not a json blob, but a binary region + @resource["message/#{message_id}/part/#{part_id}"].get + end + + def raw_message message_id + ## not a json blob, but a binary region + @resource["message/#{message_id}/raw"].get + end + + def labels; get_json("labels")["labels"] end + def info; get_json("info") end + def size; get_json("size")["size"] end + + def prune_labels!; post_json("labels/prune")["labels"] end + + def set_labels! thread_id, labels + post_json "thread/#{thread_id}/labels", :labels => labels.to_json + end + + def set_state! message_id, state + post_json "message/#{message_id}/state", :state => state.to_json + end + + def set_thread_state! thread_id, state + post_json "thread/#{thread_id}/state", :state => state.to_json + end + +private + + def get_json path, params={} + handle_errors do + response = @resource[path + ".json"].get :params => params + response.force_encoding Encoding::UTF_8 if in_ruby19_hell? + JSON.parse response + end + end + + def post_json path, params={ :please => "1" } # you need to have at least one param for RestClient to work... lame + handle_errors do + response = @resource[path + ".json"].post params + response.force_encoding Encoding::UTF_8 if in_ruby19_hell? + JSON.parse response + end + end + + def handle_errors + begin + v = yield + raise Error, "invalid response: #{v.inspect[0..200]}" unless v.is_a?(Hash) + case v["response"] + when "ok"; v + when "error"; raise Error, v["message"] + else raise Error, "invalid response: #{v.inspect[0..200]}" + end + rescue SystemCallError, RestClient::Exception, JSON::ParserError => e + raise Error, "#{e.message} (#{e.class})" + end + end + + def in_ruby19_hell? + @in_ruby19_hell = "".respond_to?(:encoding) if @in_ruby19_hell.nil? + @in_ruby19_hell + end +end +end diff --git a/lib/heliotrope/index.rb b/lib/heliotrope/index.rb index 48ea09e..197ba91 100644 --- a/lib/heliotrope/index.rb +++ b/lib/heliotrope/index.rb @@ -33,7 +33,7 @@ class Index MESSAGE_IMMUTABLE_STATE = Set.new %w(attachment signed encrypted draft sent) MESSAGE_STATE = MESSAGE_MUTABLE_STATE + MESSAGE_IMMUTABLE_STATE ## if you change any of those state things, be sure to update - ## heliotrope-client as well. + ## heliotrope/client as well. SNIPPET_MAX_SIZE = 100 # chars -- 1.7.5.4 _______________________________________________ Sup-devel mailing list Sup-devel@rubyforge.org http://rubyforge.org/mailman/listinfo/sup-devel