Class: Grifter

Inherits:
Object show all
Includes:
Configuration, Instrumentation
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,
lib/grifter/instrumentation.rb

Defined Under Namespace

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

Constant Summary

Constants included from Instrumentation

Instrumentation::InstrumentationQueueName

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Instrumentation

#metrics_all_requests, #start_instrumentation

Methods included from Configuration

#get_service_config_from_url, #load_config_file, #normalize_config, #recursive_symbolize

Constructor Details

#initialize(options = {}) ⇒ Grifter

Returns a new instance of Grifter.



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
47
48
49
50
51
52
53
# File 'lib/grifter.rb', line 21

def initialize options={}
  options = default_options.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

  start_instrumentation if @config[:instrumentation]
end

Instance Attribute Details

#servicesObject (readonly)

Returns the value of attribute services.



55
56
57
# File 'lib/grifter.rb', line 55

def services
  @services
end

Instance Method Details

#default_optionsObject



11
12
13
14
15
16
17
18
19
# File 'lib/grifter.rb', line 11

def default_options
  {
    grift_globs: ['*_grifts/**/*_grifts.rb'],
    authenticate: false,
    load_from_config_file: true,
    services: {},
    instrumentation: false,
  }
end

#grifter_authenticate_doObject

calls all methods that end with grifter_authenticate



83
84
85
86
87
88
89
# File 'lib/grifter.rb', line 83

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



58
59
60
# File 'lib/grifter.rb', line 58

def grifter_configuration
  @config.clone
end

#load_grifter_file(filename) ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'lib/grifter.rb', line 62

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



72
73
74
75
76
77
78
79
80
# File 'lib/grifter.rb', line 72

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