Class: Cloudrunpdf::Providers::Google

Inherits:
Object
  • Object
show all
Defined in:
lib/cloudrunpdf/providers/google.rb

Instance Method Summary collapse

Constructor Details

#initialize(configuration = nil) ⇒ Google

Returns a new instance of Google.



4
5
6
7
# File 'lib/cloudrunpdf/providers/google.rb', line 4

def initialize(configuration = nil)
  @configuration = configuration
  @token = token
end

Instance Method Details

#connectionObject



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/cloudrunpdf/providers/google.rb', line 30

def connection
  return unless @configuration

  Faraday::Connection.new(
    headers: {
      'Content-Type' => 'application/json',
      'Authorization' => ("Bearer #{@token}" if @configuration.google?)
    }.compact
  ) do |conn|
    conn.response :raise_error
  end
end

#tokenObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/cloudrunpdf/providers/google.rb', line 9

def token
  return unless @configuration

  if @configuration.token
    return @token ||= @configuration.token
  end

  # To be deprecated
  if !ENV['GCLOUD_AUTH_TOKEN'].nil?
    @token ||= ENV.fetch('GCLOUD_AUTH_TOKEN', nil)
  elsif Object.const_defined?(:Rails) && Rails.env.in?(['development', 'test'])
    @token ||= `gcloud auth print-identity-token`.strip
  else
    @token ||= Faraday.get(
        "http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/identity?audience=#{@configuration.cloud_function_url}&format=full",
        {},
        { metadata_flavor: 'Google' }
      ).body
  end
end