class Dragonfly::App

Constants

DEFAULT_NAME

Attributes

allow_legacy_urls[RW]
analysers[R]
datastore[W]
env[R]
generators[R]
job_methods[R]
name[R]
processors[R]
secret[RW]
server[R]
shell[R]

Public Class Methods

[](name) click to toggle source
# File lib/dragonfly/app.rb, line 32
def [](name)
  raise "Dragonfly::App[#{name.inspect}] is deprecated - use Dragonfly.app (for the default app) or Dragonfly.app(#{name.inspect}) (for extra named apps) instead. See docs at http://markevans.github.io/dragonfly for details"
end
apps() click to toggle source
# File lib/dragonfly/app.rb, line 36
def apps
  @apps ||= {}
end
available_datastores() click to toggle source
# File lib/dragonfly/app.rb, line 48
def available_datastores
  @available_datastores ||= {}
end
destroy_apps() click to toggle source
# File lib/dragonfly/app.rb, line 40
def destroy_apps
  apps.clear
end
instance(name=nil) click to toggle source
# File lib/dragonfly/app.rb, line 26
def instance(name=nil)
  name ||= DEFAULT_NAME
  name = name.to_sym
  apps[name] ||= new(name)
end
new(name) click to toggle source
# File lib/dragonfly/app.rb, line 54
def initialize(name)
  @name = name
  @analysers, @processors, @generators = Register.new, Register.new, Register.new
  @server = Server.new(self)
  @job_methods = Module.new
  @shell = Shell.new
  @env = {}
end
register_datastore(symbol, &block) click to toggle source
# File lib/dragonfly/app.rb, line 44
def register_datastore(symbol, &block)
  available_datastores[symbol] = block
end

Public Instance Methods

add_analyser(name, callable=nil, &block) click to toggle source
# File lib/dragonfly/app.rb, line 161
def add_analyser(name, callable=nil, &block)
  analysers.add(name, callable, &block)
  define(name){ analyse(name) }
end
add_generator(*args, &block) click to toggle source
# File lib/dragonfly/app.rb, line 143
def add_generator(*args, &block)
  generators.add(*args, &block)
end
add_mime_type(format, mime_type) click to toggle source
# File lib/dragonfly/app.rb, line 201
def add_mime_type(format, mime_type)
  mime_types[file_ext_string(format)] = mime_type
end
add_processor(name, callable=nil, &block) click to toggle source
# File lib/dragonfly/app.rb, line 151
def add_processor(name, callable=nil, &block)
  processors.add(name, callable, &block)
  define(name){|*args| process(name, *args) }
  define("#{name}!"){|*args| process!(name, *args) }
end
analyser(*args, &block) click to toggle source
# File lib/dragonfly/app.rb, line 86
def analyser(*args, &block)
  obj.add_analyser(*args, &block)
end
analyser_methods() click to toggle source
# File lib/dragonfly/app.rb, line 254
def analyser_methods
  analysers.names
end
create(content="", meta={})
Alias for: new_job
datastore(*args) click to toggle source
# File lib/dragonfly/app.rb, line 90
def datastore(*args)
  obj.use_datastore(*args)
end
define(method, &block) click to toggle source
# File lib/dragonfly/app.rb, line 193
def define(method, &block)
  job_methods.send(:define_method, method, &block)
end
define_macro(klass, name) click to toggle source
# File lib/dragonfly/app.rb, line 268
def define_macro(klass, name)
  raise NoMethodError, "define_macro is deprecated - instead of defining #{name}, just extend #{klass.name} with Dragonfly::Model and use dragonfly_accessor"
end
define_macro_on_include(mod, name) click to toggle source
# File lib/dragonfly/app.rb, line 272
def define_macro_on_include(mod, name)
  raise NoMethodError, "define_macro_on_include is deprecated - instead of defining #{name}, just extend the relevant class with Dragonfly::Model and use dragonfly_accessor"
end
define_url(&block) click to toggle source
# File lib/dragonfly/app.rb, line 227
def define_url(&block)
  @url_proc = block
end
endpoint(job=nil, &block) click to toggle source
# File lib/dragonfly/app.rb, line 177
def endpoint(job=nil, &block)
  block ? RoutedEndpoint.new(self, &block) : JobEndpoint.new(job)
end
ext_for(mime_type) click to toggle source
# File lib/dragonfly/app.rb, line 213
def ext_for(mime_type)
  return 'txt' if mime_type == 'text/plain'
  ext = key_for(mime_types, mime_type)
  ext.tr('.', '') if ext
end
fallback_mime_type() click to toggle source
# File lib/dragonfly/app.rb, line 262
def fallback_mime_type
  'application/octet-stream'
end
fetch_file_whitelist(patterns) click to toggle source
# File lib/dragonfly/app.rb, line 98
def fetch_file_whitelist(patterns)
  obj.server.add_to_fetch_file_whitelist(patterns)
end
fetch_url_whitelist(patterns) click to toggle source
# File lib/dragonfly/app.rb, line 102
def fetch_url_whitelist(patterns)
  obj.server.add_to_fetch_url_whitelist(patterns)
end
generator(*args, &block) click to toggle source
# File lib/dragonfly/app.rb, line 82
def generator(*args, &block)
  obj.add_generator(*args, &block)
end
generator_methods() click to toggle source
# File lib/dragonfly/app.rb, line 250
def generator_methods
  generators.names
end
get_analyser(name) click to toggle source
# File lib/dragonfly/app.rb, line 166
def get_analyser(name)
  analysers.get(name)
end
get_generator(name) click to toggle source
# File lib/dragonfly/app.rb, line 147
def get_generator(name)
  generators.get(name)
end
get_processor(name) click to toggle source
# File lib/dragonfly/app.rb, line 157
def get_processor(name)
  processors.get(name)
end
inspect() click to toggle source
# File lib/dragonfly/app.rb, line 258
def inspect
  "<#{self.class.name} name=#{name.inspect} >"
end
job_class() click to toggle source
# File lib/dragonfly/app.rb, line 183
def job_class
  @job_class ||= begin
    app = self
    Class.new(Job).class_eval do
      include app.job_methods
      self
    end
  end
end
method_missing(meth, *args) click to toggle source
# File lib/dragonfly/app.rb, line 115
def method_missing(meth, *args)
  raise NoMethodError, "no method '#{meth}' for App configuration - but the configuration API has changed! see docs at http://markevans.github.io/dragonfly for details"
end
mime_type(*args) click to toggle source
# File lib/dragonfly/app.rb, line 94
def mime_type(*args)
  obj.add_mime_type(*args)
end
mime_type_for(format) click to toggle source
# File lib/dragonfly/app.rb, line 209
def mime_type_for(format)
  mime_types[file_ext_string(format)] || fallback_mime_type
end
mime_types() click to toggle source
# File lib/dragonfly/app.rb, line 205
def mime_types
  @mime_types ||= Rack::Mime::MIME_TYPES.dup
end
new_job(content="", meta={}) click to toggle source
# File lib/dragonfly/app.rb, line 170
def new_job(content="", meta={})
  job_class.new(self, content, meta)
end
Also aliased as: create
processor(*args, &block) click to toggle source
# File lib/dragonfly/app.rb, line 78
def processor(*args, &block)
  obj.add_processor(*args, &block)
end
processor_methods() click to toggle source

Reflection

# File lib/dragonfly/app.rb, line 246
def processor_methods
  processors.names
end
protect_from_dos_attacks(boolean) click to toggle source
# File lib/dragonfly/app.rb, line 110
def protect_from_dos_attacks(boolean)
  verify_urls(boolean)
  Dragonfly.warn("configuration option protect_from_dos_attacks is deprecated - use verify_urls instead")
end
remote_url_for(uid, opts={}) click to toggle source
# File lib/dragonfly/app.rb, line 239
def remote_url_for(uid, opts={})
  datastore.url_for(uid, opts)
rescue NoMethodError
  raise NotImplementedError, "The datastore doesn't support serving content directly - #{datastore.inspect}"
end
response_header(key, value=nil, &block) click to toggle source
# File lib/dragonfly/app.rb, line 219
def response_header(key, value=nil, &block)
  response_headers[key] = value || block
end
response_headers() click to toggle source
# File lib/dragonfly/app.rb, line 223
def response_headers
  @response_headers ||= {}
end
store(object, meta={}, opts={}) click to toggle source
# File lib/dragonfly/app.rb, line 197
def store(object, meta={}, opts={})
  create(object, meta).store(opts)
end
url_for(job, opts={}) click to toggle source
# File lib/dragonfly/app.rb, line 231
def url_for(job, opts={})
  if @url_proc
    @url_proc.call(self, job, opts)
  else
    server.url_for(job, opts)
  end
end
use_datastore(store, *args) click to toggle source
# File lib/dragonfly/app.rb, line 130
def use_datastore(store, *args)
  self.datastore = if store.is_a?(Symbol)
    get_klass = self.class.available_datastores[store]
    raise UnregisteredDataStore, "the datastore '#{store}' is not registered" unless get_klass
    klass = get_klass.call
    klass.new(*args)
  else
    raise ArgumentError, "datastore only takes 1 argument unless you use a symbol" if args.any?
    store
  end
  raise "datastores have a new interface (read/write/destroy) - see docs at http://markevans.github.io/dragonfly for details" if datastore.respond_to?(:store) && !datastore.respond_to?(:write)
end

Private Instance Methods

file_ext_string(format) click to toggle source
# File lib/dragonfly/app.rb, line 278
def file_ext_string(format)
  '.' + format.to_s.downcase.sub(/^.*\./,'')
end
key_for(hash, value) click to toggle source
# File lib/dragonfly/app.rb, line 282
def key_for(hash, value)
  if hash.respond_to?(:key)
    hash.key(value)
  else
    hash.each {|k, v| return k if v == value }
    nil
  end
end