Add syntax highlighting to README

This commit is contained in:
Dillon Kearns 2013-04-28 19:29:39 -07:00
parent cc69b49b0e
commit ed0a8921c0
1 changed files with 97 additions and 79 deletions

View File

@ -21,10 +21,13 @@ Be sure `http://rubygems.org/` is in your gem sources.
For normal client usage, this is sufficient:
```bash
$ gem install google-api-client
```
## Example Usage
```ruby
# Initialize the client & Google+ API
require 'google/api_client'
client = Google::APIClient.new
@ -51,6 +54,7 @@ For normal client usage, this is sufficient:
)
puts result.data
```
## API Features
@ -58,16 +62,22 @@ For normal client usage, this is sufficient:
To take full advantage of the client, load API definitions prior to use. To load an API:
```ruby
urlshortener = client.discovered_api('urlshortener')
```
Specific versions of the API can be loaded as well:
```ruby
drive = client.discovered_api('drive', 'v2')
```
Locally cached discovery documents may be used as well. To load an API from a local file:
```ruby
doc = File.read('my-api.json')
my_api = client.register_discovery_document('myapi', 'v1', doc)
```
### Authorization
@ -77,6 +87,7 @@ Credentials can be managed at the connection level, as shown, or supplied on a p
For server-to-server interactions, like those between a web application and Google Cloud Storage, Prediction, or BigQuery APIs, use service accounts.
```ruby
key = Google::APIClient::KeyUtils.load_from_pkcs12('client.p12', 'notasecret')
client.authorization = Signet::OAuth2::Client.new(
:token_credential_uri => 'https://accounts.google.com/o/oauth2/token',
@ -86,7 +97,7 @@ For server-to-server interactions, like those between a web application and Goog
:signing_key => key)
client.authorization.fetch_access_token!
client.execute(...)
```
### Batching Requests
@ -95,6 +106,7 @@ to bundle multiple requests together.
Example:
```ruby
client = Google::APIClient.new
urlshortener = client.discovered_api('urlshortner')
@ -107,18 +119,22 @@ Example:
batch.add(:api_method => urlshortener.url.insert,
:body_object => { 'longUrl' => 'http://example.com/bar' })
client.execute(batch)
```
Blocks for handling responses can be specified either at the batch level or when adding an individual API call. For example:
```ruby
batch.add(:api_method=>urlshortener.url.insert, :body_object => { 'longUrl' => 'http://example.com/bar' }) do |result|
puts result.data
end
```
### Media Upload
For APIs that support file uploads, use `Google::APIClient::UploadIO` to load the stream. Both multipart and resumable
uploads can be used. For example, to upload a file to Google Drive using multipart
```ruby
drive = client.discovered_api('drive', 'v2')
media = Google::APIClient::UploadIO.new('mymovie.m4v', 'video/mp4')
@ -130,10 +146,12 @@ uploads can be used. For example, to upload a file to Google Drive using multipa
:parameters => { 'uploadType' => 'multipart' },
:body_object => metadata,
:media => media )
```
To use resumable uploads, change the `uploadType` parameter to `resumable`. To check the status of the upload
and continue if necessary, check `result.resumable_upload`.
```ruby
client.execute(:api_method => drive.files.insert,
:parameters => { 'uploadType' => 'resumable' },
:body_object => metadata,
@ -144,11 +162,13 @@ and continue if necessary, check `result.resumable_upload`.
if upload.resumable?
client.execute(upload)
end
```
## Command Line
Included with the gem is a command line interface for working with Google APIs.
```bash
# Log in
google-api oauth-2-login --client-id='...' --client-secret='...' --scope="https://www.googleapis.com/auth/plus.me"
@ -160,6 +180,7 @@ Included with the gem is a command line interface for working with Google APIs.
>> plus = $client.discovered_api('plus')
>> $client.execute(plus.activities.list, {'userId' => 'me', 'collection' => 'public'})
=> # returns a response from the API
```
For more information, use `google-api --help`
@ -171,6 +192,3 @@ See the full list of [samples on Google Code](http://code.google.com/p/google-ap
## Support
Please [report bugs at the project on Google Code](http://code.google.com/p/google-api-ruby-client/issues/entry). Don't hesitate to [ask questions](http://stackoverflow.com/questions/tagged/google-api-ruby-client) about the client or APIs on [StackOverflow](http://stackoverflow.com).