Sha256: ee6ec9fa7120737bf2c418e1dcce15c3595aaf13ab09aab7e26d8d496b61ce13

Contents?: true

Size: 1.19 KB

Versions: 4

Compression:

Stored size: 1.19 KB

Contents

require 'nabaztag_hack_kit/message/api'

module NabaztagHackKit
  module Mods
    module Callback

      module Helpers
        def callback(action, bunny, data, request, run = 0)
          if (cb = self.class.callbacks[action.to_s]) && (callback = cb[run])
            instance_exec(bunny, data, request, run, &callback) || callback(action, bunny, data, request, run+1)
          end
        end
      end

      def on(callback, &block)
        callbacks[callback] ||= []
        callbacks[callback] << block
      end

      def self.registered(app)
        app.helpers Callback::Helpers

        # generic api callback
        %w(get post).each do |method|
          app.send(method, "/api/:bunnyid/:action.jsp") do
            bunny = Bunny.find(params[:bunnyid])
            callback('request', bunny, params, request)
            callback(params[:action], bunny, params, request).tap do |response|
              unless response
                logger.warn "no successful callback found for #{params[:action]}"
                status 404
                return
              end
            end
          end
        end
      end

      def callbacks
        @@callbacks ||= {}
      end

    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
nabaztag_hack_kit-0.1.0.beta8 lib/nabaztag_hack_kit/mods/callback.rb
nabaztag_hack_kit-0.1.0.beta3 lib/nabaztag_hack_kit/mods/callback.rb
nabaztag_hack_kit-0.1.0.beta2 lib/nabaztag_hack_kit/mods/callback.rb
nabaztag_hack_kit-0.1.0.beta1 lib/nabaztag_hack_kit/mods/callback.rb