Skip to content

Commit 303b072

Browse files
committed
Finish updating documentation.
1 parent ef2714a commit 303b072

File tree

1 file changed

+82
-106
lines changed

1 file changed

+82
-106
lines changed

README.md

Lines changed: 82 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -58,23 +58,24 @@ gem "github_api"
5858
* [1.4.1 Response Body](#141-response-body)
5959
* [1.4.2 Response Headers](#142-response-headers)
6060
* [1.4.3 Response Success](#143-response-success)
61+
* [1.5 Request Headers](#15-request-headers)
62+
* [1.5.1 Media Types](#151-media-types)
6163
* [2. Configuration](#2-configuration)
6264
* [2.1 Basic](#21-basic)
6365
* [2.2 Advanced](#22-advanced)
6466
* [2.3 SSL](#23-ssl)
67+
* [2.4 Caching](#24-caching)
6568
* [3. Authentication](#3-authentication)
6669
* [3.1 Basic](#31-basic)
6770
* [3.2 Application OAuth](#32-application-oauth)
6871
* [3.3 Authorizations API](#33-authorizations-api)
69-
* [7. Media Types](#7-media-types)
70-
* [8. Hypermeida](#8-hypermedia)
71-
* [10. Pagination](#10-pagination)
72-
* [11. Caching](#11-caching)
73-
* [12. Debugging](#12-debugging)
74-
* [13. Error Handling](#13-error-handling)
75-
* [14. Response Message](#14-response-message)
76-
* [15. Examples](#15-examples)
77-
* [16. Testing](#16-testing)
72+
* [3.4 Scopes](#34-scopes)
73+
* [4. Pagination](#4-pagination)
74+
* [4.1 Auto pagination](#41-auto-pagination)
75+
* [5. Error Handling](#5-error-handling)
76+
* [6. Examples](#6-examples)
77+
* [6.1 Rails](#61-rails)
78+
* [7. Testing](#7-testing)
7879

7980
## 1 Usage
8081

@@ -241,6 +242,31 @@ response = Github::Client::Repos.branches 'peter-murach', 'github'
241242
response.success? # => true
242243
```
243244

245+
### 1.5 Request Headers
246+
247+
It is possible to specify additional header information which will be added to the final request.
248+
249+
For example, to set `etag` and `X-Poll_Interval` headers, use the `:headers` hash key inside the `:options` hash like in the following:
250+
251+
```ruby
252+
events = Github::Client::Activity::Events.new
253+
events.public options: {
254+
headers: {
255+
'X-Poll-Interval': 60,
256+
'ETag': "a18c3bded88eb5dbb5c849a489412bf3"
257+
}
258+
}
259+
```
260+
261+
#### 1.5.1 Media Types
262+
263+
In order to set custom media types for a request use the accept header. By using the `:accept` key you can determine media type like in the example:
264+
265+
```ruby
266+
issues = Github::Client::Issues.new
267+
issues.get 'peter-murach', 'github', 108, accept: 'application/vnd.github.raw'
268+
```
269+
244270
## 2 Configuration
245271

246272
The **github_api** provides ability to specify global configruation options. These options will be available to all api calls.
@@ -340,6 +366,28 @@ ssl: {
340366

341367
For instance, download CA root certificates from Mozilla [cacert](http://curl.haxx.se/ca/cacert.pem) and point ca_file at your certificate bundle location. This will allow the client to verify the github.com ssl certificate as authentic.
342368

369+
### 2.4 Caching
370+
371+
Caching is supported through the [`faraday-http-cache` gem](https://github.com/plataformatec/faraday-http-cache).
372+
373+
Add the gem to your Gemfile:
374+
375+
```ruby
376+
gem 'faraday-http-cache'
377+
```
378+
379+
You can now configure cache parameters as follows
380+
381+
```ruby
382+
Github.configure do |config|
383+
config.stack do |builder|
384+
builder.use Faraday::HttpCache, store: Rails.cache
385+
end
386+
end
387+
```
388+
389+
More details on the available options can be found in the gem's own documentation: https://github.com/plataformatec/faraday-http-cache#faraday-http-cache
390+
343391
## 3 Authentication
344392

345393
### 3.1 Basic
@@ -432,81 +480,44 @@ response.headers.accepted_oauth_scopes # => ['delete_repo', 'repo', 'public_rep
432480

433481
To understand what each scope means refer to [documentation](http://developer.github.com/v3/oauth/#scopes)
434482

435-
## 7 Media Types
436-
437-
You can specify custom media types to choose the format of the data you wish to receive. To make things easier you can specify the following shortcuts
438-
`json`, `blob`, `raw`, `text`, `html`, `full`. For instance:
439-
440-
```ruby
441-
github = Github.new
442-
github.issues.get 'peter-murach', 'github', 108, media: 'text'
443-
```
444-
445-
This will be expanded into `application/vnd.github.v3.text+json`
446-
447-
If you wish to specify the version, pass `media: 'beta.text'` which will be converted to `application/vnd/github.beta.text+json`.
448-
449-
Finally, you can always pass the whole accept header like so
450-
451-
```ruby
452-
github.issues.get 'peter-murach', 'github', 108, accept: 'application/vnd.github.raw'
453-
```
454-
455-
## 8 Hypermedia
456483

457-
TODO
458-
459-
## 10 Pagination
484+
## 4 Pagination
460485

461486
Any request that returns multiple items will be paginated to 30 items by default. You can specify custom `page` and `per_page` query parameters to alter default behavior. For instance:
462487

463488
```ruby
464-
repos = Github::Repos.new
465-
repos.list user: 'wycats', per_page: 10, page: 5
489+
repos = Github::Client::Repos.new
490+
response = repos.list user: 'wycats', per_page: 10, page: 5
466491
```
467492

468493
Then you can query the pagination information included in the link header by:
469494

470495
```ruby
471-
res.links.first # Shows the URL of the first page of results.
472-
res.links.next # Shows the URL of the immediate next page of results.
473-
res.links.prev # Shows the URL of the immediate previous page of results.
474-
res.links.last # Shows the URL of the last page of results.
496+
response.links.first # Shows the URL of the first page of results.
497+
response.links.next # Shows the URL of the immediate next page of results.
498+
response.links.prev # Shows the URL of the immediate previous page of results.
499+
response.links.last # Shows the URL of the last page of results.
475500
```
476501

477502
In order to iterate through the entire result set page by page, you can use convenience methods:
478503

479504
```ruby
480-
res.each_page do |page|
505+
response.each_page do |page|
481506
page.each do |repo|
482507
puts repo.name
483508
end
484509
end
485510
```
486511

487-
or use `has_next_page?` and `next_page` like in the following:
512+
or use `has_next_page?` and `next_page` helper methods like in the following:
488513

489514
```ruby
490-
while res.has_next_page?
515+
while response.has_next_page?
491516
... process response ...
492517
res.next_page
493518
end
494519
```
495520

496-
Alternatively, you can retrieve all pages in one invocation by passing the `auto_pagination` option like so:
497-
498-
```ruby
499-
github = Github.new auto_pagination: true
500-
```
501-
502-
Depending at what stage you pass the `auto_pagination` it will affect all or only a single request:
503-
504-
```ruby
505-
Github::Repos.new auto_pagination: true # affects Repos part of API
506-
507-
Github::Repos.new.list user: '...', auto_pagination: true # affects a single request
508-
```
509-
510521
One can also navigate straight to the specific page by:
511522

512523
```ruby
@@ -518,31 +529,27 @@ res.prev_page # Get previous page
518529
res.last_page # Get last page
519530
```
520531

521-
## 11 Caching
522-
523-
Caching is supported through the [`faraday-http-cache` gem](https://github.com/plataformatec/faraday-http-cache).
532+
### 4.1 Auto pagination
524533

525-
Add the gem to your Gemfile:
534+
You can retrieve all pages in one invocation by passing the `auto_pagination` option like so:
526535

527-
gem 'faraday-http-cache'
536+
```ruby
537+
github = Github.new auto_pagination: true
538+
```
528539

529-
You can now configure cache parameters as follows
540+
Depending at what stage you pass the `auto_pagination` it will affect all or only a single request. For example, in order to auto paginate all Repository API methods do:
530541

531542
```ruby
532-
Github.configure do |config|
533-
config.stack do |builder|
534-
builder.use Faraday::HttpCache, store: Rails.cache
535-
end
536-
end
543+
Github::Repos.new auto_pagination: true
537544
```
538545

539-
More details on the available options can be found in the gem's own documentation: https://github.com/plataformatec/faraday-http-cache#faraday-http-cache
540-
541-
## 12 Debugging
546+
However, to only auto paginate results for a single request do:
542547

543-
run with ENV['DEBUG'] flag or include middleware by passing `debug` flag
548+
```ruby
549+
Github::Repos.new.list user: '...', auto_pagination: true
550+
```
544551

545-
## 13 Error Handling
552+
## 5 Error Handling
546553

547554
The generic error class `Github::Error::GithubError` will handle both the client (`Github::Error::ClientError`) and service (`Github::Error::ServiceError`) side errors. For instance in your code you can catch errors like
548555

@@ -560,40 +567,9 @@ rescue Github::Error::GithubError => e
560567
end
561568
```
562569

570+
## 6 Examples
563571

564-
## 15 Examples
565-
566-
Some API methods require input parameters. These are simply added as a hash of properties, for instance
567-
568-
```ruby
569-
issues = Github::Issues.new user:'peter-murach', repo: 'github-api'
570-
issues.milestones.list state: 'open', sort: 'due_date', direction: 'asc'
571-
```
572-
573-
Other methods may require inputs as an array of strings
574-
575-
```ruby
576-
users = Github::Users.new oauth_token: 'token'
577-
users.emails.add 'email1', 'email2', ..., 'emailn' # => Adds emails to the authenticated user
578-
```
579-
580-
If a method returns a collection, you can iterate over it by supplying a block parameter,
581-
582-
```ruby
583-
events = Github::Activity::Events.new
584-
events.public do |event|
585-
puts event.actor.login
586-
end
587-
```
588-
589-
Query requests return boolean values instead of HTTP responses
590-
591-
```ruby
592-
github = Github.new
593-
github.orgs.members.member? 'github', 'technoweenie', public: true # => true
594-
```
595-
596-
### 15.1 Rails Example
572+
### 6.1 Rails
597573

598574
A Rails controller that allows a user to authorize their GitHub account and then performs a request.
599575

@@ -617,7 +593,7 @@ class GithubController < ApplicationController
617593
end
618594
```
619595

620-
## 16 Testing
596+
## 7 Testing
621597

622598
The test suite is split into two groups, `live` and `mock`.
623599

0 commit comments

Comments
 (0)