class Johac::Client
Base class for Johac
API Client
.
Extend this class and provide API specific calls
Attributes
Public Class Methods
@param config [Johac::Config]
# File lib/johac/client.rb, line 18 def initialize(config=nil) @config = Johac.merged_config(config) @mutex = Mutex.new end
Public Instance Methods
Capture requests and prevent them from going to the remote. Useful for testing.
@param [Block] the block which invokes one or more requests @return [Array] faraday env structs which were captured in the block
# File lib/johac/client.rb, line 65 def capture(&block) result = [] @mutex.synchronize { begin connection.options.context = result yield(self) ensure connection.options.context = nil end } result end
Produce curls commands for captured requests made, within the block
@param [Block] the block which invokes one or more requests @return [String] requests which were captured as curl commands
# File lib/johac/client.rb, line 42 def curl_capture(&block) capture(&block).map { |env| output = [] output << "curl -s -X#{env.method.to_s.upcase}" env.request_headers.select { |name, value| name.downcase != 'user-agent' }.each { |name, value| output << "-H'#{name}: #{value}'" } output << "'#{env.url}'" if env.body output << '-d' output << "'#{env.body}'" end output.join(' ') } end
Reference to the current environment. Set explicitly in {Johac.configure} or via environment variables JOHAC_ENV
or RAILS_ENV
.
@return [String] One of “testing”, “development”, or “production”
# File lib/johac/client.rb, line 34 def env config.env end
Reference to base_uri
set on {Johac.config} in {Johac.configure} or in the constructor.
@return [String]
# File lib/johac/client.rb, line 26 def uri config.base_uri end
Protected Instance Methods
# File lib/johac/client.rb, line 88 def delete(path, options={}) request { connection.delete(path, options[:query], options[:headers]) } end
# File lib/johac/client.rb, line 84 def get(path, options={}) request { connection.get(path, options[:query], options[:headers]) } end
# File lib/johac/client.rb, line 80 def head(path, options={}) request { connection.head(path, options[:query], options[:headers]) } end
# File lib/johac/client.rb, line 100 def patch(path, options={}) request { connection.patch(path, options[:body], options[:headers]) } end
# File lib/johac/client.rb, line 92 def post(path, options={}) request { connection.post(path, options[:body], options[:headers]) } end
# File lib/johac/client.rb, line 96 def put(path, options={}) request { connection.put(path, options[:body], options[:headers]) } end
Private Instance Methods
Wrap the response or error, or raise an exception if configured
# File lib/johac/client.rb, line 109 def request(&block) Johac::Response.new(yield) rescue => e @config.raise_exceptions ? (raise e) : Johac::Response.new(e) end