aboutsummaryrefslogtreecommitdiffstats
path: root/lib/iev_api/configuration.rb
blob: b445da795e03f3163a39df72725bf14500478afe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
module IevApi
  module Configuration
    VALID_OPTIONS_KEYS = [
      :account,
      :auth_token,
      :secure,
      :connection_options,
      :adapter,
      :user_agent,
      :middleware]

    attr_accessor *VALID_OPTIONS_KEYS

    DEFAULT_ADAPTER     = :net_http
    DEFAULT_USER_AGENT  = "IEV Ruby Gem Api"
    DEFAULT_CONNECTION_OPTIONS = {}
    DEFAULT_MIDDLEWARE  = [
                           Faraday::Request::UrlEncoded,
                           IevApi::Middleware::RaiseResponseError,
                           Faraday::Request::Multipart,
                           FaradayMiddleware::Mashify,
                           #FaradayMiddleware::Caching,
                           FaradayMiddleware::FollowRedirects,
                           FaradayMiddleware::ParseJson,
                           IevApi::Middleware::RaiseServerError,
                           IevApi::Middleware::CustomParser
                          ]

    def self.extended(base)
      base.reset
    end

    def configure(options={})
      @account    = options[:account] if options.has_key?(:account)
      @auth_token = options[:auth_token] if options.has_key?(:auth_token)
      @secure     = options[:secure] if options.has_key?(:secure)
      @middleware = options[:middleware] if options.has_key?(:middleware)
      yield self if block_given?
      self
    end

    def options
      options = {}
      VALID_OPTIONS_KEYS.each{|k| options[k] = send(k)}
      options
    end

    def reset
      @account    = nil
      @auth_token = nil
      @secure     = false
      @adapter    = DEFAULT_ADAPTER
      @user_agent = DEFAULT_USER_AGENT
      @connection_options = DEFAULT_CONNECTION_OPTIONS
      @middleware = DEFAULT_MIDDLEWARE
    end

  end
end