Make external requests in you service objects, with easy logging and error handling!
Add this line to your application's Gemfile:
gem 'wor-requests'
And then execute:
$ bundle
Or install it yourself as:
$ gem install wor-requests
Then you can run rails generate wor:requests:install
to create the initializer:
# config/initializers/wor_requests.rb
Wor::Requests.configure do |config|
config.logger = Rails.logger
end
To write your first Service using Wor-requests you can write something like this:
require 'wor/requests'
class GithubService < Wor::Requests::Base
def repositories(username)
get(
attempting_to: "get repositories of #{username}",
path: "/users/#{username}/repos"
)
rescue Wor::Requests::RequestError => e
puts e.message
end
protected
def base_url
'https://api.github.com'
end
end
puts GithubService.new.repositories('alebian')
Or, even easier, run rails generate wor:requests:service NAME
in your Rails root
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Run rubocop lint (
rubocop -R --format simple
) - Run rspec tests (
bundle exec rspec
) - Push your branch (
git push origin my-new-feature
) - Create a new Pull Request
This project is maintained by Diego Raffo along with Michel Agopian and it was written by Wolox.
wor-requests is available under the MIT license.
Copyright (c) 2017 Wolox
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.