Sha256: 212f2d21f5cbbdf7af90cf8a310a13b39c8c454bd61d46e9b65a98ac27216fdb

Contents?: true

Size: 1.92 KB

Versions: 2

Compression:

Stored size: 1.92 KB

Contents

module Troo
  module Retrieval
    class Remote
      attr_reader :external_id

      class << self
        # @param  []
        # @param  []
        # @param  [Hash]
        # @return []
        def fetch(klass, external_id, options = {})
          new(klass, external_id, options).fetch
        end
      end

      # @param  []
      # @param  []
      # @param  [Hash]
      # @return []
      def initialize(klass, external_id, options = {})
        @klass       = klass
        @external_id = external_id
        @options     = options
      end

      # @return []
      def fetch
        return []        if none?
        return persist   if persist?
        resources
      end

      private

      attr_reader :klass

      def none?
        resources.empty?
      end

      def persist
        @persisted ||= Troo::Persistence::Local
          .with_collection(resources)
      end

      def resources
        @resources ||= API::Client.perform(parameters)
      end

      def parameters
        {
          verb:          :get,
          endpoint:      nil,
          interpolation: interpolation,
          query:         {},
          model:         klass
        }.merge!(resource_parameters)
      end

      def resource_parameters
        modes.fetch(mode)
      end

      def modes
        {
          all:    klass.all,
          board:  klass.by_board_id,
          list:   klass.by_list_id,
          card:   klass.by_card_id,
          member: klass.by_member_id,
          none:   {}
        }
      end

      def mode
        options.fetch(:mode, :none)
      end

      def persist?
        options.fetch(:persist)
      end

      def options
        remote_options.merge!(@options)
      end

      def remote_options
        defaults.merge!(klass.remote_options)
      end

      def defaults
        { mode: :none, persist: true }
      end

      def interpolation
        { external_id: external_id }
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
troo-0.0.12 lib/troo/retrieval/remote.rb
troo-0.0.11 lib/troo/retrieval/remote.rb