class Johac::Client

Base class for Johac API Client.

Extend this class and provide API specific calls

Attributes

config[R]

Public Class Methods

new(config=nil) click to toggle source

@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() { |self| ... } click to toggle source

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
curl_capture(&block) click to toggle source

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
env() click to toggle source

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
uri() click to toggle source

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

delete(path, options={}) click to toggle source
# File lib/johac/client.rb, line 88
def delete(path, options={})
  request { connection.delete(path, options[:query], options[:headers]) }
end
get(path, options={}) click to toggle source
# File lib/johac/client.rb, line 84
def get(path, options={})
  request { connection.get(path, options[:query], options[:headers]) }
end
head(path, options={}) click to toggle source
# File lib/johac/client.rb, line 80
def head(path, options={})
  request { connection.head(path, options[:query], options[:headers]) }
end
patch(path, options={}) click to toggle source
# File lib/johac/client.rb, line 100
def patch(path, options={})
  request { connection.patch(path, options[:body], options[:headers]) }
end
post(path, options={}) click to toggle source
# File lib/johac/client.rb, line 92
def post(path, options={})
  request { connection.post(path, options[:body], options[:headers]) }
end
put(path, options={}) click to toggle source
# File lib/johac/client.rb, line 96
def put(path, options={})
  request { connection.put(path, options[:body], options[:headers]) }
end

Private Instance Methods

request() { || ... } click to toggle source

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