Module: FmRest::V1::Connection

Included in:
FmRest::V1
Defined in:
lib/fmrest/v1/connection.rb

Constant Summary collapse

BASE_PATH =
"/fmi/data/v1/databases".freeze

Instance Method Summary collapse

Instance Method Details

#base_connection(options = FmRest.default_connection_settings, &block) ⇒ Faraday

Builds a base Faraday connection with base URL constructed from connection options and passes it the given block

Parameters:

  • options (Hash) (defaults to: FmRest.default_connection_settings)

    a customizable set of options

Options Hash (options):

  • :host (String)

    The hostname for the FM server

  • :database (String)

    The FM database name

  • :username (String)

    The username for DAPI authentication

  • :password (String)

    The password for DAPI authentication

  • :ssl (String)

    SSL options to forward to the Faraday connection

  • :proxy (String)

    Proxy options to forward to the Faraday connection

Returns:

  • (Faraday)

    The new Faraday connection



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/fmrest/v1/connection.rb', line 59

def base_connection(options = FmRest.default_connection_settings, &block)
  host = options.fetch(:host)

  # Default to HTTPS
  scheme = "https"

  if host.match(/\Ahttps?:\/\//)
    uri = URI(host)
    host = uri.hostname
    host += ":#{uri.port}" if uri.port != uri.default_port
    scheme = uri.scheme
  end

  faraday_options = {}
  faraday_options[:ssl] = options[:ssl] if options.key?(:ssl)
  faraday_options[:proxy] = options[:proxy] if options.key?(:proxy)

  Faraday.new(
    "#{scheme}://#{host}#{BASE_PATH}/#{URI.escape(options.fetch(:database))}/".freeze,
    faraday_options,
    &block
  )
end

#build_connection(options = FmRest.default_connection_settings, &block) ⇒ Faraday

Builds a complete DAPI Faraday connection with middleware already configured to handle authentication, JSON parsing, logging and DAPI error handling. A block can be optionally given for additional middleware configuration

Options Hash (options):

  • :host (String)

    The hostname for the FM server

  • :database (String)

    The FM database name

  • :username (String)

    The username for DAPI authentication

  • :password (String)

    The password for DAPI authentication

  • :ssl (String)

    SSL options to forward to the Faraday connection

  • :proxy (String)

    Proxy options to forward to the Faraday connection

Returns:

  • (Faraday)

    The new Faraday connection



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
# File 'lib/fmrest/v1/connection.rb', line 19

def build_connection(options = FmRest.default_connection_settings, &block)
  base_connection(options) do |conn|
    conn.use RaiseErrors
    conn.use TokenSession, options

    # The EncodeJson and Multipart middlewares only encode the request
    # when the content type matches, so we can have them both here and
    # still play nice with each other, we just need to set the content
    # type to multipart/form-data when we want to submit a container
    # field
    conn.request :multipart
    conn.request :json

    if options[:log]
      conn.response :logger, nil, bodies: true, headers: true
    end

    # Allow overriding the default response middleware
    if block_given?
      yield conn
    else
      conn.response :json
    end

    conn.adapter Faraday.default_adapter
  end
end