[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[sup-devel] [PATCH] put the heliotrope client into the Heliotrope namespace
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 <vivien.didelot@gmail.com>
---
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