Class: Remi::Extractor::Gsheet

Inherits:
FileSystem show all
Defined in:
lib/remi/data_subjects/gsheet.rb

Overview

Contains methods shared between Salesforce Extractor/Parser/Encoder/Loader

Instance Attribute Summary collapse

Attributes inherited from FileSystem

#created_within, #group_by, #local_path, #most_recent_by, #most_recent_only, #pattern, #remote_path

Attributes inherited from Remi::Extractor

#logger

Instance Method Summary collapse

Methods inherited from FileSystem

#entries, #get_created_within, #matching_entries, #most_recent_matching_entry, #most_recent_matching_entry_in_group

Constructor Details

#initialize(*args, **kargs, &block) ⇒ Gsheet

Returns a new instance of Gsheet.



12
13
14
15
# File 'lib/remi/data_subjects/gsheet.rb', line 12

def initialize(*args, **kargs, &block)
  super
  init_gsheet_extractor(*args, **kargs)
end

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



20
21
22
# File 'lib/remi/data_subjects/gsheet.rb', line 20

def access_token
  @access_token
end

#client_idObject (readonly)

Returns the value of attribute client_id.



18
19
20
# File 'lib/remi/data_subjects/gsheet.rb', line 18

def client_id
  @client_id
end

#client_secretObject (readonly)

Returns the value of attribute client_secret.



19
20
21
# File 'lib/remi/data_subjects/gsheet.rb', line 19

def client_secret
  @client_secret
end

#dataObject (readonly)

Returns the value of attribute data.



17
18
19
# File 'lib/remi/data_subjects/gsheet.rb', line 17

def data
  @data
end

#expire_timeObject (readonly)

Returns the value of attribute expire_time.



23
24
25
# File 'lib/remi/data_subjects/gsheet.rb', line 23

def expire_time
  @expire_time
end

#ref_tokenObject (readonly)

Returns the value of attribute ref_token.



21
22
23
# File 'lib/remi/data_subjects/gsheet.rb', line 21

def ref_token
  @ref_token
end

#scopeObject (readonly)

Returns the value of attribute scope.



22
23
24
# File 'lib/remi/data_subjects/gsheet.rb', line 22

def scope
  @scope
end

Instance Method Details

#all_entriesArray<Extractor::FileSystemEntry>

Returns (Memoized) list of objects in the bucket/prefix.

Returns:



69
70
71
# File 'lib/remi/data_subjects/gsheet.rb', line 69

def all_entries
  @all_entries ||= all_entries!
end

#all_entries!Array<Extractor::FileSystemEntry>

Returns (Memoized) list of objects in the bucket/prefix.

Returns:



74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/remi/data_subjects/gsheet.rb', line 74

def all_entries!
  gsheet_entries = get_file_list(@default_folder_id)
  gsheet_entries.map do |entry|
    entry = entry.to_h
    FileSystemEntry.new(
      pathname:       File.join(@default_folder_id, entry[:name]),
      create_time:    entry[:created_time],
      modified_time:  entry[:created_time],
      raw:            entry[:id]
    )
  end
end

#authorizeObject



25
26
27
28
29
30
31
32
33
34
# File 'lib/remi/data_subjects/gsheet.rb', line 25

def authorize
  credentials = Google::Auth::UserRefreshCredentials.new(
    client_id:     @client_id,
    client_secret: @client_secret,
    scope:         @scope,
    access_token:  @access_token,
    refresh_token: @refresh_token,
    expires_at:    @expiration_time / 1000
  )
end

#extractObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/remi/data_subjects/gsheet.rb', line 53

def extract
  service                                 = Google::Apis::SheetsV4::SheetsService.new
  service.client_options.application_name = @application_name
  service.authorization                   = authorize()
  @data                                   = []

  entries.each do |file|
    logger.info "Extracting Google Sheet data from #{file.pathname}, with sheet name : #{@sheet_name}"
    response = get_spreadsheet_vals(service, file.raw, @sheet_name)
    data.push(response)
  end

  self
end

#get_file_list(folder_id) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/remi/data_subjects/gsheet.rb', line 37

def get_file_list(folder_id)
  service                                 = Google::Apis::DriveV3::DriveService.new
  service.client_options.application_name = @application_name
  service.authorization                   = authorize()
  response                                = service_list_files(service, folder_id)
  response.files
end

#get_spreadsheet_vals(service, spreadsheet_id, sheet_name = 'Sheet1') ⇒ Object



49
50
51
# File 'lib/remi/data_subjects/gsheet.rb', line 49

def get_spreadsheet_vals(service, spreadsheet_id, sheet_name = 'Sheet1')
  service.get_spreadsheet_values(spreadsheet_id, sheet_name)
end

#service_list_files(service, folder_id) ⇒ Object



45
46
47
# File 'lib/remi/data_subjects/gsheet.rb', line 45

def service_list_files(service, folder_id)
  service.list_files(q: "'#{folder_id}' in parents", page_size: 10, order_by: 'createdTime desc', fields: 'nextPageToken, files(id, name, createdTime, mimeType)')
end