Class: Google::AMP::Cache::Client
- Inherits:
-
Object
- Object
- Google::AMP::Cache::Client
- Defined in:
- lib/google/amp/cache/client.rb
Constant Summary collapse
- UPDATE_CACHE_API_DOMAIN_SUFFIX =
'cdn.ampproject.org'- DIGEST =
OpenSSL::Digest::SHA256.new
Instance Attribute Summary collapse
-
#google_api_key ⇒ Object
readonly
Returns the value of attribute google_api_key.
-
#private_key ⇒ Object
readonly
Returns the value of attribute private_key.
Instance Method Summary collapse
- #batch_get(urls, lookup_strategy = :FETCH_LIVE_DOC) ⇒ Object
- #format_domain(url) ⇒ Object
-
#initialize(api_key = nil, private_key = nil) ⇒ Client
constructor
A new instance of Client.
- #short_content_type(type) ⇒ Object
- #update_cache(url, content_type = :document) ⇒ Object
Constructor Details
#initialize(api_key = nil, private_key = nil) ⇒ Client
Returns a new instance of Client.
18 19 20 21 |
# File 'lib/google/amp/cache/client.rb', line 18 def initialize(api_key = nil, private_key = nil) @google_api_key = api_key @private_key = OpenSSL::PKey::RSA.new(private_key) if private_key end |
Instance Attribute Details
#google_api_key ⇒ Object (readonly)
Returns the value of attribute google_api_key.
16 17 18 |
# File 'lib/google/amp/cache/client.rb', line 16 def google_api_key @google_api_key end |
#private_key ⇒ Object (readonly)
Returns the value of attribute private_key.
16 17 18 |
# File 'lib/google/amp/cache/client.rb', line 16 def private_key @private_key end |
Instance Method Details
#batch_get(urls, lookup_strategy = :FETCH_LIVE_DOC) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/google/amp/cache/client.rb', line 23 def batch_get(urls, lookup_strategy = :FETCH_LIVE_DOC) Faraday.new('https://acceleratedmobilepageurl.googleapis.com/', headers: { 'X-Goog-Api-Key' => google_api_key }) do |conn| conn.request :json conn.response :json conn.response :raise_error end.post('/v1/ampUrls:batchGet', { urls: Array(urls), lookupStrategy: lookup_strategy }).body end |
#format_domain(url) ⇒ Object
67 68 69 |
# File 'lib/google/amp/cache/client.rb', line 67 def format_domain(url) url.gsub('-', '--').tr('.', '-') end |
#short_content_type(type) ⇒ Object
56 57 58 59 60 61 62 63 64 65 |
# File 'lib/google/amp/cache/client.rb', line 56 def short_content_type(type) case type.to_sym when :document 'c' when :image 'i' when :resource 'r' end end |
#update_cache(url, content_type = :document) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/google/amp/cache/client.rb', line 37 def update_cache(url, content_type = :document) page_uri = URI.parse(url) subdomain = format_domain(page_uri.host) path_components = ['update-cache', short_content_type(content_type)] path_components << 's' if page_uri.scheme.match?('https') path_components << "#{page_uri.host}#{page_uri.path}" path = path_components.join('/') params = Faraday::Utils::ParamsHash[{ amp_action: 'flush', amp_ts: Time.now.to_i }] sig = private_key.sign(DIGEST, "/#{path}?#{params.to_query}") params[:amp_url_signature] = Base64.urlsafe_encode64(sig) Faraday.new("https://#{subdomain}.cdn.ampproject.org/") do |conn| conn.response :raise_error end.get(path, params).body end |