# Lite::Service [![Gem Version](https://badge.fury.io/rb/lite-service.svg)](http://badge.fury.io/rb/lite-service) [![Build Status](https://travis-ci.org/drexed/lite-service.svg?branch=master)](https://travis-ci.org/drexed/lite-service) Lite::Service provides an API for building command based service objects. It also has mixins for handling errors and memoization. ## Installation Add this line to your application's Gemfile: ```ruby gem 'lite-service' ``` And then execute: $ bundle Or install it yourself as: $ gem install lite-service ## Table of Contents * [Setup](#setup) * [Base](#base) * [Errors](#errors) * [Memoize](#memoize) ## Setup Setting up the service object is very easy and provides a high level API for memoizing pre-resulted values and for surfacing errors. ```ruby class SearchMovies < Lite::Service::Base def initialize(name) @name = name end # NOTE: This method is required def command { generate_fingerprint => movies_by_name } end private def movies_by_name HTTP.get("http://movies.com?title=#{title}") end def generate_fingerprint Digest::MD5.hexdigest(movies_by_name) end end ``` **Caller** ```ruby 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.call #=> { 'fingerprint_1' => [ 'Toy Story 1', ... ] } # - or - # Use full when you are not using the Errors mixin. SearchMovies.run('Toy Story') #=> { 'fingerprint_1' => [ 'Toy Story 1', ... ] } ``` **Result** ```ruby 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', ... ] } ``` ## Errors (optional) Learn more about using [Lite::Errors](https://github.com/drexed/lite-errors) ```ruby class SearchMovies < Lite::Service::Base include Lite::Service::Errors # ... ommited ... private # Add a fingerprint error to the error pool def generate_fingerprint Digest::MD5.hexdigest(movies_by_name) rescue errors.add(:fingerprint, 'invalid md5 request value') end end ``` **Methods** ```ruby service = SearchMovies.call('Toy Story') service.errors #=> Lite::Errors::Messages object service.validate! #=> Raises Lite::Service::ValidationError if it has any errors service.valid? #=> Alias for validate! service.errored? #=> false service.success? #=> true service.failure? #=> Checks that it has been called and has errors service.result! #=> Raises Lite::Service::ValidationError if it has any errors, if not it returns the result ``` ## Memoize (optional) Learn more about using [Lite::Memoize](https://github.com/drexed/lite-memoize) ```ruby class SearchMovies < Lite::Service::Base include Lite::Service::Memoize # ... ommited ... private # Sets the value in the cache # Subsequent method calls gets the cached value # This saves you the extra external HTTP.get call def movies_by_name cache.memoize { HTTP.get("http://movies.com?title=#{title}") } end # Gets the value in the cache def generate_fingerprint Digest::MD5.hexdigest(movies_by_name) end end ``` **Methods** ```ruby service = SearchMovies.call('Toy Story') service.cache #=> Lite::Memoize::Instance object ``` ## 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](https://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](http://contributor-covenant.org) code of conduct. ## License The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT). ## 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](https://github.com/[USERNAME]/lite-service/blob/master/CODE_OF_CONDUCT.md).