Skip to content

Commit

Permalink
Merge pull request #5 from kinoppyd/feature/cron
Browse files Browse the repository at this point in the history
Cron
  • Loading branch information
kinoppyd committed Sep 15, 2018
2 parents e3b6794 + dfcff3d commit 06618e8
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 16 deletions.
49 changes: 34 additions & 15 deletions lib/mobb/base.rb
@@ -1,20 +1,24 @@
require 'repp'
require 'whenever'
require 'parse-cron'

require "mobb/version"

module Mobb
class Matcher
def initialize(pattern, options) @pattern, @options = pattern, options; end
def initialize(pattern, options = {}) @pattern, @options = pattern, options; end
def regexp?; pattern.is_a?(Regexp); end
def inspect; "pattern: #{@pattern}, options #{@options}"; end
def invoke(time = Time.now) @options[:last_invoked] = time; end
def cron?; pattern.is_a?(CronParser); end
def tick(time = Time.now) @options[:last_tick] = time; end
def last_tick; @options[:last_tick] end

def match?(context)
case context
when String
string_matcher(context)
when Time
# TODO: do something
false
cron_matcher(context)
when Array
context.all? { |c| match?(c) }
else
Expand Down Expand Up @@ -43,27 +47,26 @@ def string_matcher(string)
false
end
end

def cron_matcher(time)
last = last_tick
tick(time) if !last || time > last
return false if time < last
pattern.next(time) == pattern.next(last) ? false : true
end
end

class Base
def call(env)
dup.call!(env)
end

def tick(env)
dup.tick!(env)
end

def call!(env)
@env = env
invoke { dispatch! }
[@body, @attachments]
end

def tick!(env)
fail # TODO: write logic here
end

def dispatch!
# TODO: encode input messages

Expand Down Expand Up @@ -154,16 +157,24 @@ def settings
def receive(pattern, options = {}, &block) event(:message, pattern, options, &block); end
alias :on :receive

#def every(pattern, options = {}, &block) event(:cron, pattern, options, &block); end
def every(pattern, options = {}, &block) event(:ticker, pattern, options, &block); end

def cron(pattern, options = {}, &block) event(:ticker, pattern, options, &block); end

def event(type, pattern, options, &block)
(@events[type] ||= []) << compile!(type, pattern, options, &block)
end

def compile!(type, pattern, options, &block)
at = options.delete(:at)
options.each_pair { |option, args| send(option, *args) }

matcher = compile(pattern, options)
matcher = case type
when :message
compile(pattern, options)
when :ticker
compile_cron(pattern, at)
end
unbound_method = generate_method("#{type}", &block)
before_conditions, @before_conditions = @before_conditions, []
after_conditions, @after_conditions = @after_conditions, []
Expand All @@ -176,6 +187,14 @@ def compile!(type, pattern, options, &block)

def compile(pattern, options) Matcher.new(pattern, options); end

def compile_cron(time, at)
if String === time
Matcher.new(CronParser.new(time))
else
Matcher.new(CronParser.new(Whenever::Output::Cron.new(time, nil, at).time_in_cron_syntax))
end
end

def generate_method(name, &block)
define_method(name, &block)
method = instance_method(name)
Expand Down Expand Up @@ -394,7 +413,7 @@ def self.delegate(*methods)
end
end

delegate :receive, :on, #:every,
delegate :receive, :on, :every, :cron,
:set, :enable, :disable, :clear,
:helpers

Expand Down
4 changes: 3 additions & 1 deletion mobb.gemspec
Expand Up @@ -19,7 +19,9 @@ Gem::Specification.new do |spec|
end
spec.require_paths = ["lib"]

spec.add_dependency "repp", "~> 0.2"
spec.add_dependency "repp", "~> 0.3"
spec.add_dependency "whenever", "~> 0.10"
spec.add_dependency "parse-cron", "~> 0.1"

spec.add_development_dependency "bundler", "~> 1.16"
spec.add_development_dependency "rake", "~> 10.0"
Expand Down

0 comments on commit 06618e8

Please sign in to comment.