Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
34 changed files
with
143 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
source 'https://rubygems.org' | ||
|
||
# Specify your gem's dependencies in spike.gemspec | ||
# Specify your gem's dependencies in spyke.gemspec | ||
gemspec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,84 @@ | ||
# Spike | ||
# Spyke | ||
|
||
TODO: Write a gem description | ||
Spyke allows you to interact with remote REST services in an ActiveRecord-like manner. | ||
|
||
## Installation | ||
It basically ~~rips off~~ takes inspiration from :innocent: [Her](https://github.com/remiprev/her), a gem which we sadly had to abandon as it showed significant performance problems and maintenance seemed to had gone stale. | ||
|
||
We therefore made Spyke which adds a few fixes/features that we needed for our projects: | ||
|
||
- Fast handling of even large amounts of JSON | ||
- Proper support for scopes | ||
- Ability to define custom URIs for associations | ||
- Googlable name! :) | ||
|
||
## Configuration | ||
|
||
Add this line to your application's Gemfile: | ||
|
||
gem 'spike' | ||
gem 'spyke' | ||
|
||
And then execute: | ||
Like Her, Spyke uses Faraday to handle requests and expects a hash in the following format: | ||
|
||
$ bundle | ||
```ruby | ||
{ result: { id: 1, name: 'Bob' }, metadata: {} } | ||
``` | ||
|
||
Or install it yourself as: | ||
The simplest possible configuration that can work is something like this: | ||
|
||
$ gem install spike | ||
```ruby | ||
# config/initializers/spyke.rb | ||
|
||
class JSONParser < Faraday::Response::Middleware | ||
def parse(body) | ||
json = MultiJson.load(body, symbolize_keys: true) | ||
{ | ||
data: json[:result], | ||
metadata: json[:metadata] | ||
} | ||
end | ||
end | ||
|
||
Spyke::Config.connection = Faraday.new(url: 'http://api.com') do |c| | ||
c.use JSONParser | ||
c.use Faraday::Adapter::NetHttp | ||
end | ||
``` | ||
|
||
## Usage | ||
|
||
TODO: Write usage instructions here | ||
Adding a class and inheriting from `Spyke::Base` will allow you to interact with the remote service: | ||
|
||
```ruby | ||
class User < Spyke::Base | ||
has_many :posts | ||
end | ||
|
||
user = User.find(3) # => GET http://api.com/users/3 | ||
user.posts # => find embedded in user or GET http://api.com/users/3/posts | ||
``` | ||
|
||
### Custom URIs | ||
|
||
You can specify custom URIs on both the class and association level: | ||
|
||
```ruby | ||
class User < Spyke::Base | ||
uri '/v1/users/:id' | ||
|
||
has_one :image, uri: nil | ||
has_many :posts, uri: '/posts/for_user/:user_id' | ||
end | ||
|
||
class Post < Spyke::Base | ||
end | ||
|
||
user = User.find(3) # => GET http://api.com/v1/users/3 | ||
user.image # Will only use embedded JSON and never call out to api | ||
user.posts # => GET http://api.com/posts/for_user/3 | ||
Post.find(4) # => GET http://api.com/posts/4 | ||
``` | ||
|
||
## Contributing | ||
|
||
1. Fork it ( https://github.com/[my-github-username]/spike/fork ) | ||
2. Create your feature branch (`git checkout -b my-new-feature`) | ||
3. Commit your changes (`git commit -am 'Add some feature'`) | ||
4. Push to the branch (`git push origin my-new-feature`) | ||
5. Create a new Pull Request | ||
If possible please take a look at the tests marked "wishlisted"! | ||
These are features/fixes we want to implement but haven't gotten around to doing yet :) |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
require 'active_support/core_ext' | ||
require 'spyke/base' | ||
require 'spyke/version' | ||
|
||
module Spyke | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
lib/spike/associations/association.rb → lib/spyke/associations/association.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
lib/spike/associations/belongs_to.rb → lib/spyke/associations/belongs_to.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
module Spike | ||
module Spyke | ||
module Associations | ||
class BelongsTo < Association | ||
def initialize(*args) | ||
|
2 changes: 1 addition & 1 deletion
2
lib/spike/associations/has_many.rb → lib/spyke/associations/has_many.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
module Spike | ||
module Spyke | ||
module Associations | ||
class HasMany < Association | ||
def initialize(*args) | ||
|
2 changes: 1 addition & 1 deletion
2
lib/spike/associations/has_one.rb → lib/spyke/associations/has_one.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
module Spike | ||
module Spyke | ||
module Associations | ||
class HasOne < Association | ||
def initialize(*args) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
module Spike | ||
module Spyke | ||
class Collection < ::Array | ||
attr_reader :metadata | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
module Spike | ||
module Spyke | ||
class Config | ||
class_attribute :connection | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module Spike | ||
module Spyke | ||
class ResourceNotFound < StandardError; end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
module Spike | ||
module Spyke | ||
module Orm | ||
extend ActiveSupport::Concern | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
module Spike | ||
module Spyke | ||
class Result | ||
attr_reader :body | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
module Spike | ||
module Spyke | ||
class ScopeRegistry | ||
extend ActiveSupport::PerThreadRegistry | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module Spyke | ||
VERSION = '1.0.0' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.