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.config, &block) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/fmrest/v1/connection.rb', line 40

def base_connection(options = FmRest.config, &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.new("#{scheme}://#{host}#{BASE_PATH}/#{URI.escape(options.fetch(:database))}/".freeze, &block)
end

#build_connection(options = FmRest.config, &block) ⇒ Object



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

def build_connection(options = FmRest.config, &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