Lite::Service

Gem Version Build Status

Lite::Service provides an API for building service objects.

Installation

Add this line to your application's Gemfile:

gem 'lite-service'

And then execute:

$ bundle

Or install it yourself as:

$ gem install lite-service

Table of Contents

Setup

Setting up the service object is very easy and provides a high level API for memoizing pre-resulted values and for surfacing errors.

Learn more about using memoization subclass in Lite::Memoize and errors subclass Lite::Errors.

class SearchMovies
  prepend Lite::Service::Command

  def initialize(name)
    @name = name
  end

  def call
    { generate_fingerprint => movies_by_name }
  end

  private

  def movies_by_name
    cache.memoize { HTTP.get("http://movies.com?title=#{title}") }
  end

  def generate_fingerprint
    Digest::MD5.hexdigest(find_by_name)
  rescue
    errors.add(:fingerprint, 'invalid movie name')
  end

end

Usage

Caller

service = SearchMovies.new('Toy Story')
service.called? #=> false
service.call    #=> { 'fingerprint_1' => [ 'Toy Story 1', ... ] }
service.called? #=> true

# or

service = SearchMovies.call('Toy Story')
service.called? #=> true
service.result  #=> { 'fingerprint_1' => [ 'Toy Story 2', ... ] }

Cache

service = SearchMovies.call('Toy Story')
service.cache #=> Lite::Memoize::Instance object

Errors

service = SearchMovies.call('Toy Story')
service.validate! #=> Raises Lite::Service::ValidationError if it has any errors
service.valid?    #=> Alias for validate!

service.errors    #=> Lite::Errors::Messages object
service.errored?  #=> false
service.success?  #=> true
service.failure?  #=> Checks that it has been called and has errors

Result

service = SearchMovies.new('Toy Story')
service.result  #=> nil

service.call    #=> { 'fingerprint_1' => [ 'Toy Story 1', ... ] }
service.result  #=> { 'fingerprint_1' => [ 'Toy Story 1', ... ] }

service.recall! #=> Clears the call, cache, errors, and then re-performs the call
service.result  #=> { 'fingerprint_2' => [ 'Toy Story 2', ... ] }

service.result! #=> Raises Lite::Service::ValidationError if it has any errors

Development

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

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/lite-service. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the Lite::Service project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.