Created
March 24, 2015 09:49
-
-
Save nagachika/57368b3aa08c5553cc0d to your computer and use it in GitHub Desktop.
workaround for Google Cloud Pub/Sub via google-api-client.gem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
source "https://rubygems.org" | |
gem "google-api-client", "0.8.3" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
GEM | |
remote: https://rubygems.org/ | |
specs: | |
activesupport (4.2.1) | |
i18n (~> 0.7) | |
json (~> 1.7, >= 1.7.7) | |
minitest (~> 5.1) | |
thread_safe (~> 0.3, >= 0.3.4) | |
tzinfo (~> 1.1) | |
addressable (2.3.7) | |
autoparse (0.3.3) | |
addressable (>= 2.3.1) | |
extlib (>= 0.9.15) | |
multi_json (>= 1.0.0) | |
extlib (0.9.16) | |
faraday (0.9.1) | |
multipart-post (>= 1.2, < 3) | |
google-api-client (0.8.2) | |
activesupport (>= 3.2) | |
addressable (~> 2.3) | |
autoparse (~> 0.3) | |
extlib (~> 0.9) | |
faraday (~> 0.9) | |
launchy (~> 2.4) | |
multi_json (~> 1.10) | |
retriable (~> 1.4) | |
signet (~> 0.6) | |
i18n (0.7.0) | |
json (1.8.2) | |
jwt (1.4.1) | |
launchy (2.4.3) | |
addressable (~> 2.3) | |
minitest (5.5.1) | |
multi_json (1.11.0) | |
multipart-post (2.0.0) | |
retriable (1.4.1) | |
signet (0.6.0) | |
addressable (~> 2.3) | |
extlib (~> 0.9) | |
faraday (~> 0.9) | |
jwt (~> 1.0) | |
multi_json (~> 1.10) | |
thread_safe (0.3.5) | |
tzinfo (1.2.2) | |
thread_safe (~> 0.1) | |
PLATFORMS | |
ruby | |
DEPENDENCIES | |
google-api-client |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# google-api-client.gem seems have some known issues which could encountered while using Cloud Pub/Sub | |
require "base64" | |
require "google/api_client" | |
auth_params = { | |
token_credential_uri: "https://accounts.google.com/o/oauth2/token", | |
audience: "https://accounts.google.com/o/oauth2/token", | |
scope: "https://www.googleapis.com/auth/pubsub", | |
issuer: ENV["SERVICE_ACCOUNT_EMAIL"], | |
signing_key: Google::APIClient::PKCS12.load_key(Base64.decode64(ENV["SERVICE_ACCOUNT_P12_KEY_BASE64"]), "notasecret") | |
} | |
client = Google::APIClient.new(application_name: "pubsubtest", application_version: "v1", | |
authorization: Signet::OAuth2::Client.new(auth_params)) | |
client.authorization.fetch_access_token! | |
pubsub_api = client.discovered_api("pubsub", "v1beta2") | |
# workaround for an issue https://github.com/google/google-api-ruby-client/issues/195 | |
pubsub_api.method_base = "https://pubsub.googleapis.com/v1beta2/" | |
# workaround for an issue publish discovery_docuent["path"] contains colon and Addressable::URI doesn't treat it as path | |
pubsub_api.projects.topics.publish.discovery_document["path"] = "/v1beta2/{+topic}:publish" | |
r = client.execute(api_method: pubsub_api.projects.topics.list, parameters: { project: "projects/#{ENV["PROJECT"]}" }) | |
if r.success? | |
puts "Topic List:" | |
p r.data.topics.map(&:name) | |
else | |
p r.data.error | |
raise "fail to get topic list" | |
end | |
puts "enter messages:" | |
while l = $stdin.gets | |
r = client.execute(api_method: pubsub_api.projects.topics.publish, parameters: { topic: "projects/scenic-doodad-617/topics/mytopic" }, | |
body_object: { messages: [{ data: Base64.strict_encode64(l.chomp) }] }) | |
if r.error? | |
p r.data.error | |
raise "fail to publish" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment