No commit activity in last 3 years
No release in over 3 years
REST resource similar to angular resource based on faraday
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.9
~> 10.0

Runtime

 Project Readme

FaradayResource

A backbone model like ruby http wrapper : )

Installation

Add this line to your application's Gemfile:

gem 'faraday_resource'

And then execute:

$ bundle

Or install it yourself as:

$ gem install faraday_resource

Usage

such as:


	class User
	  include FaradayResource::Base

	  set_url 'http://localhost:5100'

	  set_content_type 'application/json'

	  get :load, 'url' => '/users/:id', 'params' => {:test => 'dfdjfk', :id => 1}

	  collection do
	    get :get_list, 'url' => '/users', 'is_array' => true
	  end
	end

now you can use User class such as:

	user = User.new
	response = user.load #=> fetch data
	user.name #=> get name
	response.status #=> status code faraday response

if is_array is true return response and array[user] or only response

	response, users = User.get_list #=> only when is_array is true
	response.status #=> status code faraday response
	users.class #=> Array, array[user]

	response = User.get_list #=> when is_array is false

overwrite url params

	User.get_list({
		:url => '/other_users',
		:params => {
			:q => 'wp'
		}
	})

	#now faraday use '/other_users' and params will merge this params

you can set custom parse function for instance_methods (default JSON.parse)


	class User
		set_parse do |body|
			JSON.parse(body)['entity']
		end
	end

you can set custom array_parse function for is_array is true (default JSON.parse)

	class User
		set_array_parse do |body|
			JSON.parse(body)['entities']
		end
	end

you can set global setting (will be overwitten by class set_url..)

	FaradayResource.configure do |settings|
	  settings.url = 'http://localhost:5100'
	  settings.content_type = 'application/json'
	end

url params xxx will replaced by params[:xxx] or params[xxx]

	class User

		get :load, 'url' => '/users/:id', 'params' => {:test => 'dfdjfk', :id => 1} do |params, instance|
		    params['test'] = instance.name
		    params
		end
	end

get/post/put/delete methods (not in collection) can accept block (params, instance then return params)

	class User

		put :update, 'url' => '/users/:id', 'params' => {} do |params, instance|
		    params['user'] = {
		    	:name => instance.name,
		    	:age => instance.age
		    }
		    params
		end
	end

	u = User.new({id: 2})
	u.load
	u.name = 'zkf'
	u.update

can assign value for attributes, check stale?

	u = User.new({id: 2})
	u.load
	u.name #=> 'wp'
	u.name = 'zkf'
	u.name #=> 'zkf'
	u.stale? #=> true
	u.stale_attributes #=> {'name': 'wp'}

##other

I think this gem will help communicating between app servers (soa)

Development

After checking out the repo, run bin/setup to install dependencies. Then, 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 to create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

  1. Fork it ( https://github.com/[my-github-username]/faraday_resource/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