Class: Dragonfly::Job

Inherits:
Object show all
Extended by:
Forwardable
Defined in:
lib/dragonfly/job.rb,
lib/dragonfly/job/step.rb,
lib/dragonfly/job/fetch.rb,
lib/dragonfly/job/process.rb,
lib/dragonfly/job/generate.rb,
lib/dragonfly/job/fetch_url.rb,
lib/dragonfly/job/fetch_file.rb

Defined Under Namespace

Classes: AppDoesNotMatch, Fetch, FetchFile, FetchUrl, Generate, IncorrectSHA, InvalidArray, JobAlreadyApplied, NoSHAGiven, NothingToProcess, Process, Step

Constant Summary

STEPS =
[
  Fetch,
  Process,
  Generate,
  FetchFile,
  FetchUrl
]

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Job) initialize(app, content = "", meta = {})

Returns a new instance of Job



79
80
81
82
83
84
85
# File 'lib/dragonfly/job.rb', line 79

def initialize(app, content="", meta={})
  @app = app
  @next_step_index = 0
  @steps = []
  @content = Content.new(app, content, meta)
  @url_attributes = UrlAttributes.new
end

Instance Attribute Details

- (Object) app (readonly)

Returns the value of attribute app



96
97
98
# File 'lib/dragonfly/job.rb', line 96

def app
  @app
end

- (Object) content (readonly)

Returns the value of attribute content



96
97
98
# File 'lib/dragonfly/job.rb', line 96

def content
  @content
end

- (Object) steps

Returns the value of attribute steps



96
97
98
# File 'lib/dragonfly/job.rb', line 96

def steps
  @steps
end

- (Object) url_attributes (readonly)

Returns the value of attribute url_attributes



173
174
175
# File 'lib/dragonfly/job.rb', line 173

def url_attributes
  @url_attributes
end

Class Method Details

+ (Object) deserialize(string, app)



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/dragonfly/job.rb', line 56

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

+ (Object) from_a(steps_array, app)



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/dragonfly/job.rb', line 43

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

+ (Object) step_abbreviations



69
70
71
# File 'lib/dragonfly/job.rb', line 69

def step_abbreviations
  @step_abbreviations ||= STEPS.inject({}){|hash, step_class| hash[step_class.abbreviation] = step_class; hash }
end

+ (Object) step_names



73
74
75
# File 'lib/dragonfly/job.rb', line 73

def step_names
  @step_names ||= STEPS.map{|step_class| step_class.step_name }
end

Instance Method Details

- (Boolean) applied?

Returns:

  • (Boolean)


122
123
124
# File 'lib/dragonfly/job.rb', line 122

def applied?
  next_step_index == steps.length
end

- (Object) applied_steps



126
127
128
# File 'lib/dragonfly/job.rb', line 126

def applied_steps
  steps[0...next_step_index]
end

- (Object) apply

Applying, etc.



116
117
118
119
120
# File 'lib/dragonfly/job.rb', line 116

def apply
  pending_steps.each{|step| step.apply }
  self.next_step_index = steps.length
  self
end

- (Object) fetch_file_step



214
215
216
# File 'lib/dragonfly/job.rb', line 214

def fetch_file_step
  last_step_of_type(FetchFile)
end

- (Object) fetch_step



206
207
208
# File 'lib/dragonfly/job.rb', line 206

def fetch_step
  last_step_of_type(Fetch)
end

- (Object) fetch_url_step



218
219
220
# File 'lib/dragonfly/job.rb', line 218

def fetch_url_step
  last_step_of_type(FetchUrl)
end

- (Object) generate_step



210
211
212
# File 'lib/dragonfly/job.rb', line 210

def generate_step
  last_step_of_type(Generate)
end

- (Object) initialize_copy(other)

Used by 'dup' and 'clone'



88
89
90
91
92
93
94
# File 'lib/dragonfly/job.rb', line 88

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

- (Object) inspect

Misc



232
233
234
# File 'lib/dragonfly/job.rb', line 232

def inspect
  "<Dragonfly::Job app=#{app.name.inspect}, steps=#{steps.inspect}, content=#{content.inspect}, steps applied:#{applied_steps.length}/#{steps.length} >"
end

- (Object) pending_steps



130
131
132
# File 'lib/dragonfly/job.rb', line 130

def pending_steps
  steps[next_step_index..-1]
end

- (Object) process_steps



222
223
224
# File 'lib/dragonfly/job.rb', line 222

def process_steps
  steps.select{|s| s.is_a?(Process) }
end

- (Object) serialize



144
145
146
# File 'lib/dragonfly/job.rb', line 144

def serialize
  Serializer.json_b64_encode(to_a)
end

- (Object) sha



152
153
154
# File 'lib/dragonfly/job.rb', line 152

def sha
  Digest::SHA1.hexdigest("#{to_unique_s}#{app.secret}")[0...8]
end

- (Object) signature



148
149
150
# File 'lib/dragonfly/job.rb', line 148

def signature
  Digest::SHA1.hexdigest(to_unique_s)
end

- (Object) step_types



226
227
228
# File 'lib/dragonfly/job.rb', line 226

def step_types
  steps.map{|s| s.class.step_name }
end

- (Object) to_a



134
135
136
# File 'lib/dragonfly/job.rb', line 134

def to_a
  steps.map{|step| step.to_a }
end

- (Object) to_app

to_stuff…



183
184
185
# File 'lib/dragonfly/job.rb', line 183

def to_app
  JobEndpoint.new(self)
end

- (Object) to_fetched_job(uid)



191
192
193
194
195
196
197
# File 'lib/dragonfly/job.rb', line 191

def to_fetched_job(uid)
  new_job = dup
  new_job.steps = []
  new_job.fetch!(uid)
  new_job.next_step_index = 1
  new_job
end

- (Object) to_response(env = {"REQUEST_METHOD" => "GET"})



187
188
189
# File 'lib/dragonfly/job.rb', line 187

def to_response(env={"REQUEST_METHOD" => "GET"})
  to_app.call(env)
end

- (Object) to_unique_s

Serializing, etc.



140
141
142
# File 'lib/dragonfly/job.rb', line 140

def to_unique_s
  to_a.to_dragonfly_unique_s
end

- (Object) uid

Step inspection



201
202
203
204
# File 'lib/dragonfly/job.rb', line 201

def uid
  step = fetch_step
  step.uid if step
end

- (Object) update_url_attributes(hash)



175
176
177
178
179
# File 'lib/dragonfly/job.rb', line 175

def update_url_attributes(hash)
  hash.each do |key, value|
    url_attributes.send("#{key}=", value)
  end
end

- (Object) url(opts = {})

URLs, etc.



169
170
171
# File 'lib/dragonfly/job.rb', line 169

def url(opts={})
  app.url_for(self, opts) unless steps.empty?
end

- (Object) validate_sha!(sha)



156
157
158
159
160
161
162
163
164
165
# File 'lib/dragonfly/job.rb', line 156

def validate_sha!(sha)
  case sha
  when nil
    raise NoSHAGiven
  when self.sha
    self
  else
    raise IncorrectSHA, sha
  end
end