Idiomatic Ruby client for Google Cloud Platform services.
This client supports the following Google Cloud Platform services at a Beta quality level:
- BigQuery (Beta)
- Cloud Datastore (Beta)
- Stackdriver Logging (Beta)
- Cloud Storage (Beta)
- Cloud Vision API (Beta)
This client supports the following Google Cloud Platform services at an Alpha quality level:
- Cloud DNS (Alpha)
- Cloud Natural Language API (Alpha)
- Cloud Pub/Sub (Alpha)
- Cloud Resource Manager (Alpha)
- Cloud Speech API (Alpha)
- Cloud Translation API (Alpha)
The support for each service is distributed as a separate gem. However, for your convenience, the google-cloud
gem lets you install the entire collection.
If you need support for other Google APIs, check out the Google API Ruby Client library.
$ gem install google-cloud
The google-cloud
gem shown above provides all of the individual service gems in the google-cloud-ruby project, making it easy to explore Google Cloud Platform. To avoid unnecessary dependencies, you can also install the service gems independently.
In general, the google-cloud-ruby library uses Service Account credentials to connect to Google Cloud services. When running on Compute Engine the credentials will be discovered automatically. When running on other environments, the Service Account credentials can be specified by providing the path to the JSON keyfile for the account (or the JSON itself) in environment variables. Additionally, Cloud SDK credentials can also be discovered automatically, but this is only recommended during development.
General instructions, environment variables, and configuration options are covered in the general Authentication guide for the google-cloud
umbrella package. Specific instructions and environment variables for each individual service are linked from the README documents listed below for each service.
The preview examples below demonstrate how to provide the Project ID and Credentials JSON file path directly in code.
- google-cloud-bigquery README
- google-cloud-bigquery API documentation
- google-cloud-bigquery on RubyGems
- Google BigQuery documentation
$ gem install google-cloud-bigquery
require "google/cloud/bigquery"
bigquery = Google::Cloud::Bigquery.new(
project: "my-todo-project",
keyfile: "/path/to/keyfile.json"
)
# Create a new table to archive todos
dataset = bigquery.dataset "my-todo-archive"
table = dataset.create_table "todos",
name: "Todos Archive",
description: "Archive for completed TODO records"
# Load data into the table
file = File.open "/archive/todos/completed-todos.csv"
load_job = table.load file
# Run a query for the number of completed todos by owner
count_sql = "SELECT owner, COUNT(*) AS complete_count FROM todos GROUP BY owner"
data = bigquery.query count_sql
data.each do |row|
puts row["name"]
end
- google-cloud-datastore README
- google-cloud-datastore API documentation
- google-cloud-datastore on RubyGems
- Google Cloud Datastore documentation
Follow the activation instructions to use the Google Cloud Datastore API with your project.
$ gem install google-cloud-datastore
require "google/cloud/datastore"
datastore = Google::Cloud::Datastore.new(
project: "my-todo-project",
keyfile: "/path/to/keyfile.json"
)
# Create a new task to demo datastore
task = datastore.entity "Task", "sampleTask" do |t|
t["type"] = "Personal"
t["done"] = false
t["priority"] = 4
t["description"] = "Learn Cloud Datastore"
end
# Save the new task
datastore.save task
# Run a query for all completed tasks
query = datastore.query("Task").
where("done", "=", false)
tasks = datastore.run query
- google-cloud-dns README
- google-cloud-dns API documentation
- google-cloud-dns on RubyGems
- Google Cloud DNS documentation
$ gem install google-cloud-dns
require "google/cloud/dns"
dns = Google::Cloud::Dns.new
# Retrieve a zone
zone = dns.zone "example-com"
# Update records in the zone
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
- google-cloud-logging README
- google-cloud-logging API documentation
- google-cloud-logging on RubyGems
- Stackdriver Logging documentation
$ gem install google-cloud-logging
require "google/cloud/logging"
logging = Google::Cloud::Logging.new
# List all log entries
logging.entries.each do |e|
puts "[#{e.timestamp}] #{e.log_name} #{e.payload.inspect}"
end
# List only entries from a single log
entries = logging.entries filter: "log:syslog"
# Write a log entry
entry = logging.entry
entry.payload = "Job started."
entry.log_name = "my_app_log"
entry.resource.type = "gae_app"
entry.resource.labels[:module_id] = "1"
entry.resource.labels[:version_id] = "20150925t173233"
logging.write_entries entry
- google-cloud-language README
- google-cloud-language API documentation
- google-cloud-language on RubyGems
- Google Cloud Natural Language API documentation
$ gem install google-cloud-language
require "google/cloud/language"
language = Google::Cloud::Language.new
content = "Star Wars is a great movie. The Death Star is fearsome."
document = language.document content
annotation = document.annotate
annotation.entities.count #=> 3
annotation.sentiment.score #=> 0.10000000149011612
annotation.sentiment.magnitude #=> 1.100000023841858
annotation.sentences.count #=> 2
annotation.tokens.count #=> 13
- google-cloud-pubsub README
- google-cloud-pubsub API documentation
- google-cloud-pubsub on RubyGems
- Google Cloud Pub/Sub documentation
$ gem install google-cloud-pubsub
require "google/cloud/pubsub"
pubsub = Google::Cloud::Pubsub.new(
project: "my-todo-project",
keyfile: "/path/to/keyfile.json"
)
# Retrieve a topic
topic = pubsub.topic "my-topic"
# Publish a new message
msg = topic.publish "new-message"
# Retrieve a subscription
sub = pubsub.subscription "my-topic-sub"
# Pull available messages
msgs = sub.pull
- google-cloud-resource_manager README
- google-cloud-resource_manager API documentation
- google-cloud-resource_manager on RubyGems
- Google Cloud Resource Manager documentation
$ gem install google-cloud-resource_manager
require "google/cloud/resource_manager"
resource_manager = Google::Cloud::ResourceManager.new
# List all projects
resource_manager.projects.each do |project|
puts projects.project_id
end
# Label a project as production
project = resource_manager.project "tokyo-rain-123"
project.update do |p|
p.labels["env"] = "production"
end
# List only projects with the "production" label
projects = resource_manager.projects filter: "labels.env:production"
- google-cloud-speech README
- google-cloud-speech API documentation
- google-cloud-speech on RubyGems
- Google Cloud Speech API documentation
$ gem install google-cloud-speech
require "google/cloud/speech"
speech = Google::Cloud::Speech.new
audio = speech.audio "path/to/audio.raw",
encoding: :raw, sample_rate: 16000
results = audio.recognize
result = results.first
result.transcript #=> "how old is the Brooklyn Bridge"
result.confidence #=> 0.9826789498329163
- google-cloud-storage README
- google-cloud-storage API documentation
- google-cloud-storage on RubyGems
- Google Cloud Storage documentation
$ gem install google-cloud-storage
require "google/cloud/storage"
storage = Google::Cloud::Storage.new(
project: "my-todo-project",
keyfile: "/path/to/keyfile.json"
)
bucket = storage.bucket "task-attachments"
file = bucket.file "path/to/my-file.ext"
# Download the file to the local file system
file.download "/tasks/attachments/#{file.name}"
# Copy the file to a backup bucket
backup = storage.bucket "task-attachment-backups"
file.copy backup, file.name
- google-cloud-translate README
- google-cloud-translate API documentation
- google-cloud-translate on RubyGems
- Google Cloud Translation API documentation
$ gem install google-cloud-translate
require "google/cloud/translate"
translate = Google::Cloud::Translate.new
translation = translate.translate "Hello world!", to: "la"
puts translation #=> Salve mundi!
translation.from #=> "en"
translation.origin #=> "Hello world!"
translation.to #=> "la"
translation.text #=> "Salve mundi!"
- google-cloud-vision README
- google-cloud-vision API documentation
- google-cloud-vision on RubyGems
- Google Cloud Vision API documentation
$ gem install google-cloud-vision
require "google/cloud/vision"
vision = Google::Cloud::Vision.new
image = vision.image "path/to/landmark.jpg"
landmark = image.landmark
landmark.description #=> "Mount Rushmore"
google-cloud-ruby is supported on Ruby 2.0+.
This library follows Semantic Versioning.
Please note it is currently under active development. Any release versioned 0.x.y is subject to backwards incompatible changes at any time.
Beta: Libraries defined at a Beta quality level are expected to be mostly stable and we're working towards their release candidate. We will address issues and requests with a higher priority.
Alpha: Libraries defined at an Alpha quality level are still a work-in-progress and are more likely to get backwards-incompatible updates.
Contributions to this library are always welcome and highly encouraged.
See CONTRIBUTING for more information on how to get started.
Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms. See Code of Conduct for more information.
This library is licensed under Apache 2.0. Full license text is available in LICENSE.
Please report bugs at the project on Github. Don't hesitate to ask questions about the client or APIs on StackOverflow.