Class: StorehouseClient::API

Inherits:
Object
  • Object
show all
Defined in:
lib/storehouse_client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ API

Returns a new instance of API.



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/storehouse_client.rb', line 17

def initialize(args)

  @error       = nil
  @url         = args.fetch(:url) { raise 'You must provide :url' }
  @api_version = args.fetch(:api_version) { 1 }
  @auth_token  = args.fetch(:auth_token) { raise 'You must provide :auth_token' }

  @record_count = 0
  @duration     = nil

  @resource = RestClient::Resource.new @url

end

Instance Attribute Details

#data_source_idObject (readonly)

Returns the value of attribute data_source_id.



15
16
17
# File 'lib/storehouse_client.rb', line 15

def data_source_id
  @data_source_id
end

#durationObject (readonly)

Returns the value of attribute duration.



15
16
17
# File 'lib/storehouse_client.rb', line 15

def duration
  @duration
end

#errorObject (readonly)

Returns the value of attribute error.



15
16
17
# File 'lib/storehouse_client.rb', line 15

def error
  @error
end

#record_countObject (readonly)

Returns the value of attribute record_count.



15
16
17
# File 'lib/storehouse_client.rb', line 15

def record_count
  @record_count
end

#run_idObject (readonly)

Returns the value of attribute run_id.



15
16
17
# File 'lib/storehouse_client.rb', line 15

def run_id
  @run_id
end

Instance Method Details

#add_record(primary_key, data) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/storehouse_client.rb', line 73

def add_record(primary_key, data)

  begin
    e                                                 = {}
    e['export_record']                                = {}
    e['export_record']['blob_attributes']             = {}
    e['export_record']['blob_attributes']['data']     = data
    e['export_record']['blob_attributes']['checksum'] = Digest::SHA256.hexdigest(data.to_json)
    e['export_record']['record_size']                 = data.to_json.length
    e['export_record']['checksum']                    = e['export_record']['blob_attributes']['checksum']
    e['export_record']['primary_key']                 = primary_key.to_s # Ensure primary key is a string
    e['export_record']['data_source_id']              = @data_source_id
    e['export_record']['export_run_id']               = @run_id

    post("api/#{@api_version}/export_records/?auth_token=#{@auth_token}", e)

  rescue Exception => @error
    return nil
  end

  @record_count += 1

  self
end

#data_sourcesObject



35
36
37
# File 'lib/storehouse_client.rb', line 35

def data_sources
  get ("data_sources.json?auth_token=#{@auth_token}")
end

#error?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/storehouse_client.rb', line 31

def error?
  !@error.nil?
end

#finish_runObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/storehouse_client.rb', line 57

def finish_run

  run                               = {}
  run['export_run']                 = {}
  run['export_run']['finished_at']  = Time.now.to_s
  run['export_run']['record_count'] = @record_count

  r = put("api/#{@api_version}/export_runs/#{@run_id}?auth_token=#{@auth_token}", run)

  return nil if error?

  @duration = r['response']['duration']

  self
end

#start_run(data_source_id) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/storehouse_client.rb', line 39

def start_run(data_source_id)

  @data_source_id = data_source_id

  run                                 = {}
  run['export_run']                   = {}
  run['export_run']['data_source_id'] = @data_source_id
  run['export_run']['started_at']     = Time.now

  r = post("api/#{@api_version}/export_runs?auth_token=#{@auth_token}", run)

  return nil if error?

  @run_id = r['response']['id']

  self
end