Class: Apruve::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/apruve/client.rb

Constant Summary collapse

DEFAULTS =
{
    :scheme => 'http',
    :host => 'localhost',
    :port => 3000,
    :version => 'v4',
    :logging_level => 'WARN',
    :connection_timeout => 60,
    :read_timeout => 60,
    :logger => nil,
    :ssl_verify => true,
    :faraday_adapter => Faraday.default_adapter,
    :accept_type => 'application/json'
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key, options = {}) ⇒ Client

Returns a new instance of Client.



27
28
29
30
31
# File 'lib/apruve/client.rb', line 27

def initialize(api_key, options={})
  @api_key = api_key.nil? ? api_key : api_key.strip
  @config = DEFAULTS.merge options
  build_conn
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



79
80
81
82
83
84
85
86
# File 'lib/apruve/client.rb', line 79

def method_missing(method, *args, &block)
  if is_http_method? method
    conn.headers['Apruve-Api-Key'] = @api_key unless @api_key.nil?
    conn.send method, *args
  else
    super method, *args, &block
  end
end

Instance Attribute Details

#api_keyObject

attr_reader :conn



25
26
27
# File 'lib/apruve/client.rb', line 25

def api_key
  @api_key
end

#configObject

attr_reader :conn



25
26
27
# File 'lib/apruve/client.rb', line 25

def config
  @config
end

#connObject

attr_reader :conn



25
26
27
# File 'lib/apruve/client.rb', line 25

def conn
  @conn
end

Instance Method Details

#build_connObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/apruve/client.rb', line 34

def build_conn
  if config[:logger]
    logger = config[:logger]
  else
    logger = Logger.new(STDOUT)
    logger.level = Logger.const_get(config[:logging_level].to_s)
    config[:logger] = logger
  end

  Faraday::Response.register_middleware :handle_apruve_errors => lambda { Faraday::Response::RaiseApruveError }
  Faraday::Response.register_middleware :apruve_json_parser => lambda { FaradayMiddleware::ApruveParseJson }

  options = {
      :request => {
          :open_timeout => config[:connection_timeout],
          :timeout => config[:read_timeout]
      },
      :ssl => {
          :verify => @config[:ssl_verify] # Only set this to false for testing
      }
  }
  @conn = Faraday.new(url, options) do |builder|
    # Order is kinda important here...
    builder.response :raise_error # raise exceptions on 40x, 50x responses
    builder.use Apruve::FaradayErrorHandler
    builder.request :json
    builder.response :logger, logger
    builder.response :handle_apruve_errors
    builder.response :apruve_json_parser
    builder.adapter config[:faraday_adapter]
  end
  conn.path_prefix = "/api/#{@config[:version]}"
  conn.headers['User-Agent'] = "apruve-ruby/#{Apruve::VERSION}"
  conn.headers['Content-Type'] = 'application/json'
  conn.headers['Accept'] = "#{@config[:accept_type]};revision=#{@config[:version]}"
end

#urlObject



71
72
73
74
75
76
77
# File 'lib/apruve/client.rb', line 71

def url
  builder = (config[:scheme] == 'http') ? URI::HTTP : URI::HTTPS

  builder.build({:host => config[:host],
                 :port => config[:port],
                 :scheme => config[:scheme]})
end