Class: GoogleCustomSearch

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

Instance Method Summary collapse

Instance Method Details

#make_authorizerObject



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

def make_authorizer
  sa_key = ENV.fetch("GOOGLE_SA_PRIVATE_KEY")
  key = ::OpenSSL::PKey::RSA.new(sa_key)
  cred = ::Signet::OAuth2::Client.new(
    token_credential_uri: "https://oauth2.googleapis.com/token",
    audience: "https://oauth2.googleapis.com/token",
    scope: %w[
      https://www.googleapis.com/auth/cse
    ],
    issuer: ENV.fetch("GOOGLE_SA_CLIENT_EMAIL"),
    signing_key: key
  )
  cred.configure_connection({})
end

#search(query, args = {}) ⇒ Object



2
3
4
# File 'lib/google_custom_search.rb', line 2

def search(query, args = {})
  service.list_cses(cx: ENV['GOOGLE_CUSTOM_SEARCH_CSE_ID'], q: query, **args)
end

#serviceObject



6
7
8
9
10
11
12
13
14
# File 'lib/google_custom_search.rb', line 6

def service
  @service ||= begin
    service = Google::Apis::CustomsearchV1::CustomSearchAPIService.new
    authorizer = make_authorizer
    authorizer.fetch_access_token!
    service.authorization = authorizer
    service
  end
end