module FmRest::Spyke::Model::Connection

This module provides methods for configuring the Farday connection for the model, as well as setting up the connection itself.

Public Instance Methods

clear_fmrest_config_overlay() click to toggle source

Clears the connection settings overlay.

# File lib/fmrest/spyke/model/connection.rb, line 70
def clear_fmrest_config_overlay
  Thread.current[fmrest_config_overlay_key] = nil
end
connection() click to toggle source

Spyke override – Defaults to `fmrest_connection`

Calls superclass method
# File lib/fmrest/spyke/model/connection.rb, line 98
def connection
  super || fmrest_connection
end
faraday(&block) click to toggle source

Sets a block for injecting custom middleware into the Faraday connection.

@example

class MyModel < FmRest::Spyke::Base
  faraday do |conn|
    # Set up a custom logger for the model
    conn.response :logger, MyApp.logger, bodies: true
  end
end
# File lib/fmrest/spyke/model/connection.rb, line 113
def faraday(&block)
  self.faraday_block = block
end
fmrest_config() click to toggle source
# File lib/fmrest/spyke/model/connection.rb, line 21
def fmrest_config
  if fmrest_config_overlay
    return FmRest.default_connection_settings.merge(fmrest_config_overlay, skip_validation: true)
  end

  FmRest.default_connection_settings
end
fmrest_config=(settings) click to toggle source

Sets the FileMaker connection settings for the model.

Behaves similar to ActiveSupport's `class_attribute`, so it can be inherited and safely overwritten in subclasses.

@param settings [Hash] The settings hash

# File lib/fmrest/spyke/model/connection.rb, line 36
def fmrest_config=(settings)
  settings = ConnectionSettings.new(settings, skip_validation: true)

  singleton_class.redefine_method(:fmrest_config) do
    overlay = fmrest_config_overlay
    return settings.merge(overlay, skip_validation: true) if overlay
    settings
  end
end
fmrest_config_overlay() click to toggle source

@return [FmRest::ConnectionSettings] the connection settings

overlay if any is in use
# File lib/fmrest/spyke/model/connection.rb, line 60
def fmrest_config_overlay
  Thread.current[fmrest_config_overlay_key] || begin
    superclass.fmrest_config_overlay
  rescue NoMethodError
    nil
  end
end
fmrest_config_overlay=(settings) click to toggle source

Allows overriding some connection settings in a thread-local manner. Useful in the use case where you want to connect to the same database using different accounts (e.g. credentials provided by users in a web app context).

@param (see fmrest_config=)

# File lib/fmrest/spyke/model/connection.rb, line 53
def fmrest_config_overlay=(settings)
  Thread.current[fmrest_config_overlay_key] = settings
end
fmrest_config_overlay_key() click to toggle source
# File lib/fmrest/spyke/model/connection.rb, line 152
def fmrest_config_overlay_key
  :"#{object_id}.fmrest_config_overlay"
end
fmrest_connection() click to toggle source
# File lib/fmrest/spyke/model/connection.rb, line 119
def fmrest_connection
  memoize = false

  # Don't memoize the connection if there's an overlay, since
  # overlays are thread-local and so should be the connection
  unless fmrest_config_overlay
    return @fmrest_connection if @fmrest_connection
    memoize = true
  end

  config = ConnectionSettings.wrap(fmrest_config)

  connection =
    FmRest::V1.build_connection(config) do |conn|
      faraday_block.call(conn) if faraday_block

      # Pass the class to SpykeFormatter's initializer so it can have
      # access to extra context defined in the model, e.g. a portal
      # where name of the portal and the attributes prefix don't match
      # and need to be specified as options to `portal`
      conn.use FmRest::Spyke::SpykeFormatter, self

      conn.use FmRest::V1::TypeCoercer, config

      # FmRest::Spyke::JsonParse expects symbol keys
      conn.response :json, parser_options: { symbolize_names: true }
    end

  @fmrest_connection = connection if memoize

  connection
end
with_overlay(settings) { || ... } click to toggle source

Runs a block of code in the context of the given connection settings without affecting the connection settings outside said block.

@param (see fmrest_config=)

@example

Honeybee.with_overlay(username: "...", password: "...") do
  Honeybee.query(...)
end
# File lib/fmrest/spyke/model/connection.rb, line 85
def with_overlay(settings, &block)
  Fiber.new do
    begin
      self.fmrest_config_overlay = settings
      yield
    ensure
      self.clear_fmrest_config_overlay
    end
  end.resume
end