0.0
A long-lived project that still receives updates
wrapper of faraday
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 0.14.2

Runtime

~> 1.10, >= 1.10.3
 Project Readme

JsonRequester

A Wrapper of Faraday Gem

Install

$ gem install json_requester

How to Use

  host = 'http://httpbingo.org'
  # `timeout` at Faraday gem default is 60 secs.
  # `user_agent` at Faraday gem default is like "Faraday v1.10.0", it would be deep_merge at Faraday default setting.
  requester = JsonRequester.new(host, timeout: 120, user_agent: 'My Agent 1.2')
  
  http_method = :get  # :get / :post / :put / :delete
  path = ''
  headers = { 'Authorization' => 'Bearer token' }

  # The `:get` method will use query params;
  # Other HTTP methods will use JSON body to request.
  params = {
    key_1: 'value_1',
    key_2: 'value_2'
  }

  # Request by using JSON body or query params, use the `http_send` method.
  # other methods: `form_send`, `multipart_form_send`
  # `sort_params` at Faraday gem default is true.
  # `content_type_charset` default is 'utf-8', this will add ; charset=utf-8 after `Content-Type` header (ex. `Content-Type=application/json; charset=utf-8`).
  res = requester.http_send(http_method, path, params, headers, sort_params: true, content_type_charset: 'utf-8')
  
  # http response code
  puts res['status'] # 200, 404, .. etc
  # the response JSON body
  puts res['body'] # { foo: 'bar' }