sheetsu-ruby
Ruby bindings for the Sheetsu API (https://sheetsu.com/docs).
Installation
Add this line to your application's Gemfile:
gem 'sheetsu-ruby'
And then execute:
$ bundle
Or install it yourself as:
$ gem install sheetsu-ruby
Usage
Generating a Client
You need to create a new Sheetsu::Client object, and populate it with your Sheetsu API URL. You can find this URL on Sheetsu Dashboard.
client = Sheetsu::Client.new("https://sheetsu.com/apis/v1.0/020b2c0f")
or
client = Sheetsu::Client.new("020b2c0f")
If you have HTTP Basic Authentication turned on for your API, you should pass api_key
and api_secret
here, like:
client = Sheetsu::Client.new("020b2c0f", api_key: "YOUR_API_KEY", api_secret: "YOUR_API_SECRET")
CRUD
Sheetsu gives you the ability to use full CRUD on your Google Spreadsheet. Remember to populate the first row of every sheet with column names. You can look at example spreadsheet.
Create
Link to docs
To add data to Google Spreadsheets, send a hash or an array of hashes.
client.create({ id: 7, name: "Glenn", score: "69" })
rows = [
{ id: 7, name: "Glenn", score: "69" },
{ id: 8, name: "Brian", score: "77" },
{ id: 9, name: "Joe", score: "45" }
]
client.create(rows)
By default, all writes are performed on the first sheet (worksheet). Pass name of a sheet as a 2nd param to add data to other worksheet.
client.create({ "foo" => "bar", "baz" => "quux" }, "Sheet3")
On success returns a hash or an array of hashes with created values. On error check errors.
Read
Link to docs
Read the whole sheet
client.read
You can pass hash with options
sheet
- get data from named worksheetlimit
- limit number of resultsoffset
- start from N first recordsearch
- hash with search params (more below)
client.read(sheet: "Sheet2", limit: 2)
client.read(
offset: 4,
limit: 1,
sheet: "Sheet3"
)
search
Link to docs
To get rows that match search criteria, pass a hash with search params
client.read(search: { id: "foo", value: "bar" })
client.read(search: { "First name" => "Peter", "Score" => 42 })
client.read(
search: { "First name" => "Peter", "Score" => 42 }
limit: 2
sheet: "Sheet3"
)
On success returns an array of hashes. On error check errors.
Update
Link to docs
To update row(s), pass column name and its value which is used to find row(s).
client.update(
"name",
"Peter",
{ "score": 99, "last name" => "Griffin" },
)
By default, PATCH request is sent, which is updating only values which are in the hash passed to the method. To send PUT request, pass 4th argument being true
. Read more about the difference between PUT and PATCH in our docs.
client.update(
"name",
"Peter",
{ "score": 99, "last name" => "Griffin" },
true
)
To perform #update
on different than the first sheet, pass sheet name as a 5th argument.
client.update(
"name",
"Peter",
{ "score": 99, "last name" => "Griffin" },
true,
"Sheet3"
)
On success returns an array of hashes with updated values. On error check errors.
Delete
Link to docs
To delete row(s), pass column name and its value which is used to find row(s).
client.delete(
"name",
"Peter"
)
You can pass sheet name as a 3rd argument. All operations are performed on the first sheet, by default.
client.delete(
"foo",
"bar",
"Sheet3"
)
If success returns :ok
symbol. If error check errors.
Errors
There are different styles of error handling. We choose to throw exceptions and signal failure loudly. You do not need to deal with any HTTP responses from the API calls directly. All exceptions are matching particular response code from Sheetsu API. You can read more about it here.
All exceptions are a subclass of Sheetsu::SheetsuError
. The list of different error subclasses is listed below. You can choose to rescue each of them or rescue just the parent class (Sheetsu::SheetsuError
).
Sheetsu::NotFoundError
Sheetsu::ForbiddenError
Sheetsu::LimitExceedError
Sheetsu::UnauthorizedError
Development
After checking out the repo, run bin/setup
to install dependencies. You can also run bin/console
for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install
. To release a new version, update the version number in version.rb
, and then run bundle exec rake release
, which will create a git tag for the version, push git commits and tags, and push the .gem
file to rubygems.org.
Run all tests:
rspec
Run a single test:
rspec spec/read_spec.rb
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/sheetsu/sheetsu-ruby. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
Pull Requests
-
Add tests! Your patch won't be accepted if it doesn't have tests.
-
Create topic branches. Please, always create a branch with meaningful name. Don't ask us to pull from your master branch.
-
One pull request per feature. If you want to do more than one thing, please send
multiple pull requests.
-
Send coherent history. Make sure each individual commit in your pull
request is meaningful. If you had to make multiple intermediate commits while
developing, please squash them before sending them to us.
Docs
Sheetsu documentation sits on GitHub. We would love your contributions! We want to make these docs accessible and easy to understand for everyone. Please send us Pull Requests or open issues on GitHub.
To do
- Allow passing whole API URLs as well as just slugs to the client, like:
client = Sheetsu::Client.new("https://sheetsu.com/apis/v1.0/020b2c0f")
client = Sheetsu::Client.new("020b2c0f")