Class: BarnyardHarvester::Barn

Inherits:
Object
  • Object
show all
Defined in:
lib/barnyard_harvester/hash.rb,
lib/barnyard_harvester/redis.rb,
lib/barnyard_harvester/mongodb.rb,
lib/barnyard_harvester/dynamodb.rb

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Barn

Returns a new instance of Barn.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/barnyard_harvester/hash.rb', line 10

def initialize(args)

  @crop_number = args.fetch(:crop_number) { raise "You must provide :crop_number" }
  @redis_settings = args.fetch(:redis_settings) { DEFAULT_REDIS_SETTINGS }
  @debug = args.fetch(:debug) { false }
  @log = args.fetch(:logger) { Logger.new(STDOUT) }

  @my_id = "#{args[:crop_number]}-#{self.class}"
  @barn = Hash.new

  # Setup data source connection
  begin
    @barn = YAML::load_file("#{@my_id}.yml")
  rescue
  end
end

Instance Method Details

#[](primary_key) ⇒ Object



45
46
47
48
49
# File 'lib/barnyard_harvester/hash.rb', line 45

def [] primary_key
  check_key primary_key

  Crack::JSON.parse(@barn[primary_key])
end

#[]=(primary_key, object) ⇒ Object



38
39
40
41
42
43
# File 'lib/barnyard_harvester/hash.rb', line 38

def []= primary_key, object
  check_key primary_key
  check_object object

  @barn[primary_key]= object.to_json
end

#delete(primary_key) ⇒ Object

def log_run(harvester_uuid, crop_number, began_at, ended_at, source_count, change_count, add_count, delete_count)

begin
  Resque.enqueue(HarvesterLogs, Time.now, harvester_uuid, crop_number, began_at, ended_at, source_count, change_count, add_count, delete_count)
rescue Exception => e
  @log.fatal "#{self.class} Fail in Resque.enqueue of HarvesterLogs. #{e.backtrace}"
end

end



55
56
57
58
59
60
# File 'lib/barnyard_harvester/mongodb.rb', line 55

def delete(primary_key)
  check_key primary_key
  object = Crack::JSON.parse(@barn[primary_key])
  @barn.delete primary_key
  object # Return the object
end

#eachObject



61
62
63
64
65
# File 'lib/barnyard_harvester/hash.rb', line 61

def each
  @barn.each do |primary_key, value|
    yield primary_key, Crack::JSON.parse(value)
  end
end

#flushObject



67
68
69
# File 'lib/barnyard_harvester/hash.rb', line 67

def flush
  File.open("#{@my_id}.yml", "w") { |file| file.puts(@barn.to_yaml) }
end

#has_key?(primary_key) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
54
55
56
57
58
59
# File 'lib/barnyard_harvester/hash.rb', line 51

def has_key?(primary_key)
  check_key primary_key

  if @barn.has_key? primary_key
    Crack::JSON.parse(@barn[primary_key])
  else
    nil
  end
end

#log_run(harvester_uuid, crop_number, began_at, ended_at, source_count, change_count, add_count, delete_count) ⇒ Object



27
28
29
# File 'lib/barnyard_harvester/hash.rb', line 27

def log_run(harvester_uuid, crop_number, began_at, ended_at, source_count, change_count, add_count, delete_count)
  @log.debug "HARVESTER_LOG #{harvester_uuid}, #{crop_number}, #{began_at}, #{ended_at}, #{source_count}, #{change_count}, #{add_count}, #{delete_count})"
end