aboutsummaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
authorKazuki Yamaguchi <k@rhe.jp>2015-11-08 15:45:49 +0900
committerKazuki Yamaguchi <k@rhe.jp>2015-11-08 15:45:49 +0900
commitebc34c914a07cb8ae543b3ac5113718eefa6b87a (patch)
tree49f47c5694c6ccc09345f93d5c7b9cfad3ec25f3 /examples
parent4fd79fb69c1f49620d9ec19497fd8a2096fcc44f (diff)
downloadplum-ebc34c914a07cb8ae543b3ac5113718eefa6b87a.tar.gz
examples: add client examples
Diffstat (limited to 'examples')
-rw-r--r--examples/client/large.rb20
-rw-r--r--examples/client/twitter.rb51
2 files changed, 71 insertions, 0 deletions
diff --git a/examples/client/large.rb b/examples/client/large.rb
new file mode 100644
index 0000000..c1007ab
--- /dev/null
+++ b/examples/client/large.rb
@@ -0,0 +1,20 @@
+# -*- frozen-string-literal: true -*-
+# client/large.rb: download 3 large files in parallel
+$LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
+require "plum"
+
+def log(s)
+ puts "[#{Time.now.strftime("%Y/%m/%d %H:%M:%S.%L")}] #{s}"
+end
+
+Plum::Client.start("http2.golang.org", 443, http2_settings: { max_frame_size: 32768 }) { |rest|
+ 3.times { |i|
+ rest.get_async("/file/go.src.tar.gz",
+ "accept-encoding" => "identity;q=1") { |res|
+ log "#{i}: #{res.status}"
+ res.on_chunk { |chunk|
+ log "#{i}: chunk: #{chunk.size}"
+ }
+ }
+ }
+}
diff --git a/examples/client/twitter.rb b/examples/client/twitter.rb
new file mode 100644
index 0000000..6f936a6
--- /dev/null
+++ b/examples/client/twitter.rb
@@ -0,0 +1,51 @@
+# -*- frozen-string-literal: true -*-
+# client/twitter.rb
+# Twitter の User stream に(現在はサーバーが非対応のため)HTTP/1.1 を使用して接続する。
+# 「にゃーん」を含むツイートを受信したら、REST API で HTTP/2 を使用して返信する。
+$LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
+require "plum"
+require "json"
+require "cgi"
+require "simple_oauth"
+
+credentials = { consumer_key: "",
+ consumer_secret: "",
+ token: "",
+ token_secret: "" }
+
+rest = Plum::Client.start("api.twitter.com", 443)
+Plum::Client.start("userstream.twitter.com", 443) { |streaming|
+ streaming.get("/1.1/user.json",
+ "authorization" => SimpleOAuth::Header.new(:get, "https://userstream.twitter.com/1.1/user.json", {}, credentials).to_s,
+ "accept-encoding" => "identity;q=1") { |res| # plum doesn't have built-in gzip/deflate decoder
+ if res.status != 200
+ puts "failed userstream"
+ exit
+ end
+
+ buf = String.new
+ res.on_chunk { |chunk| # when received DATA frame
+ buf << chunk
+ *msgs, buf = buf.split("\r\n", -1)
+
+ msgs.each do |msg|
+ next if msg.empty?
+
+ json = JSON.parse(msg)
+ next unless json["user"] # unless it is a tweet
+
+ puts "@#{json["user"]["screen_name"]}: #{json["text"]}"
+
+ if /にゃーん/ =~ json["text"]
+ args = { "status" => "@#{json["user"]["screen_name"]} にゃーん",
+ "in_reply_to_status_id" => json["id"].to_s }
+ rest.post("/1.1/statuses/update.json",
+ args.map { |k, v| "#{k}=#{CGI.escape(v)}" }.join("&"),
+ "authorization" => SimpleOAuth::Header.new(:post, "https://api.twitter.com/1.1/statuses/update.json", args, credentials).to_s,
+ "content-type" => "application/x-www-form-urlencoded")
+ end
+ end
+ }
+ }
+}
+rest.close