sheets_v4 0.3.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 27e014c65904a88e3fb4df001a8b28c64c307363b3cd658da252a951c55da992
4
- data.tar.gz: c6900f0d163abdb621a49080295ca7e59f82b89bae69d5e5729aa74aac765a1b
3
+ metadata.gz: ea8bb19ec472d573c796d277e420c088273c087aa9ffe4ebbb63df0a2ffb69ae
4
+ data.tar.gz: 5a2dca82c26a9efdd7584a5c33f4e4d9bfa1f8ac0003cf504d936554e6bb9f7d
5
5
  SHA512:
6
- metadata.gz: e60614708c5f348d05388a1bfe7b51603a92797e852aa20445d37451a493655aac69ea86fd901d500a9898a289f469d9a3780fdbe2f2756cfb80a65110641455
7
- data.tar.gz: 71ea62ff3e93faf9d9ece533ccd98f5491dea2f04d00d01c01020dd501a251a3b0b61544e0e57e85643fc0afedbfe97e92250d71b5968c63650822f0e3338433
6
+ metadata.gz: 0676d4dcff6bd008104638a1a593cfcf638ad990d15a6f1a2f71cba44e0a387e5c3e527978cb3fc9e0517ecf9ef35dd8fac2348ac993f58f28147769261d3412
7
+ data.tar.gz: ab5ae3a72384e45ee10411d1f1a89231ef9092393e686e81b0cb9e7734b216c039589697f2ef5e7d3313b677f5ea022ac8b2135adaa57e7e01c6032a12de0bb0
data/.rubocop.yml CHANGED
@@ -28,6 +28,7 @@ Metrics/ModuleLength:
28
28
 
29
29
  # When writing minitest tests, it is very hard to limit test class length:
30
30
  Metrics/ClassLength:
31
+ CountAsOne: ['hash']
31
32
  Exclude:
32
33
  - "test/**/*_test.rb"
33
34
 
data/CHANGELOG.md CHANGED
@@ -4,6 +4,25 @@ Changes for each release are listed in this file.
4
4
 
5
5
  This project adheres to [Semantic Versioning](https://semver.org/) for its releases.
6
6
 
7
+ ## v0.5.0 (2023-10-01)
8
+
9
+ [Full Changelog](https://github.com/main-branch/sheets_v4/compare/v0.4.0..v0.5.0)
10
+
11
+ Changes since v0.4.0:
12
+
13
+ * b403430 Refactor SheetsV4.validate_api_object (#15)
14
+
15
+ ## v0.4.0 (2023-09-29)
16
+
17
+ [Full Changelog](https://github.com/main-branch/sheets_v4/compare/v0.3.0..v0.4.0)
18
+
19
+ Changes since v0.3.0:
20
+
21
+ * 843c1b5 Add SheetsV4.sheets_service (#13)
22
+ * 16a73f5 Refactor ValidateApiObject (#12)
23
+ * 1525cd3 Add a link in the README to the Sheet V4 API Discover Doc (#11)
24
+ * 6d5bfb3 Improve color api and documentation (#10)
25
+
7
26
  ## v0.3.0 (2023-09-28)
8
27
 
9
28
  [Full Changelog](https://github.com/main-branch/sheets_v4/compare/v0.2.0..v0.3.0)
data/README.md CHANGED
@@ -15,6 +15,8 @@ Unofficial helpers for the Google Sheets V4 API
15
15
  * [Other Links](#other-links)
16
16
  * [Installation](#installation)
17
17
  * [Usage](#usage)
18
+ * [Obtaining an authenticated SheetsService](#obtaining-an-authenticated-sheetsservice)
19
+ * [Colors](#colors)
18
20
  * [Development](#development)
19
21
  * [Creating a Google API Service Account](#creating-a-google-api-service-account)
20
22
  * [Contributing](#contributing)
@@ -27,6 +29,7 @@ Unofficial helpers for the Google Sheets V4 API
27
29
  * [Google Sheets API Overview](https://developers.google.com/sheets/api)
28
30
  * [Google Sheets API Reference](https://developers.google.com/sheets/api/reference/rest)
29
31
  * [Batch Update Requests](https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/request)
32
+ * [Discovery Document for the Sheets API](https://sheets.googleapis.com/$discovery/rest?version=v4)
30
33
 
31
34
  ### Ruby Implementation of the Sheets API
32
35
 
@@ -55,6 +58,60 @@ gem install sheets_v4
55
58
 
56
59
  [Detailed API documenation](https://rubydoc.info/gems/sheets_v4/) is hosted on rubygems.org.
57
60
 
61
+ ### Obtaining an authenticated SheetsService
62
+
63
+ Typically, to construct an authenticated SheetsService object where the credential
64
+ is read from a file, the following code is required:
65
+
66
+ ```Ruby
67
+ sheets_service = Google::Apis::SheetsV4::SheetsService.new
68
+ credential = File.read(File.expand_path('~/google-api-credential.json')) do |credential_source|
69
+ scopes = Google::Apis::SheetsV4::AUTH_SPREADSHEETS
70
+ options = { json_key_io: credential_source, scope: scopes }
71
+ Google::Auth::DefaultCredentials.make_creds(options).tap(&:fetch_access_token)
72
+ end
73
+ sheets_service.authorization = credential
74
+ ```
75
+
76
+ The `SheetsV4.sheets_service` method simplifies this a bit.
77
+
78
+ By default, the credential is read from `~/.google-api-credential.json`. In that case,
79
+ an authenticated SheetsService object can be obtained with one method call:
80
+
81
+ ```Ruby
82
+ sheets_service = SheetsV4.sheets_service
83
+ ```
84
+
85
+ If the credential is stored elsewhere, pass the credential_source to `SheetsV4.sheets_service`
86
+ manually. `credential_source` can be a String:
87
+
88
+ ```Ruby
89
+ sheets_service = SheetsV4.sheets_service(credential_sourvce: File.read('credential.json'))
90
+ ```
91
+
92
+ an IO object:
93
+
94
+ ```Ruby
95
+ sheets_service = File.open('credential.json') do |credential_source|
96
+ SheetsV4.sheets_service(credential_sourvce:)
97
+ end
98
+ ```
99
+
100
+ or an already constructed `Google::Auth::*`` object.
101
+
102
+ ### Colors
103
+
104
+ Color objects (with appropriate :red, :green, :blue values) can be retrieved by name
105
+ using `SheetsV4.color(:black)` or `SheetsV4::Color.black` (these are equivalent).
106
+
107
+ Color names can be listed using `SheetsV4.color_names`.
108
+
109
+ The color object returned is a Hash as follows:
110
+
111
+ ```Ruby
112
+ SheetsV4::Color.black #=> {:red=>0.0, :green=>0.0, :blue=>0.0}
113
+ ```
114
+
58
115
  ## Development
59
116
 
60
117
  After checking out the repo, run `bin/setup` to install dependencies. Then, run
data/examples/README.md CHANGED
@@ -3,6 +3,7 @@
3
3
  * [ ] Creating a Google API service account [1](https://www.youtube.com/watch?v=sAgWCbGMzTo&list=PL3JVwFmb_BnSee8RFaRPZ3nykuMRlaQp1&index=1)
4
4
  [2](https://www.youtube.com/watch?v=sVURhxyc6jE&list=PL3JVwFmb_BnSee8RFaRPZ3nykuMRlaQp1&index=43)
5
5
  * [ ] Create a SheetsService instance
6
+ * [X] Set background color
6
7
  * [ ] Creating Google Sheets files [1](https://www.youtube.com/watch?v=JRUxeQ6ZCy0&list=PL3JVwFmb_BnSee8RFaRPZ3nykuMRlaQp1&index=2)
7
8
  * [ ] Writing data to a sheet [1](https://www.youtube.com/watch?v=YF7Ad-7pvks&list=PL3JVwFmb_BnSee8RFaRPZ3nykuMRlaQp1&index=3)
8
9
  * [ ] Reading data from a sheet [1](https://www.youtube.com/watch?v=gkglr8GID5E&list=PL3JVwFmb_BnSee8RFaRPZ3nykuMRlaQp1&index=4)
@@ -0,0 +1,54 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'sheets_v4'
5
+ require 'googleauth'
6
+
7
+ # Example to show using the SheetsV4 module to set the background color of a cell
8
+ #
9
+ # GIVEN the credential file is at ~/.google-api-credential.json
10
+ # AND the spreadsheet id is 18FAcgotK7nDfLTOTQuGCIjKwxkJMAguhn1OVzpFFgWY
11
+ # AND the spreadsheet has a sheet whose id is 0
12
+ # WHEN the script is run
13
+ # THEN the sheet whose id is 0 will have the background color names starting at A2
14
+ # AND the background color of the cells in column B will be set to the color matching the color name in column A
15
+
16
+ # Build the requests
17
+
18
+ def name_rows
19
+ SheetsV4.color_names.map do |color_name|
20
+ { values: [{ user_entered_value: { string_value: color_name.to_s } }] }
21
+ end
22
+ end
23
+
24
+ def write_names
25
+ rows = name_rows
26
+ fields = 'user_entered_value'
27
+ start = { sheet_id: 0, row_index: 1, column_index: 0 }
28
+ { update_cells: { rows:, fields:, start: } }
29
+ end
30
+
31
+ def background_color_rows
32
+ SheetsV4.color_names.map { |color_name| SheetsV4.color(color_name) }.map do |color|
33
+ { values: [{ user_entered_format: { background_color: color } }] }
34
+ end
35
+ end
36
+
37
+ def set_background_colors
38
+ rows = background_color_rows
39
+ fields = 'user_entered_format'
40
+ start = { sheet_id: 0, row_index: 1, column_index: 1 }
41
+ { update_cells: { rows:, fields:, start: } }
42
+ end
43
+
44
+ def requests = { requests: [write_names, set_background_colors] }
45
+
46
+ # OPTIONAL: validate the requests against the schema before sending them to the API.
47
+ #
48
+ # While not necessary, it can be helpful to identify errors since the API will return
49
+ # an error if the request is invalid but does not tell you where the problem is.
50
+ #
51
+ SheetsV4.validate_api_object(schema_name: 'batch_update_spreadsheet_request', object: requests)
52
+
53
+ spreadsheet_id = '18FAcgotK7nDfLTOTQuGCIjKwxkJMAguhn1OVzpFFgWY'
54
+ SheetsV4.sheets_service.batch_update_spreadsheet(spreadsheet_id, requests)
@@ -0,0 +1,67 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'sheets_v4'
5
+ require 'googleauth'
6
+
7
+ # Example to show using the SheetsV4 module to set the background color of a cell
8
+ #
9
+ # GIVEN the credential file is at ~/.google-api-credential.json
10
+ # AND the spreadsheet id is 18FAcgotK7nDfLTOTQuGCIjKwxkJMAguhn1OVzpFFgWY
11
+ # AND the spreadsheet has a sheet whose id is 0
12
+ # WHEN the script is run
13
+ # THEN the sheet whose id is 0 will have the background color names starting at A2
14
+ # AND the background color of the cells in column B will be set to the color matching the color name in column A
15
+
16
+ # Build the requests
17
+
18
+ def name_rows
19
+ SheetsV4.color_names.map do |color_name|
20
+ Google::Apis::SheetsV4::RowData.new(
21
+ values: [
22
+ Google::Apis::SheetsV4::CellData.new(
23
+ user_entered_value: Google::Apis::SheetsV4::ExtendedValue.new(string_value: color_name.to_s)
24
+ )
25
+ ]
26
+ )
27
+ # { values: [{ user_entered_value: { string_value: color_name.to_s } }] }
28
+ end
29
+ end
30
+
31
+ def write_names
32
+ rows = name_rows
33
+ fields = 'user_entered_value'
34
+ start = Google::Apis::SheetsV4::GridCoordinate.new(sheet_id: 0, row_index: 1, column_index: 0)
35
+ Google::Apis::SheetsV4::Request.new(
36
+ update_cells: Google::Apis::SheetsV4::UpdateCellsRequest.new(rows:, fields:, start:)
37
+ )
38
+ end
39
+
40
+ def background_color_rows
41
+ SheetsV4.color_names.map { |color_name| SheetsV4.color(color_name) }.map do |color|
42
+ background_color = Google::Apis::SheetsV4::Color.new(**color)
43
+ user_entered_format = Google::Apis::SheetsV4::CellFormat.new(background_color:)
44
+ cell_data = Google::Apis::SheetsV4::CellData.new(user_entered_format:)
45
+ Google::Apis::SheetsV4::RowData.new(values: [cell_data])
46
+ end
47
+ end
48
+
49
+ def set_background_colors
50
+ rows = background_color_rows
51
+ fields = 'user_entered_format'
52
+ start = Google::Apis::SheetsV4::GridCoordinate.new(sheet_id: 0, row_index: 1, column_index: 1)
53
+ update_cells = Google::Apis::SheetsV4::UpdateCellsRequest.new(rows:, fields:, start:)
54
+ Google::Apis::SheetsV4::Request.new(update_cells:)
55
+ end
56
+
57
+ def requests = Google::Apis::SheetsV4::BatchUpdateSpreadsheetRequest.new(requests: [write_names, set_background_colors])
58
+
59
+ # OPTIONAL: validate the requests against the schema before sending them to the API.
60
+ #
61
+ # While not necessary, it can be helpful to identify errors since the API will return
62
+ # an error if the request is invalid but does not tell you where the problem is.
63
+ #
64
+ SheetsV4.validate_api_object(schema_name: 'batch_update_spreadsheet_request', object: requests.to_h)
65
+
66
+ spreadsheet_id = '18FAcgotK7nDfLTOTQuGCIjKwxkJMAguhn1OVzpFFgWY'
67
+ SheetsV4.sheets_service.batch_update_spreadsheet(spreadsheet_id, requests)
@@ -3,30 +3,195 @@
3
3
  require 'json_schemer'
4
4
 
5
5
  module SheetsV4
6
- # Predefined color objects
6
+ # Returns predefined color objects by name
7
+ #
8
+ # A method is implemented for each color name. The list of available colors is
9
+ # given by `SheetsV4.color_names`.
10
+ #
11
+ # @example
12
+ # # Get a color object by name:
13
+ # SheetsV4::Color.black #=> { "red" => 0.0, "green" => 0.0, "blue" => 0.0 }
14
+ #
15
+ # @see SheetsV4.color a method that returns a color object by name
16
+ # @see SheetsV4.color_names an array of predefined color names
17
+ #
7
18
  # @api public
19
+ #
8
20
  class Color
9
21
  class << self
10
- # Return a color object for the given name from SheetsV4::COLORS or call super
22
+ # Return a color object for the given name from SheetsV4.color_names or call super
11
23
  #
12
- # @param method_name [Symbol] the name of the color
24
+ # @param method_name [#to_sym] the name of the color
13
25
  # @param arguments [Array] ignored
14
26
  # @param block [Proc] ignored
15
27
  # @return [Hash] the color object
16
28
  # @api private
17
29
  def method_missing(method_name, *arguments, &)
18
- SheetsV4::COLORS[method_name.to_sym] || super
30
+ COLORS[method_name.to_sym] || super
19
31
  end
20
32
 
21
33
  # Return true if the given method name is a color name or call super
22
34
  #
23
- # @param method_name [Symbol] the name of the color
35
+ # @param method_name [#to_sym] the name of the color
24
36
  # @param include_private [Boolean] ignored
25
37
  # @return [Boolean] true if the method name is a color name
26
38
  # @api private
27
39
  def respond_to_missing?(method_name, include_private = false)
28
- SheetsV4::COLORS.key?(method_name.to_sym) || super
40
+ COLORS.key?(method_name.to_sym) || super
29
41
  end
30
42
  end
43
+
44
+ # Colors to use in the Google Sheets API
45
+ COLORS = {
46
+ # Standard Google Sheets colors
47
+ #
48
+ black: { red: 0.00000000000, green: 0.00000000000, blue: 0.00000000000 },
49
+ dark_gray4: { red: 0.26274509804, green: 0.26274509804, blue: 0.26274509804 },
50
+ dark_gray3: { red: 0.40000000000, green: 0.40000000000, blue: 0.40000000000 },
51
+ dark_gray2: { red: 0.60000000000, green: 0.60000000000, blue: 0.60000000000 },
52
+ dark_gray1: { red: 0.71764705882, green: 0.71764705882, blue: 0.71764705882 },
53
+ gray: { red: 0.80000000000, green: 0.80000000000, blue: 0.80000000000 },
54
+ light_gray1: { red: 0.85098039216, green: 0.85098039216, blue: 0.85098039216 },
55
+ light_gray2: { red: 0.93725490196, green: 0.93725490196, blue: 0.93725490196 },
56
+ light_gray3: { red: 0.95294117647, green: 0.95294117647, blue: 0.95294117647 },
57
+ white: { red: 1.00000000000, green: 1.00000000000, blue: 1.00000000000 },
58
+ red_berry: { red: 0.59607843137, green: 0.00000000000, blue: 0.00000000000 },
59
+ red: { red: 1.00000000000, green: 0.00000000000, blue: 0.00000000000 },
60
+ orange: { red: 1.00000000000, green: 0.60000000000, blue: 0.00000000000 },
61
+ yellow: { red: 1.00000000000, green: 1.00000000000, blue: 0.00000000000 },
62
+ green: { red: 0.00000000000, green: 1.00000000000, blue: 0.00000000000 },
63
+ cyan: { red: 0.00000000000, green: 1.00000000000, blue: 1.00000000000 },
64
+ cornflower_blue: { red: 0.29019607843, green: 0.52549019608, blue: 0.90980392157 },
65
+ blue: { red: 0.00000000000, green: 0.00000000000, blue: 1.00000000000 },
66
+ purple: { red: 0.60000000000, green: 0.00000000000, blue: 1.00000000000 },
67
+ magenta: { red: 1.00000000000, green: 0.00000000000, blue: 1.00000000000 },
68
+ light_red_berry3: { red: 0.90196078431, green: 0.72156862745, blue: 0.68627450980 },
69
+ light_red3: { red: 0.95686274510, green: 0.80000000000, blue: 0.80000000000 },
70
+ light_orange3: { red: 0.98823529412, green: 0.89803921569, blue: 0.80392156863 },
71
+ light_yellow3: { red: 1.00000000000, green: 0.94901960784, blue: 0.80000000000 },
72
+ light_green3: { red: 0.85098039216, green: 0.91764705882, blue: 0.82745098039 },
73
+ light_cyan3: { red: 0.81568627451, green: 0.87843137255, blue: 0.89019607843 },
74
+ light_cornflower_blue3: { red: 0.78823529412, green: 0.85490196078, blue: 0.97254901961 },
75
+ light_blue3: { red: 0.81176470588, green: 0.88627450980, blue: 0.95294117647 },
76
+ light_purple3: { red: 0.85098039216, green: 0.82352941176, blue: 0.91372549020 },
77
+ light_magenta3: { red: 0.91764705882, green: 0.81960784314, blue: 0.86274509804 },
78
+ light_red_berry2: { red: 0.86666666667, green: 0.49411764706, blue: 0.41960784314 },
79
+ light_red2: { red: 0.91764705882, green: 0.60000000000, blue: 0.60000000000 },
80
+ light_orange2: { red: 0.97647058824, green: 0.79607843137, blue: 0.61176470588 },
81
+ light_yellow2: { red: 1.00000000000, green: 0.89803921569, blue: 0.60000000000 },
82
+ light_green2: { red: 0.71372549020, green: 0.84313725490, blue: 0.65882352941 },
83
+ light_cyan2: { red: 0.63529411765, green: 0.76862745098, blue: 0.78823529412 },
84
+ light_cornflower_blue2: { red: 0.64313725490, green: 0.76078431373, blue: 0.95686274510 },
85
+ light_blue2: { red: 0.62352941176, green: 0.77254901961, blue: 0.90980392157 },
86
+ light_purple2: { red: 0.70588235294, green: 0.65490196078, blue: 0.83921568627 },
87
+ light_magenta2: { red: 0.83529411765, green: 0.65098039216, blue: 0.74117647059 },
88
+ light_red_berry1: { red: 0.80000000000, green: 0.25490196078, blue: 0.14509803922 },
89
+ light_red1: { red: 0.87843137255, green: 0.40000000000, blue: 0.40000000000 },
90
+ light_orange1: { red: 0.96470588235, green: 0.69803921569, blue: 0.41960784314 },
91
+ light_yellow1: { red: 1.00000000000, green: 0.85098039216, blue: 0.40000000000 },
92
+ light_green1: { red: 0.57647058824, green: 0.76862745098, blue: 0.49019607843 },
93
+ light_cyan1: { red: 0.46274509804, green: 0.64705882353, blue: 0.68627450980 },
94
+ light_cornflower_blue1: { red: 0.42745098039, green: 0.61960784314, blue: 0.92156862745 },
95
+ light_blue1: { red: 0.43529411765, green: 0.65882352941, blue: 0.86274509804 },
96
+ light_purple1: { red: 0.55686274510, green: 0.48627450980, blue: 0.76470588235 },
97
+ light_magenta1: { red: 0.76078431373, green: 0.48235294118, blue: 0.62745098039 },
98
+ dark_red_berry1: { red: 0.65098039216, green: 0.10980392157, blue: 0.00000000000 },
99
+ dark_red1: { red: 0.80000000000, green: 0.00000000000, blue: 0.00000000000 },
100
+ dark_orange1: { red: 0.90196078431, green: 0.56862745098, blue: 0.21960784314 },
101
+ dark_yellow1: { red: 0.94509803922, green: 0.76078431373, blue: 0.19607843137 },
102
+ dark_green1: { red: 0.41568627451, green: 0.65882352941, blue: 0.30980392157 },
103
+ dark_cyan1: { red: 0.27058823529, green: 0.50588235294, blue: 0.55686274510 },
104
+ dark_cornflower_blue1: { red: 0.23529411765, green: 0.47058823529, blue: 0.84705882353 },
105
+ dark_blue1: { red: 0.23921568627, green: 0.52156862745, blue: 0.77647058824 },
106
+ dark_purple1: { red: 0.40392156863, green: 0.30588235294, blue: 0.65490196078 },
107
+ dark_magenta1: { red: 0.65098039216, green: 0.30196078431, blue: 0.47450980392 },
108
+ dark_red_berry2: { red: 0.52156862745, green: 0.12549019608, blue: 0.04705882353 },
109
+ dark_red2: { red: 0.60000000000, green: 0.00000000000, blue: 0.00000000000 },
110
+ dark_orange2: { red: 0.70588235294, green: 0.37254901961, blue: 0.02352941176 },
111
+ dark_yellow2: { red: 0.74901960784, green: 0.56470588235, blue: 0.00000000000 },
112
+ dark_green2: { red: 0.21960784314, green: 0.46274509804, blue: 0.11372549020 },
113
+ dark_cyan2: { red: 0.07450980392, green: 0.30980392157, blue: 0.36078431373 },
114
+ dark_cornflower_blue2: { red: 0.06666666667, green: 0.33333333333, blue: 0.80000000000 },
115
+ dark_blue2: { red: 0.04313725490, green: 0.32549019608, blue: 0.58039215686 },
116
+ dark_purple2: { red: 0.20784313725, green: 0.10980392157, blue: 0.45882352941 },
117
+ dark_magenta2: { red: 0.45490196078, green: 0.10588235294, blue: 0.27843137255 },
118
+ dark_red_berry3: { red: 0.35686274510, green: 0.05882352941, blue: 0.00000000000 },
119
+ dark_red3: { red: 0.40000000000, green: 0.00000000000, blue: 0.00000000000 },
120
+ dark_orange3: { red: 0.47058823529, green: 0.24705882353, blue: 0.01568627451 },
121
+ dark_yellow3: { red: 0.49803921569, green: 0.37647058824, blue: 0.00000000000 },
122
+ dark_green3: { red: 0.15294117647, green: 0.30588235294, blue: 0.07450980392 },
123
+ darn_cyan3: { red: 0.04705882353, green: 0.20392156863, blue: 0.23921568627 },
124
+ dark_cornflower_blue3: { red: 0.10980392157, green: 0.27058823529, blue: 0.52941176471 },
125
+ dark_blue3: { red: 0.02745098039, green: 0.21568627451, blue: 0.38823529412 },
126
+ dark_purple3: { red: 0.12549019608, green: 0.07058823529, blue: 0.30196078431 },
127
+ dark_magenta3: { red: 0.29803921569, green: 0.06666666667, blue: 0.18823529412 },
128
+
129
+ # Yahoo brand colors
130
+ #
131
+ grape_jelly: { red: 0.37647058824, green: 0.00392156863, blue: 0.82352941176 },
132
+ hulk_pants: { red: 0.49411764706, green: 0.12156862745, blue: 1.00000000000 },
133
+ malbec: { red: 0.22352941176, green: 0.00000000000, blue: 0.49019607843 },
134
+ tumeric: { red: 1.00000000000, green: 0.65490196078, blue: 0.00000000000 },
135
+ mulah: { red: 0.10196078431, green: 0.77254901961, blue: 0.40392156863 },
136
+ dory: { red: 0.05882352941, green: 0.41176470588, blue: 1.00000000000 },
137
+ malibu: { red: 1.00000000000, green: 0.00000000000, blue: 0.50196078431 },
138
+ sea_foam: { red: 0.06666666667, green: 0.82745098039, blue: 0.80392156863 },
139
+ tumeric_tint: { red: 0.98039215686, green: 0.86666666667, blue: 0.69411764706 },
140
+ mulah_tint: { red: 0.73333333333, green: 0.90196078431, blue: 0.77647058824 },
141
+ dory_tint: { red: 0.66274509804, green: 0.77254901961, blue: 0.98431372549 },
142
+ malibu_tint: { red: 0.96862745098, green: 0.68235294118, blue: 0.80000000000 },
143
+ sea_foam_tint: { red: 0.74901960784, green: 0.92549019608, blue: 0.92156862745 },
144
+
145
+ # Yahoo health colors
146
+ #
147
+ health_green: { red: 0.00000000000, green: 0.69019607843, blue: 0.31372549020 },
148
+ health_yellow: { red: 1.00000000000, green: 0.65490196078, blue: 0.00000000000 },
149
+ health_red: { red: 1.00000000000, green: 0.00000000000, blue: 0.00000000000 },
150
+
151
+ # Yahoo Fuji design color palette
152
+ #
153
+ fuji_color_watermelon: { red: 1.00000000000, green: 0.32156862745, blue: 0.34117647059 },
154
+ fuji_color_solo_cup: { red: 0.92156862745, green: 0.05882352941, blue: 0.16078431373 },
155
+ fuji_color_malibu: { red: 1.00000000000, green: 0.00000000000, blue: 0.50196078431 },
156
+ fuji_color_barney: { red: 0.80000000000, green: 0.00000000000, blue: 0.54901960784 },
157
+ fuji_color_mimosa: { red: 1.00000000000, green: 0.82745098039, blue: 0.20000000000 },
158
+ fuji_color_turmeric: { red: 1.00000000000, green: 0.65490196078, blue: 0.00000000000 },
159
+ fuji_color_cheetos: { red: 0.99215686275, green: 0.38039215686, blue: 0.00000000000 },
160
+ fuji_color_carrot_juice: { red: 1.00000000000, green: 0.32156862745, blue: 0.05098039216 },
161
+ fuji_color_mulah: { red: 0.10196078431, green: 0.77254901961, blue: 0.40392156863 },
162
+ fuji_color_bonsai: { red: 0.00000000000, green: 0.52941176471, blue: 0.31764705882 },
163
+ fuji_color_spirulina: { red: 0.00000000000, green: 0.62745098039, blue: 0.62745098039 },
164
+ fuji_color_sea_foam: { red: 0.06666666667, green: 0.82745098039, blue: 0.80392156863 },
165
+ fuji_color_peeps: { red: 0.49019607843, green: 0.79607843137, blue: 1.00000000000 },
166
+ fuji_color_sky: { red: 0.07058823529, green: 0.66274509804, blue: 1.00000000000 },
167
+ fuji_color_dory: { red: 0.05882352941, green: 0.41176470588, blue: 1.00000000000 },
168
+ fuji_color_scooter: { red: 0.00000000000, green: 0.38823529412, blue: 0.92156862745 },
169
+ fuji_color_cobalt: { red: 0.00000000000, green: 0.22745098039, blue: 0.73725490196 },
170
+ fuji_color_denim: { red: 0.10196078431, green: 0.05098039216, blue: 0.67058823529 },
171
+ fuji_color_blurple: { red: 0.36470588235, green: 0.36862745098, blue: 1.00000000000 },
172
+ fuji_color_hendrix: { red: 0.97254901961, green: 0.95686274510, blue: 1.00000000000 },
173
+ fuji_color_thanos: { red: 0.56470588235, green: 0.48627450980, blue: 1.00000000000 },
174
+ fuji_color_starfish: { red: 0.46666666667, green: 0.34901960784, blue: 1.00000000000 },
175
+ fuji_color_hulk_pants: { red: 0.49411764706, green: 0.12156862745, blue: 1.00000000000 },
176
+ fuji_color_grape_jelly: { red: 0.37647058824, green: 0.00392156863, blue: 0.82352941176 },
177
+ fuji_color_mulberry: { red: 0.31372549020, green: 0.08235294118, blue: 0.69019607843 },
178
+ fuji_color_malbec: { red: 0.22352941176, green: 0.00000000000, blue: 0.49019607843 },
179
+ fuji_grayscale_black: { red: 0.00000000000, green: 0.00000000000, blue: 0.00000000000 },
180
+ fuji_grayscale_midnight: { red: 0.06274509804, green: 0.08235294118, blue: 0.09411764706 },
181
+ fuji_grayscale_inkwell: { red: 0.11372549020, green: 0.13333333333, blue: 0.15686274510 },
182
+ fuji_grayscale_batcave: { red: 0.13725490196, green: 0.16470588235, blue: 0.19215686275 },
183
+ fuji_grayscale_ramones: { red: 0.17254901961, green: 0.21176470588, blue: 0.24705882353 },
184
+ fuji_grayscale_charcoal: { red: 0.27450980392, green: 0.30588235294, blue: 0.33725490196 },
185
+ fuji_grayscale_battleship: { red: 0.35686274510, green: 0.38823529412, blue: 0.41568627451 },
186
+ fuji_grayscale_dolphin: { red: 0.43137254902, green: 0.46666666667, blue: 0.50196078431 },
187
+ fuji_grayscale_shark: { red: 0.50980392157, green: 0.54117647059, blue: 0.57647058824 },
188
+ fuji_grayscale_gandalf: { red: 0.59215686275, green: 0.61960784314, blue: 0.65882352941 },
189
+ fuji_grayscale_bob: { red: 0.69019607843, green: 0.72549019608, blue: 0.75686274510 },
190
+ fuji_grayscale_pebble: { red: 0.78039215686, green: 0.80392156863, blue: 0.82352941176 },
191
+ fuji_grayscale_dirty_seagull: { red: 0.87843137255, green: 0.89411764706, blue: 0.91372549020 },
192
+ fuji_grayscale_grey_hair: { red: 0.94117647059, green: 0.95294117647, blue: 0.96078431373 },
193
+ fuji_grayscale_marshmallow: { red: 0.96078431373, green: 0.97254901961, blue: 0.98039215686 },
194
+ fuji_grayscale_white: { red: 1.00000000000, green: 1.00000000000, blue: 1.00000000000 }
195
+ }.freeze.each_value(&:freeze)
31
196
  end
32
197
  end
@@ -0,0 +1,93 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json_schemer'
4
+
5
+ module SheetsV4
6
+ # Creates a Google API credential with an access token
7
+ #
8
+ class CredentialCreator
9
+ # Creates a Google API credential with an access token
10
+ #
11
+ # This wraps the boiler plate code into one function to make constructing a
12
+ # credential easy and less error prone.
13
+ #
14
+ # @example Constructing a credential from the contents of ~/.credential
15
+ # credential_source = File.read(File.join(Dir.home, '.credential'))
16
+ # scope = Google::Apis::SheetsV4::AUTH_SPREADSHEETS
17
+ # credential = GoogleApisHelpers.credential(credential_source, scope)
18
+ #
19
+ # @param [Google::Auth::*, String, IO, nil] credential_source may be one of four things:
20
+ # (1) a previously created credential that you want to reuse, (2) a credential read
21
+ # into a string, (3) an IO object with the credential ready to be read, or (4)
22
+ # if nill, the credential is read from ~/.google-api-credential.json
23
+ # @param scopes [Object, Array] one or more scopes to access.
24
+ # @param credential_factory [#make_creds] Used inject the credential_factory for tests
25
+ #
26
+ # @return [Object] a credential object with an access token
27
+ #
28
+ def self.call(
29
+ credential_source, scopes, credential_factory = Google::Auth::DefaultCredentials
30
+ )
31
+ return credential_source if credential?(credential_source)
32
+
33
+ credential_source ||= default_credential_source
34
+ options = make_creds_options(credential_source, scopes)
35
+ credential_factory.make_creds(options).tap(&:fetch_access_token)
36
+ end
37
+
38
+ private
39
+
40
+ # Reads credential source from `~/.google-api-credential.json`
41
+ #
42
+ # @return [String] the credential as a string
43
+ #
44
+ # @api private
45
+ #
46
+ private_class_method def self.default_credential_source
47
+ File.read(File.expand_path('~/.google-api-credential.json'))
48
+ end
49
+
50
+ # Constructs creds_options needed to create a credential
51
+ #
52
+ # @param [Google::Auth::*, String, #read] credential_source a credential (which
53
+ # is an object whose class is in the Google::Auth module), a String containing
54
+ # the credential, or a IO object with the credential ready to be read.
55
+ #
56
+ # @return [Hash] returns the cred_options
57
+ #
58
+ # @api private
59
+ #
60
+ private_class_method def self.make_creds_options(credential_source, scopes)
61
+ { json_key_io: to_io(credential_source), scope: scopes }
62
+ end
63
+
64
+ # Wraps a credential_source that is a string in a StringIO
65
+ #
66
+ # @param [Google::Auth::*, String, #read] credential_source a credential (which
67
+ # is an object whose class is in the Google::Auth module), a String containing
68
+ # the credential, or a IO object with the credential ready to be read.
69
+ #
70
+ # @return [IO, StringIO] returns a StringIO object is a String was passed in.
71
+ #
72
+ # @api private
73
+ #
74
+ private_class_method def self.to_io(credential_source)
75
+ credential_source.is_a?(String) ? StringIO.new(credential_source) : credential_source
76
+ end
77
+
78
+ # Determines if a credential_source is already a credential
79
+ #
80
+ # @param [Google::Auth::*, String, #read] credential_source a credential (which
81
+ # is an object whose class is in the Google::Auth module), a String containing
82
+ # the credential, or a IO object with the credential ready to be read.
83
+ #
84
+ # @return [Boolean] true if the credential source is an object whose class is in the
85
+ # Google::Auth module.
86
+ #
87
+ # @api private
88
+ #
89
+ private_class_method def self.credential?(credential_source)
90
+ credential_source.class.name.start_with?('Google::Auth::')
91
+ end
92
+ end
93
+ end