class Dragonfly::Job

Constants

STEPS

Attributes

app[R]
content[R]
next_step_index[RW]
steps[RW]
url_attributes[R]

Public Class Methods

deserialize(string, app) click to toggle source
# File lib/dragonfly/job.rb, line 58
def deserialize(string, app)
  array = begin
    Serializer.json_b64_decode(string)
  rescue Serializer::BadString
    if app.allow_legacy_urls
      Serializer.marshal_b64_decode(string, :check_malicious => true) # legacy strings
    else
      raise
    end
  end
  from_a(array, app)
end
from_a(steps_array, app) click to toggle source
# File lib/dragonfly/job.rb, line 45
def from_a(steps_array, app)
  unless steps_array.is_a?(Array) &&
         steps_array.all?{|s| s.is_a?(Array) && step_abbreviations[s.first.to_s] }
    raise InvalidArray, "can't define a job from #{steps_array.inspect}"
  end
  job = app.new_job
  steps_array.each do |step_array|
    step_class = step_abbreviations[step_array.shift.to_s]
    job.steps << step_class.new(job, *step_array)
  end
  job
end
new(app, content="", meta={}) click to toggle source
# File lib/dragonfly/job.rb, line 81
def initialize(app, content="", meta={})
  @app = app
  @next_step_index = 0
  @steps = []
  @content = Content.new(app, content, meta)
  @url_attributes = UrlAttributes.new
end
step_abbreviations() click to toggle source
# File lib/dragonfly/job.rb, line 71
def step_abbreviations
  @step_abbreviations ||= STEPS.inject({}){|hash, step_class| hash[step_class.abbreviation] = step_class; hash }
end
step_names() click to toggle source
# File lib/dragonfly/job.rb, line 75
def step_names
  @step_names ||= STEPS.map{|step_class| step_class.step_name }
end

Public Instance Methods

applied?() click to toggle source
# File lib/dragonfly/job.rb, line 124
def applied?
  next_step_index == steps.length
end
applied_steps() click to toggle source
# File lib/dragonfly/job.rb, line 128
def applied_steps
  steps[0...next_step_index]
end
apply() click to toggle source

Applying, etc.

# File lib/dragonfly/job.rb, line 118
def apply
  pending_steps.each{|step| step.apply }
  self.next_step_index = steps.length
  self
end
fetch_file_step() click to toggle source
# File lib/dragonfly/job.rb, line 220
def fetch_file_step
  last_step_of_type(FetchFile)
end
fetch_step() click to toggle source
# File lib/dragonfly/job.rb, line 212
def fetch_step
  last_step_of_type(Fetch)
end
fetch_url_step() click to toggle source
# File lib/dragonfly/job.rb, line 224
def fetch_url_step
  last_step_of_type(FetchUrl)
end
generate_step() click to toggle source
# File lib/dragonfly/job.rb, line 216
def generate_step
  last_step_of_type(Generate)
end
initialize_copy(other) click to toggle source

Used by 'dup' and 'clone'

# File lib/dragonfly/job.rb, line 90
def initialize_copy(other)
  @steps = other.steps.map do |step|
    step.class.new(self, *step.args)
  end
  @content = other.content.dup
  @url_attributes = other.url_attributes.dup
end
inspect() click to toggle source

Misc

# File lib/dragonfly/job.rb, line 238
def inspect
  "<Dragonfly::Job app=#{app.name.inspect}, steps=#{steps.inspect}, content=#{content.inspect}, steps applied:#{applied_steps.length}/#{steps.length} >"
end
pending_steps() click to toggle source
# File lib/dragonfly/job.rb, line 132
def pending_steps
  steps[next_step_index..-1]
end
process_steps() click to toggle source
# File lib/dragonfly/job.rb, line 228
def process_steps
  steps.select{|s| s.is_a?(Process) }
end
serialize() click to toggle source
# File lib/dragonfly/job.rb, line 146
def serialize
  Serializer.json_b64_encode(to_a)
end
sha() click to toggle source
# File lib/dragonfly/job.rb, line 154
def sha
  unless app.secret
    raise CannotGenerateSha, "A secret is required to sign and verify Dragonfly job requests. "                                  "Use `secret '...'` or `verify_urls false` (not recommended!) in your config."
  end
  OpenSSL::HMAC.hexdigest('SHA256', app.secret, to_unique_s)[0,16]
end
signature() click to toggle source
# File lib/dragonfly/job.rb, line 150
def signature
  Digest::SHA1.hexdigest(to_unique_s)
end
step_types() click to toggle source
# File lib/dragonfly/job.rb, line 232
def step_types
  steps.map{|s| s.class.step_name }
end
to_a() click to toggle source
# File lib/dragonfly/job.rb, line 136
def to_a
  steps.map{|step| step.to_a }
end
to_app() click to toggle source

to_stuff…

# File lib/dragonfly/job.rb, line 189
def to_app
  JobEndpoint.new(self)
end
to_fetched_job(uid) click to toggle source
# File lib/dragonfly/job.rb, line 197
def to_fetched_job(uid)
  new_job = dup
  new_job.steps = []
  new_job.fetch!(uid)
  new_job.next_step_index = 1
  new_job
end
to_response(env={"REQUEST_METHOD" => "GET"}) click to toggle source
# File lib/dragonfly/job.rb, line 193
def to_response(env={"REQUEST_METHOD" => "GET"})
  to_app.call(env)
end
to_unique_s() click to toggle source

Serializing, etc.

# File lib/dragonfly/job.rb, line 142
def to_unique_s
  to_a.to_dragonfly_unique_s
end
uid() click to toggle source

Step inspection

# File lib/dragonfly/job.rb, line 207
def uid
  step = fetch_step
  step.uid if step
end
update_url_attributes(hash) click to toggle source
# File lib/dragonfly/job.rb, line 181
def update_url_attributes(hash)
  hash.each do |key, value|
    url_attributes.send("#{key}=", value)
  end
end
url(opts={}) click to toggle source

URLs, etc.

# File lib/dragonfly/job.rb, line 175
def url(opts={})
  app.url_for(self, opts) unless steps.empty?
end
validate_sha!(sha) click to toggle source
# File lib/dragonfly/job.rb, line 162
def validate_sha!(sha)
  case sha
  when nil
    raise NoSHAGiven
  when self.sha
    self
  else
    raise IncorrectSHA, sha
  end
end

Private Instance Methods

last_step_of_type(type) click to toggle source
# File lib/dragonfly/job.rb, line 254
def last_step_of_type(type)
  steps.select{|s| s.is_a?(type) }.last
end
result() click to toggle source
# File lib/dragonfly/job.rb, line 249
def result
  apply
  content
end