<img src=“https://badge.fury.io/rb/logical_model.png” alt=“Gem Version” /> <img src=“https://travis-ci.org/dwaynemac/logical_model.png” /> <img src=“https://codeclimate.com/github/dwaynemac/logical_model.png” />

logical_model

LogicalModel allows to use a RESTfull resources as models.

It was written following this tutorial: www.slideshare.net/ihower/serviceoriented-design-and-implement-with-rails3

Assumptions

OUTDATED – some of this assumpitions are now configurable. Will update this readme as soon as possible.

LogicalModel makes some assumptions about the service you’ll be consuming

  • resource service is RESTfull

  • INDEX response is

    {
      collection: [resources...],
      total:      total items
    }
    
  • SHOW response is

    {
      .... resource attributes ...
    }
    
  • CREATE response on success is

    {
      id: created_id
    }
    
  • CREATE response on failure is

    {
      message: 'error message',
      error_codes: [],
      errors: attribute_errors
    }
    
  • UPDATE response on success is

    "OK"
    
  • UPDATE response on failure is

    {
      message: 'error message',
      error_codes: [],
      errors: attribute_errors
    }
    
  • DESTROY response is

    "ok"
    

Usage

In your Gemfile:

gem "logical_model"

In a model file:

class RemoteResource < LogicalModel

  set_resource_host "remote.server"
  set_resource_path "/api/remote/path"
  # equivalent: set_resource_url "remote.server", "/api/remote/path"

  # optional
  set_api_key(:token, 'asdfasdf') # will add token=asdfasdf to all requests.

  attribute :id
  attribute :attribute_a
  attribute :attribute_b

  validates_presence_of :id
end

Testing

To run spec:

bundle exec rake

Contributing to logical_model

  • Check out the latest master to make sure the feature hasn’t been implemented or the bug hasn’t been fixed yet

  • Check out the issue tracker to make sure someone already hasn’t requested it and/or contributed it

  • Fork the project

  • Start a feature/bugfix branch

  • Commit and push until you are happy with your contribution

  • Make sure to add tests for it. This is important so I don’t break it in a future version unintentionally.

  • Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.

Copyright © 2011,2012,2013 Dwayne Macgowan. See LICENSE.txt for further details.

TODO

  • put log in /log

  • on create parse response and show errors, etc.