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
0 parents
commit e491013
Showing
8 changed files
with
240 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
*.gem | ||
*.rbc | ||
.bundle | ||
.config | ||
.yardoc | ||
Gemfile.lock | ||
InstalledFiles | ||
_yardoc | ||
coverage | ||
doc/ | ||
lib/bundler/man | ||
pkg | ||
rdoc | ||
spec/reports | ||
test/tmp | ||
test/version_tmp | ||
tmp |
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,4 @@ | ||
source 'https://rubygems.org' | ||
|
||
# Specify your gem's dependencies in active_rest_client.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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
Copyright (c) 2013 Andy Jeffries | ||
|
||
MIT License | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining | ||
a copy of this software and associated documentation files (the | ||
"Software"), to deal in the Software without restriction, including | ||
without limitation the rights to use, copy, modify, merge, publish, | ||
distribute, sublicense, and/or sell copies of the Software, and to | ||
permit persons to whom the Software is furnished to do so, subject to | ||
the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be | ||
included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
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,159 @@ | ||
# ActiveRestClient | ||
|
||
This gem is for accessing REST services in an ActiveRecord style. ActiveResource already exists for this, but it doesn't work where the resource naming doesn't follow Rails conventions, it doesn't have in-built caching and it's not as flexible in general. | ||
|
||
## Installation | ||
|
||
Add this line to your application's Gemfile: | ||
|
||
gem 'active_rest_client' | ||
|
||
And then execute: | ||
|
||
$ bundle | ||
|
||
Or install it yourself as: | ||
|
||
$ gem install active_rest_client | ||
|
||
## Usage | ||
|
||
First you need to create your new model class: | ||
|
||
``` | ||
# config/environments/production.rb | ||
MyApp::Application.configure do | ||
# ... | ||
config.api_server_url = "https://www.example.com/api/v1" | ||
end | ||
# app/models/person.rb | ||
class Person < ActiveRestClient::Base | ||
base_url Rails.application.config.api_server_url | ||
get :all => "/people" | ||
get :find => "/people/:id" | ||
put :save => "/people/:id" | ||
post :create => "/people" | ||
end | ||
``` | ||
|
||
Then you can use it like this: | ||
|
||
``` | ||
# Create a new person | ||
@person = Person.create( | ||
first_name:"John" | ||
last_name:"Smith" | ||
) | ||
# Find a person (not needed after creating) | ||
id = @person.id | ||
@person = Person.find(id) | ||
# Update a person | ||
@person.last_name = "Jones" | ||
@person.save | ||
# Get all people | ||
@people = Person.all | ||
@people.each do |person| | ||
puts "Hi " + person.first_name | ||
end | ||
``` | ||
|
||
Note, you can assign to any attribute, whether it exists or not before and read from any attribute (which will return nil if not found). You can also call any mapped method as an instance variable which will pass the current attribute set in as parameters (either GET or POST depending on the mapped method type). So, we could rewrite the create call above as: | ||
|
||
``` | ||
@person = Person.new | ||
@person.first_name = "John" | ||
@person.last_name = "Smith" | ||
@person.create | ||
puts @person.id | ||
``` | ||
|
||
The response of the #create call set the attributes at that point (any manually set attributes before that point are removed). | ||
|
||
## Advanced Features | ||
|
||
### Caching | ||
|
||
You can enable Expires and ETag based caching with a simple line in the application.rb/production.rb: | ||
|
||
``` | ||
ActiveRestClient::Base.perform_caching = true | ||
``` | ||
|
||
or you can enable it per classes with: | ||
|
||
``` | ||
class Person < ActiveRestClient::Base | ||
perform_caching true | ||
end | ||
``` | ||
|
||
### Using filters | ||
|
||
You can use filters to alter get/post parameters or the URL before a request. This can either be a block or a named method (like ActionController's `before_filter`/`before_action` methods). | ||
|
||
The filter is passed the name of the method (e.g. `:save`) and a request object. The request object has three public attributes `post_params` (a Hash of the POST parameters), `get_params` (a Hash of the GET parameters) and `url` (a String containing the full URL without GET parameters appended) | ||
|
||
``` | ||
require 'secure_random' | ||
class Person < ActiveRestClient::Base | ||
before_request do |name, request| | ||
if request.post? || name == :save | ||
id = request.post_params.delete(:id) | ||
request.get_params[:id] = id | ||
end | ||
end | ||
before_request :replace_token_in_url | ||
private | ||
def replace_token_in_url(name, request) | ||
request.url.gsub!("#token", SecureRandom.hex) | ||
end | ||
end | ||
``` | ||
|
||
### Authentication | ||
|
||
You can authenticate with Basic authentication by putting the username and password in to the `base_url` or by setting them within the specific model: | ||
|
||
``` | ||
class Person < ActiveRestClient::Base | ||
username 'api' | ||
password 'eb693ec-8252c-d6301-02fd0-d0fb7-c3485' | ||
# ... | ||
end | ||
``` | ||
|
||
### Validation | ||
|
||
|
||
|
||
### Content Types | ||
|
||
The default configuration is that the response should be JSON. This automatically adds an "Accept" header "application/json". If you prefer you can have the response as XML by using the following: | ||
|
||
``` | ||
class Person < ActiveRestClient::Base | ||
content_type :xml | ||
# ... | ||
end | ||
``` | ||
|
||
This works for any MIME type registered in Rails using `Mime::Type.register`. | ||
|
||
## Contributing | ||
|
||
1. Fork it | ||
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 new Pull Request |
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 @@ | ||
require "bundler/gem_tasks" | ||
require 'rspec/core/rake_task' | ||
RSpec::Core::RakeTask.new('spec') |
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,27 @@ | ||
# coding: utf-8 | ||
lib = File.expand_path('../lib', __FILE__) | ||
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) | ||
require 'active_rest_client/version' | ||
|
||
Gem::Specification.new do |spec| | ||
spec.name = "active_rest_client" | ||
spec.version = ActiveRestClient::VERSION | ||
spec.platform = Gem::Platform::RUBY | ||
spec.authors = ["Andy Jeffries"] | ||
spec.email = ["andy.jeffries@which.co.uk"] | ||
spec.description = %q{Accessing REST services in an ActiveRecord style} | ||
spec.summary = %q{This gem is for accessing REST services in an ActiveRecord style. ActiveResource already exists for this, but it doesn't work where the resource naming doesn't follow Rails conventions, it doesn't have in-built caching and it's not as flexible in general.} | ||
spec.homepage = "http://www.which.co.uk/" | ||
spec.license = "MIT" | ||
|
||
spec.files = `git ls-files`.split($/) | ||
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } | ||
spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) | ||
spec.require_paths = ["lib"] | ||
|
||
spec.add_development_dependency "bundler", "~> 1.3" | ||
spec.add_development_dependency "rake" | ||
spec.add_development_dependency "rspec" | ||
spec.add_runtime_dependency "oj" | ||
spec.add_runtime_dependency "patron" | ||
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
require "active_rest_client/version" | ||
|
||
module ActiveRestClient | ||
# Your code goes here... | ||
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module ActiveRestClient | ||
VERSION = "0.0.1" | ||
end |