Skip to content

Instantly share code, notes, and snippets.

@beccasaurus
Last active April 25, 2017 21:28
Show Gist options
  • Save beccasaurus/618d6b3bad1d870ed9d40e73b0b0edff to your computer and use it in GitHub Desktop.
Save beccasaurus/618d6b3bad1d870ed9d40e73b0b0edff to your computer and use it in GitHub Desktop.
RÉMI <3 IDIOMS :: TOP 10 LIST

rémi's most favoritest idiomatic bits from Google Cloud Client Libraries

#1 Make an entity

task = datastore.entity "Task" do |t|
  t["type"] = "Personal"
  t["done"] = false
  t["priority"] = 4
  t["description"] = "Learn Cloud Datastore"
end

#2 Where and what now?

datastore.query("Task").where("done", "=", false).order("created").limit(5)

#3 ALIAS ALL TEH THINGS

bucket.new_file "cat.png"
bucket.create_file "cat.png"
bucket.upload_file "cat.png"

#4 Storage IO upload/download (coming soon)

(pending release... merged into master...)

bucket.create_file StringIO.new("Haha!  I'm not a file!")

text = StringIO.new
bucket.file("README").download text

#5 wait_until_done!

job = bigquery.query_job "SELECT COUNT(word) as count FROM " \
                         "publicdata.samples.shakespeare"

job.wait_until_done!

if job.failed?
  puts job.error
else
  puts job.query_results.first
end

#6 Pub/Sub listen

subscription = pubsub.subscription "cool-things"

subscription.listen do |message|
  puts "Hey! So there I was, just sitting here blocking..."
  puts "When I heard this cool thing! #{message.data}"
end

#7 DNS DSL

Don't know Ruby? That's okay. I dare you not to easily comprehend this...

require "google/cloud/dns"

dns = Google::Cloud::Dns.new
zone = dns.zone "example-com"
change = zone.update do |tx|
  tx.add     "www", "A",  86400, "1.2.3.4"
  tx.remove  "example.com.", "TXT"
  tx.replace "example.com.", "MX", 86400, ["10 mail1.example.com.",
                                           "20 mail2.example.com."]
  tx.modify "www.example.com.", "CNAME" do |r|
    r.ttl = 86400 # only change the TTL
  end
end

#8 Just a regular ol' logger, nothing special about me ...

logger = logging.logger "my_app_log", resource, env: :production

logger.info "Job started"
logger.info { "Job started" }
logger.debug?

#9 create_table

Huh. That looks familiar for some reason...

dataset.create_table "people" do |schema|
  schema.string "name"
  schema.integer "age"
end

Number 10!

And rémi's top most favoritest Google Cloud Client Library idiom is ...

🥁 🥁 🥁 🥁 🥁 🥁 🥁 🥁 🥁

#10 Stackdriver!

gem "stackdriver"

Railtie that just works

Using Sinatra or Rack? That's okay!

Just use the middleware!

use Google::Cloud::Logging::Middleware
use Google::Cloud::ErrorReporting::Middleware
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment