README.md in fmrest-0.1.0 vs README.md in fmrest-0.2.0
- old
+ new
@@ -1,7 +1,9 @@
# fmrest-ruby
+<a href="https://rubygems.org/gems/fmrest"><img src="https://badge.fury.io/rb/fmrest.svg?style=flat" alt="Gem Version"></a>
+
A Ruby client for
[FileMaker 17's Data API](https://fmhelp.filemaker.com/docs/17/en/dataapi/)
using
[Faraday](https://github.com/lostisland/faraday) and with optional
[Spyke](https://github.com/balvig/spyke) support (ActiveRecord-ish models).
@@ -18,10 +20,13 @@
Add this line to your Gemfile:
```ruby
gem 'fmrest'
+
+# Optional (for ORM features)
+gem 'spyke'
```
## Basic usage
To get a Faraday connection that can handle FM's Data API auth workflow:
@@ -61,28 +66,64 @@
By default fmrest-ruby will use a memory-based store for the session tokens.
This is generally good enough for development, but not good enough for
production, as in-memory tokens aren't shared across threads/processes.
-Besides the default memory token store an ActiveRecord-based token store is
-included with the gem (maybe more to come later).
+Besides the default token store the following token stores are bundled with fmrest-ruby:
+### ActiveRecord
+
On Rails apps already using ActiveRecord setting up this token store should be
dead simple:
```ruby
# config/initializers/fmrest.rb
-require "fmrest/v1/token_store/active_record"
+require "fmrest/token_store/active_record"
-FmRest.token_store = FmRest::V1::TokenStore::ActiveRecord
+FmRest.token_store = FmRest::TokenStore::ActiveRecord
```
No migrations are needed, the token store table will be created automatically
-when needed, defaulting to the table name "fmrest_session_tokens".
+when needed, defaulting to the table name "fmrest_session_tokens". If you want
+to change the table name you can do so by initializing the token store and
+passing it the `:table_name` option:
-## Spyke support
+```ruby
+FmRest.token_store = FmRest::TokenStore::ActiveRecord.new(table_name: "my_token_store")
+```
+### Redis
+
+To use the Redis token store do:
+
+```ruby
+require "fmrest/token_store/redis"
+
+FmRest.token_store = FmRest::TokenStore::Redis
+```
+
+You can also initialize it with the following options:
+
+* `:redis` - A `Redis` object to use as connection, if ommited a new `Redis` object will be created with remaining options
+* `:prefix` - The prefix to use for token keys, by default `"fmrest-token:"`
+* Any other options will be passed to `Redis.new` if `:redis` isn't provided
+
+Examples:
+
+```ruby
+# Passing a Redis connection explicitly
+FmRest.token_store = FmRest::TokenStore::Redis.new(redis: Redis.new, prefix: "my-fancy-prefix:")
+
+# Passing options for Redis.new
+FmRest.token_store = FmRest::TokenStore::Redis.new(prefix: "my-fancy-prefix:", host: "10.0.1.1", port: 6380, db: 15)
+```
+
+**NOTE:** redis-rb is not included as a gem dependency of fmrest-ruby, so you'll
+have to add it to your Gemfile.
+
+## Spyke support (ActiveRecord-like ORM)
+
[Spyke](https://github.com/balvig/spyke) is an ActiveRecord-like gem for
building REST models. fmrest-ruby has Spyke support out of the box, although
Spyke itself is not a dependency of fmrest-ruby, so you'll need to add it to
your Gemfile yourself:
@@ -346,22 +387,28 @@
has_portal :toys, portal_key: "CatToys"
end
```
+#### .limit
+
`.limit` sets the limit for get and find request:
```ruby
Kitty.limit(10)
```
+#### .offset
+
`.offset` sets the offset for get and find requests:
```ruby
Kitty.offset(10)
```
+#### .sort
+
`.sort` (or `.order`) sets sorting options for get and find requests:
```ruby
Kitty.sort(:name, :age)
Kitty.order(:name, :age) # alias method
@@ -373,18 +420,22 @@
```ruby
Kitty.sort(:name, :age!)
Kitty.sort(:name, :age__desc)
```
+#### .portal
+
`.portal` (or `.includes`) sets the portals to fetch for get and find requests
(this recognizes portals defined with `has_portal`):
```ruby
Kitty.portal(:toys)
Kitty.includes(:toys) # alias method
```
+#### .query
+
`.query` sets query conditions for a find request (and supports attributes as
defined with `attributes`):
```ruby
Kitty.query(name: "Mr. Fluffers")
@@ -405,10 +456,12 @@
Kitty.query(name: "Mr. Fluffers").query(name: "Coronel Chai Latte")
Kitty.query({ name: "Mr. Fluffers" }, { name: "Coronel Chai Latte" })
# JSON -> {"query": [{"CatName": "Mr. Fluffers"}, {"CatName": "Coronel Chai Latte"}]}
```
+#### .omit
+
`.omit` works like `.query` but excludes matches:
```ruby
Kitty.omit(name: "Captain Whiskers")
# JSON -> {"query": [{"CatName": "Captain Whiskers", "omit": "true"}]}
@@ -419,10 +472,12 @@
```ruby
Kitty.query(name: "Captain Whiskers", omit: true)
# JSON -> {"query": [{"CatName": "Captain Whiskers", "omit": "true"}]}
```
+#### Other notes on querying
+
You can chain all query methods together:
```ruby
Kitty.limit(10).offset(20).sort(:name, :age!).portal(:toys).query(name: "Mr. Fluffers")
```
@@ -461,10 +516,47 @@
```ruby
Kitty.find(89) # => <Kitty...>
```
+### Container fields
+
+You can define container fields on your model class with `container`:
+
+```ruby
+class Kitty < FmRest::Spyke::Base
+ container :photo, field_name: "Vet Card Photo ID"
+end
+```
+
+`:field_name` specifies the original field in the FM layout and is optional, if
+not given it will default to the name of your attribute (just `:photo` in this
+example).
+
+(Note that you don't need to define container fields with `attributes` in
+addition to the `container` definition.)
+
+This will provide you with the following instance methods:
+
+```ruby
+kitty = Kitty.new
+
+kitty.photo.url # The URL of the container file on the FileMaker server
+
+kitty.photo.download # Download the contents of the container as an IO object
+
+kitty.photo.upload(filename_or_io) # Upload a file to the container
+```
+
+`upload` also accepts an options hash with the following options:
+
+* `:repetition` - Sets the field repetition
+* `:filename` - The filename to use when uploading (defaults to
+ `filename_or_io.original_filename` if available)
+* `:content_type` - The MIME content type to use (defaults to
+ `application/octet-stream`)
+
## Logging
If using fmrest-ruby + Spyke in a Rails app pretty log output will be set up
for you automatically by Spyke (see [their
README](https://github.com/balvig/spyke#log-output)).
@@ -508,14 +600,16 @@
end
```
## TODO
+- [ ] Support for FM18 features
- [ ] Better/simpler-to-use core Ruby API
- [ ] Better API documentation and README
- [ ] Oauth support
- [ ] Support for portal limit and offset
-- [ ] More options for token storage
+- [x] More options for token storage
+- [x] Support for container fields
- [x] Optional logging
- [x] FmRest::Spyke::Base class for single inheritance (as alternative for mixin)
- [x] Specs
- [x] Support for portal data