Class: Takenoko::GoogleClient

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

Instance Method Summary collapse

Constructor Details

#initialize(cridential_file = nil) ⇒ GoogleClient

Returns a new instance of GoogleClient.



3
4
5
6
7
# File 'lib/takenoko/google_client.rb', line 3

def initialize(cridential_file=nil)
  if cridential_file && ::File.exist?(cridential_file)
    @cridential = JSON.parse(File.read(cridential_file)).with_indifferent_access
  end
end

Instance Method Details

#folder_by_id(folder_id) ⇒ Object



101
102
103
# File 'lib/takenoko/google_client.rb', line 101

def folder_by_id(folder_id)
  session.collection_by_id(folder_id)
end

#get_table(table_name) ⇒ Object



9
10
11
12
13
14
15
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/takenoko/google_client.rb', line 9

def get_table(table_name)
  table = Takenoko.table_config(table_name)
  raise "GoogleDrive: Sheet not found" unless sheet = session.spreadsheet_by_key(table['sheet_id'])

  if table[:worksheet_id].present?
    worksheet = sheet.worksheet_by_gid(table[:worksheet_id])
    table[:worksheet] = worksheet.title
  elsif table[:worksheet].present?
    raise "Worksheet #{table[:worksheet]} not found" unless worksheet = sheet.worksheet_by_title(table[:worksheet])
    table[:worksheet_id] = worksheet.gid.to_i
  elsif
    raise "You must specify worksheet or worksheet_id if mapping_file.yml"
  end

  update_table_config(table,worksheet.header)

  postprocess_class = if table[:enable_postprocess]
    begin
      Object.const_get(table[:postprocess_class])
    rescue NameError => e
      Rails.logger.warn e.message
      false
    end
  end

  Rails.logger.info "Getting table #{table_name}"
  rows = worksheet.populated_rows.map do |r|
    hash = HashWithIndifferentAccess.new
    table['columns_mapping'].each do |key,val|
      begin
        val = r.public_send(val)
      rescue Exception => e
        if key == 'id'
          val = r.row_num - 1
        else
          val = nil
        end
      end
      hash[key.to_sym] = format_row val
    end
    hash
  end

  if(postprocess_class)
    table[:rows] = rows.select do |row|
      if postprocess_class.respond_to? "spreadsheet_row_valid?"
        postprocess_class.public_send("spreadsheet_row_valid?",row)
      end
    end.map do |row|
      if postprocess_class.respond_to? "postprocess_spreadsheet_row"
        row = postprocess_class.public_send("postprocess_spreadsheet_row",row)
      end
      row
    end

    if postprocess_class.respond_to? "postprocess_spreadsheet_table"
      table = postprocess_class.public_send("postprocess_spreadsheet_table",table)
    end
  else
    table[:rows] = rows
  end

  return table
end

#sessionObject



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

def session
  Rails.logger.info "Init session"
  unless @cridential
    return GoogleDrive.saved_session(Takenoko.personal_cridential_file)
  end

  key = OpenSSL::PKey::RSA.new(@cridential['private_key'])
  auth = Signet::OAuth2::Client.new(
    token_credential_uri: @cridential['token_uri'],
    audience: @cridential['token_uri'],
    scope: %w(
      https://www.googleapis.com/auth/drive
      https://spreadsheets.google.com/feeds/
    ),
    issuer: @cridential['client_email'],
    signing_key: key
  )

  auth.fetch_access_token!
  session = GoogleDrive.(auth.access_token)
  return session
end

#spreadsheet(sheet_id = Takenoko.sheet_id) ⇒ Object



97
98
99
# File 'lib/takenoko/google_client.rb', line 97

def spreadsheet(sheet_id=Takenoko.sheet_id)
  session.spreadsheet_by_key(sheet_id)
end