Skip to content

Commit 3e13d39

Browse files
committed
Update docs to explain file manipulations.
1 parent 303b072 commit 3e13d39

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

README.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ gem "github_api"
7575
* [5. Error Handling](#5-error-handling)
7676
* [6. Examples](#6-examples)
7777
* [6.1 Rails](#61-rails)
78+
* [6.2 Manipulating Files](#62-manipulating-files)
7879
* [7. Testing](#7-testing)
7980

8081
## 1 Usage
@@ -593,6 +594,56 @@ class GithubController < ApplicationController
593594
end
594595
```
595596

597+
### 6.2 Manipulating Files
598+
599+
In order to be able to create/update/remove files you need to use Contents API like so:
600+
601+
```ruby
602+
contents = Github::Client::Repos::Contents.new oauth_token: '...'
603+
```
604+
605+
Having instantiaed the contents, to create a file do:
606+
607+
```ruby
608+
contents.create 'username', 'repo_name', 'full_path_to/file.ext',
609+
path: 'full_path_to/file.ext',
610+
message: 'Your commit message',
611+
content: 'The contents of your file'
612+
```
613+
614+
Content is all Base64 encoded to/from the API, and when you create a file it encodes it automatically for you.
615+
616+
To update a file, first you need to find the file so you can get the SHA you're updating off of:
617+
618+
```ruby
619+
file = contents.find path: 'full_path_to/file.ext'
620+
```
621+
622+
Then update the file just like you do with creating:
623+
624+
```ruby
625+
contents.update 'username', 'repo_name', 'full_path_to/file.ext',
626+
path: 'full_path_to/file.ext'
627+
message: 'Your commit message',
628+
content: 'The contens to be updated',
629+
sha: file.sha
630+
```
631+
632+
Finally to remove a file, find the file so you can get the SHA you're removing:
633+
634+
```ruby
635+
file = contents.find path: 'full_path_to/file.ext'
636+
```
637+
638+
Then delete the file like so:
639+
640+
```ruby
641+
github.delete 'username', 'tome-of-knowledge', 'full_path_to/file.ext',
642+
path: 'full_path_to/file.ext',
643+
message: 'Your Commit Message',
644+
sha: file.sha
645+
```
646+
596647
## 7 Testing
597648

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

0 commit comments

Comments
 (0)