Class: Unobtainium::Faraday::Driver

Inherits:
Object
  • Object
show all
Extended by:
Support::Utility
Defined in:
lib/unobtainium-faraday/driver.rb

Overview

Driver implementation using faraday (and open-uri).

Constant Summary collapse

LABELS =

Recognized labels for matching the driver

{
  faraday: [:api,],
}.freeze
DEFAULT_OPTIONS =

Default options to pass to Faraday

PathedHash.new(
  connection: {
    request: [:multipart, :json],
    response: [
      [:xml, content_type: /\bxml$/],
      [:json, content_type: /\bjson$/],
    ],
  }
).freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

Map any missing method to nokogiri



151
152
153
154
155
156
# File 'lib/unobtainium-faraday/driver.rb', line 151

def method_missing(meth, *args, &block)
  if not @conn.nil? and @conn.respond_to?(meth)
    return @conn.send(meth.to_s, *args, &block)
  end
  return super
end

Instance Attribute Details

#connObject

Returns the value of attribute conn.



158
159
160
# File 'lib/unobtainium-faraday/driver.rb', line 158

def conn
  @conn
end

Class Method Details

.create(_, options) ⇒ Object

Create and return a driver instance



87
88
89
90
# File 'lib/unobtainium-faraday/driver.rb', line 87

def create(_, options)
  driver = ::Unobtainium::Faraday::Driver.new(options)
  return driver
end

.ensure_preconditions(_, _) ⇒ Object

Ensure that the driver’s preconditions are fulfilled.



51
52
53
54
55
56
57
58
59
60
# File 'lib/unobtainium-faraday/driver.rb', line 51

def ensure_preconditions(_, _)
  require 'faraday'
  require 'faraday_middleware'
  require 'faraday_json'
rescue LoadError => err
  raise LoadError, "#{err.message}: you need to add "\
        "'faraday', 'faraday_middleware' and 'faraday_json' to your "\
        "Gemfile to use this driver!",
        err.backtrace
end

.matches?(label) ⇒ Boolean

Return true if the given label matches this driver implementation, false otherwise.

Returns:

  • (Boolean)


45
46
47
# File 'lib/unobtainium-faraday/driver.rb', line 45

def matches?(label)
  return nil != normalize_label(label)
end

.resolve_options(label, options) ⇒ Object

Mostly serves to populate options with default options.



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/unobtainium-faraday/driver.rb', line 64

def resolve_options(label, options)
  # Start with sensible defaults
  opts = {}
  if not options.nil?
    opts = options.dup
  end
  opts = DEFAULT_OPTIONS.recursive_merge(opts)

  # For SSL options, if 'client_key' and 'client_cert' are
  # 1) Strings that name files, then the file contents are substituted, and
  # 2) Strings that are certificates/private keys, then they are parsed
  resolve_ssl_option(opts, 'ssl.client_cert',
                     /BEGIN CERTIFICATE/,
                     ::OpenSSL::X509::Certificate)
  resolve_ssl_option(opts, 'ssl.client_key',
                     /BEGIN (EC|DSA|RSA| *) ?PRIVATE KEY/,
                     ::OpenSSL::PKey, 1)

  return normalize_label(label), opts
end

.resolve_ssl_option(opts, path, pattern, klass, match_index = 0) ⇒ Object

Helper function for resolve_options for resolving SSL options



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/unobtainium-faraday/driver.rb', line 94

def resolve_ssl_option(opts, path, pattern, klass, match_index = 0)
  # Ignore if the path doesn't exist
  if opts.nil? or opts[path].nil?
    return
  end

  # Skip if the path isn't a String. We assume it's already been
  # processed. Either way, faraday can take care of it.
  val = opts[path]
  if not val.is_a? String
    return
  end

  # If the string represents a file name, read the file! Any errors with
  # that should go through to the caller.
  if File.file?(val)
    val = File.read(val)
  end

  # If the value doesn't match the given pattern, that seems like an
  # error.
  match = val.match(pattern)
  if not match
    raise ArgumentError, "Option '#{path}' does not appear to be valid, "\
          "as it does not match #{pattern}."
  end

  # Finally, we can pass the value on to OpenSSL/the klass. Make that
  # dependent on what class klass actually is.
  case klass
  when Class
    val = klass.new(val)
  when Module
    name = match[match_index]
    if name.nil? or name.empty?
      name = 'RSA'
    end
    name = '::' + klass.name + '::' + name
    val = Object.const_get(name).new(val)
  end

  # Overwrite the options!
  opts[path] = val
end

Instance Method Details

#respond_to_missing?(meth, include_private = false) ⇒ Boolean

Map any missing method to nokogiri

Returns:

  • (Boolean)


142
143
144
145
146
147
# File 'lib/unobtainium-faraday/driver.rb', line 142

def respond_to_missing?(meth, include_private = false)
  if not @conn.nil? and @conn.respond_to?(meth, include_private)
    return true
  end
  return super
end