Class: Prove::Client

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

Constant Summary collapse

DEFAULTS =
{faraday_adapter: Faraday.default_adapter,
log_level: :warn,
logger: Logger.new(STDOUT),
scheme: :https,
port: 443,
host: 'getprove.com'}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}, &block) ⇒ Client

Returns a new instance of Client.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/prove/client.rb', line 16

def initialize(options={}, &block)
  config = DEFAULTS.merge(options)

  if !options.has_key?(:logger) or (options.has_key?(:log_level) and options.has_key?(:logger))
    config[:logger].level = Logger.const_get(config[:log_level].to_s.upcase)
  end

 
  block = block_given? ? block : Proc.new do |cxn|
    cxn.request  :url_encoded
    cxn.response :logger, config[:logger] 
    cxn.response :json
    # cxn.response :raise_error  # raise exceptions on 40x, 50x responses
    cxn.adapter config[:faraday_adapter] 
  end
  
  url_builder = config[:scheme] == :http ? URI::HTTP : URI::HTTPS
  url = url_builder.build(host: config[:host], port: config[:port])

  @connection = Faraday.new(url, &block)     
  @connection.headers['User-Agent'] = "Prove-Ruby"

  self.api_key = config[:api_key]
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



7
8
9
# File 'lib/prove/client.rb', line 7

def api_key
  @api_key
end

#connectionObject

Returns the value of attribute connection.



7
8
9
# File 'lib/prove/client.rb', line 7

def connection
  @connection
end

Instance Method Details

#get(url, options = {}, &block) ⇒ Object



46
47
48
# File 'lib/prove/client.rb', line 46

def get(url, options = {}, &block)
  return @connection.get(url, options, &block) 
end

#post(url, options = {}, &block) ⇒ Object



50
51
52
# File 'lib/prove/client.rb', line 50

def post(url, options = {}, &block)
  return @connection.post(url, options, &block)
end