Wolox on Rails - Requests

Gem Version Build Status Code Climate

Make external requests in you service objects, with easy logging and error handling!

Installation

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

Generators

rails generate wor:requests:service NAME

Generator options

module

Specifying the module name as:

rails generate wor:requests:service NAME --module MODULE_NAME

We can create a service with an inner class called NAME, and external module called MODULE_NAME

module ModuleName
  class NameService < Wor::Requests::Base
    # Your code here
  end
end

Service example

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('wolox')

If you need to get the response headers, add a block in the call with the headers output

GithubService.new.repositories('wolox') do |response|
  puts response.headers
end

If you need to send body parameters in a post request you can write something like this:

require 'wor/requests'

class ExternalService < Wor::Requests::Base
  def post_request_example(authorization)
    post(
      attempting_to: 'Make a POST request to an external service.',
      path: '/some_endpoint',
      headers: {
        Authorization: authorization,
        'Content-type' => 'application/json'
      },
      body: {
        # Some Json
      }
    )
  end

  protected

  def base_url
    'https://external.service.com'
  end
end

ExternalService.new.post_request_example(token)

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Run rubocop lint (rubocop -R --format simple)
  5. Run rspec tests (bundle exec rspec)
  6. Push your branch (git push origin my-new-feature)
  7. Create a new Pull Request

About

This project was developed by Diego Raffo along with Michel Agopian and it was written by Wolox.

Maintainers: Samir Tapiero

Contributors: Diego Raffo Michel Agopian

Wolox

License

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.