foobara-http-api-command encapsulates an external HTTP API call in a Foobara command.
Add foobara-http-api-command to your Gemfile or .gemspec file and bundle install.
Or even just gem install foobara-http-api-command if playing with it directly in the scripts.
Require the library and include Foobara::HttpApiCommand in a command class.
require "foobara/http_api_command"
class Search < Foobara::Command
include Foobara::HttpApiCommand
inputs do
query :string, :required
end
result [Hash]
base_url "https://rubygems.org/api/v1"
path "/search.json"
def build_request_body
self.request_body = { query: }
end
end
results = Search.run!(query: "foobara")
pp results.first # => an Array of Hash results from the APITo make a POST request, set http_method :post. The request body will then be sent as JSON.
require "foobara/http_api_command"
class Post < Foobara::Command
include Foobara::HttpApiCommand
inputs do
foo :string, :required
end
result Hash
url "https://httpbin.org/post"
http_method :post
http_timeout 120
def build_request_body
self.request_body = { foo: }
end
end
response = Post.run!(foo: "bar")
pp response.fetch("json") # => {"foo"=>"bar"}Contributions are welcome on Github at foobara/http-api-command.
To work on an existing issue or submit a PR:
- Fork the repository and clone it to your local machine
- Run
bundle installto install dependencies - Run
raketo ensure that everything works as expected before making any changes - Implement your changes
- Re-run
raketo make sure that all tests and Rubocop pass - Commit your changes, push to GitHub and open a Pull Request for review
This project is licensed under the MPL-2.0 license. Please see LICENSE.txt for more information.