Class: Grifter

Inherits:
Object show all
Includes:
Configuration
Defined in:
lib/grifter.rb,
lib/grifter/log.rb,
lib/grifter/helpers.rb,
lib/grifter/http_service.rb,
lib/grifter/json_helpers.rb,
lib/grifter/configuration.rb

Defined Under Namespace

Modules: Configuration, Helpers, JsonHelpers Classes: HTTPService, Log, RequestException

Constant Summary collapse

DefaultConfigOptions =
{
  #TODO: service_config: nil,
  grift_globs: ['*_grifts/**/*_grifts.rb'],
  authenticate: false,
  load_from_config_file: true,
  services: {},
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Configuration

#load_config_file, #normalize_config, #recursive_symbolize

Constructor Details

#initialize(options = {}) ⇒ Grifter

Returns a new instance of Grifter.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/grifter.rb', line 16

def initialize options={}
  options = DefaultConfigOptions.merge(options)
  @config = if options[:load_from_config_file]
              options.merge load_config_file(options)
            else
              options
            end

  #setup the services
  @services = []
  @config[:services].each_pair do |service_name, service_config|
    service = HTTPService.new(service_config)
    define_singleton_method service_name.intern do
      service
    end
    @services << service
  end

  #setup the grifter methods if any
  if @config[:grift_globs]
    @config[:grift_globs].each do |glob|
      Dir[glob].each do |grifter_file|
        load_grifter_file grifter_file
      end
    end
  end

  if @config[:authenticate]
    self.grifter_authenticate_do
  end
end

Instance Attribute Details

#servicesObject (readonly)

Returns the value of attribute services.



48
49
50
# File 'lib/grifter.rb', line 48

def services
  @services
end

Instance Method Details

#grifter_authenticate_doObject

calls all methods that end with grifter_authenticate



76
77
78
79
80
81
82
# File 'lib/grifter.rb', line 76

def grifter_authenticate_do
  auth_methods = self.singleton_methods.select { |m| m =~ /grifter_authenticate$/ }
  auth_methods.each do |m|
    Log.debug "Executing a grifter_authentication on method: #{m}"
    self.send(m)
  end
end

#grifter_configurationObject

this allows configuration to be accessed in grift scripts



51
52
53
# File 'lib/grifter.rb', line 51

def grifter_configuration
  @config.clone
end

#load_grifter_file(filename) ⇒ Object



55
56
57
58
59
60
61
62
63
# File 'lib/grifter.rb', line 55

def load_grifter_file filename
  Log.debug "Loading extension file '#{filename}'"
  #by evaling in a anonymous module, we protect this class's namespace
  anon_mod = Module.new
  with_local_load_path File.dirname(filename) do
    anon_mod.class_eval(IO.read(filename), filename, 1)
  end
  self.extend anon_mod
end

#run_script_file(filename) ⇒ Object



65
66
67
68
69
70
71
72
73
# File 'lib/grifter.rb', line 65

def run_script_file filename
  Log.info "Running data script '#{filename}'"
  raise "No such file '#{filename}'" unless File.exist? filename
  #by running in a anonymous class, we protect this class's namespace
  anon_class = BlankSlate.new(self)
  with_local_load_path File.dirname(filename) do
    anon_class.instance_eval(IO.read(filename), filename, 1)
  end
end