RestclientApiBase

以 REST 方式调用外部 API 的基础类, Content-Type 为 JSON

Dependency

active_support/concern

rest-client

lumberjack

Installation

Add this line to your application's Gemfile:

gem 'restclient_api_base'

And then execute:

$ bundle

Or install it yourself as:

$ gem install restclient_api_base

Usage

require 'restclient_api_base'

module A
  include RestclientApiBase

  self.base_url       = 'http://www.example.com'  # define app base url
  self.private_params = { key: 'api_key' }        # define app secret key
  self.logger_name    = 'Error'                   # define error name in log dir
  self.debug          = true                      # debug
end

res = A.get('/api/xxx', key_1: value_1, key_2: value_2)   # => { response: json_response }
res.code     # => 200
res.cookies  # => { 'key' => 'value' }
res.headers  # => {:content_type=>"text/html; charset=utf-8", :cache_control=>"private" ...

res = A.post('/api/xxx', key_1: value_1, key_2: value_2)
res = A.patch('/api/xxx', key_1: value_1, key_2: value_2)

write_log(res) # => [2014-08-19T22:10:29.438 INFO (32890)] res..

Contributing

  1. Fork it ( https://github.com/Naixspirit/restclient_api_base/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request