google-amp-cache 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: e2029c53203e254334b2c265ceeeaebe12ee61e5bccd8ae537910d9eabd24e87
4
+ data.tar.gz: c7a36a638dc5dc4a71959b58d41977e511c11ca18460327538171467ac9c0ea2
5
+ SHA512:
6
+ metadata.gz: ee6b103810ad6dc17b1bf2933906a6b4bd3bcc1efc65d8e55440995728c390bf783a46b5108004d04b3ac6646a9a86bbd37de5ffe67a9e48d7efad8dcd59928c
7
+ data.tar.gz: ae3693004fec1e4f4b4da2e40f9172af5dd522ced079ae024206a9cd9881fb8ef860c864bce4b8ce3dd3df54ad5d5435d2f9084c484debde297d8dce949ef6a0
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
@@ -0,0 +1,14 @@
1
+ # Changelog
2
+ All notable changes to this project will be documented in this file.
3
+
4
+ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
5
+ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
6
+
7
+ ## [Unreleased]
8
+
9
+ ## 0.1.0 - 2018-06-13
10
+ ### Added
11
+
12
+ * Import the project with initial implementation.
13
+
14
+ [Unreleased]: https://github.com/polydice/google-amp-cache/compare/v0.1.0...HEAD
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in google-amp-cache.gemspec
6
+ gemspec
@@ -0,0 +1,39 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ google-amp-cache (0.1.0)
5
+ httparty (>= 0.7.6)
6
+ multi_json (~> 1.3)
7
+ rack
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ ansi (1.5.0)
13
+ builder (3.2.3)
14
+ httparty (0.16.2)
15
+ multi_xml (>= 0.5.2)
16
+ minitest (5.11.3)
17
+ minitest-reporters (1.2.0)
18
+ ansi
19
+ builder
20
+ minitest (>= 5.0)
21
+ ruby-progressbar
22
+ multi_json (1.13.1)
23
+ multi_xml (0.6.0)
24
+ rack (2.0.5)
25
+ rake (10.5.0)
26
+ ruby-progressbar (1.9.0)
27
+
28
+ PLATFORMS
29
+ ruby
30
+
31
+ DEPENDENCIES
32
+ bundler (~> 1.16)
33
+ google-amp-cache!
34
+ minitest (~> 5.3)
35
+ minitest-reporters (~> 1.2)
36
+ rake (~> 10.0)
37
+
38
+ BUNDLED WITH
39
+ 1.16.1
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Polydice, Inc.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,39 @@
1
+ # google-amp-cache
2
+
3
+ A simple Ruby API wrapper for [Google AMP Cache](https://developers.google.com/amp/cache/).
4
+
5
+ ## Usage
6
+
7
+ Create API client using Google API key:
8
+
9
+ ```ruby
10
+ client = Google::AMP::Cache::Client.new MY_GOOGLE_API_KEY, File.read(MY_PRIVATE_KEY)
11
+ ```
12
+
13
+
14
+ ### Update cache
15
+
16
+ Just simply:
17
+
18
+ ```ruby
19
+ client.update_cache('https://limitless-tundra-65881.herokuapp.com/amp-access/sample/0')
20
+ ```
21
+
22
+ ### Match URLs to AMP URLs
23
+
24
+ You can query AMP URLs from Google AMP Cache:
25
+
26
+ ```ruby
27
+ client.batch_get("https://www.theguardian.com/membership/2016/feb/24/todays-release-of-accelerated-mobile-pages-amp")
28
+ # => {"ampUrls"=>[{"originalUrl"=>"https://www.theguardian.com/membership/2016/feb/24/todays-release-of-accelerated-mobile-pages-amp", "ampUrl"=>"https://amp.theguardian.com/membership/2016/feb/24/todays-release-of-accelerated-mobile-pages-amp", "cdnAmpUrl"=>"https://amp-theguardian-com.cdn.ampproject.org/c/s/amp.theguardian.com/membership/2016/feb/24/todays-release-of-accelerated-mobile-pages-amp"}]}
29
+ ```
30
+
31
+ Or multiple URLs:
32
+
33
+ ```ruby
34
+ client.batch_get(["URL A", "URL B"])
35
+ ```
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.test_files = FileList['test/**/*_test.rb']
6
+ end
7
+ desc "Run tests"
8
+
9
+ task default: :test
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "google/amp/cache"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,31 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "google/amp/cache/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "google-amp-cache"
8
+ spec.version = Google::AMP::Cache::VERSION
9
+ spec.authors = ["Richard Lee"]
10
+ spec.email = ["rl@polydice.com"]
11
+
12
+ spec.summary = %q{A Ruby wrapper for Google AMP Cache API}
13
+ spec.description = spec.summary
14
+ spec.homepage = "https://github.com/polydice/google-amp-cache"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_runtime_dependency "multi_json", "~> 1.3"
24
+ spec.add_runtime_dependency "httparty", ">= 0.7.6"
25
+ spec.add_runtime_dependency "rack"
26
+
27
+ spec.add_development_dependency "bundler", "~> 1.16"
28
+ spec.add_development_dependency "rake", "~> 10.0"
29
+ spec.add_development_dependency "minitest", "~> 5.3"
30
+ spec.add_development_dependency "minitest-reporters", "~> 1.2"
31
+ end
@@ -0,0 +1,5 @@
1
+ require "multi_json"
2
+ require "httparty"
3
+
4
+ require "google/amp/cache/version"
5
+ require "google/amp/cache/client"
@@ -0,0 +1,70 @@
1
+ require 'base64'
2
+ require 'rack'
3
+ require 'uri'
4
+
5
+ module Google::AMP::Cache
6
+ class Client
7
+ include HTTParty
8
+ base_uri 'https://acceleratedmobilepageurl.googleapis.com/v1'
9
+
10
+ UPDATE_CACHE_API_DOMAIN_SUFFIX = 'cdn.ampproject.org'
11
+ DIGEST = OpenSSL::Digest::SHA256.new
12
+
13
+ attr_reader :private_key, :google_api_key
14
+
15
+ def initialize(api_key = nil, private_key = nil)
16
+ @google_api_key = api_key
17
+ @private_key = OpenSSL::PKey::RSA.new(private_key) if private_key
18
+ end
19
+
20
+ def batch_get(urls, lookup_strategy = :FETCH_LIVE_DOC)
21
+ post('/ampUrls:batchGet',
22
+ body: {
23
+ urls: Array(urls),
24
+ lookupStrategy: lookup_strategy
25
+ }.to_json,
26
+ headers: {
27
+ 'Content-Type' => 'application/json',
28
+ "X-Goog-Api-Key" => google_api_key
29
+ })
30
+ end
31
+
32
+ def update_cache(url, content_type = :document)
33
+ page_uri = URI.parse(url)
34
+ subdomain = format_domain(page_uri.host)
35
+
36
+ api_host = URI.parse(["https://", subdomain, '.', UPDATE_CACHE_API_DOMAIN_SUFFIX].join)
37
+ params = {
38
+ amp_action: 'flush',
39
+ amp_ts: Time.now.to_i
40
+ }
41
+
42
+ api_path = "/update-cache/#{short_content_type(content_type)}/#{'s/' if page_uri.scheme.match?('https')}#{page_uri.host}#{page_uri.path}?#{Rack::Utils.build_query(params)}"
43
+ sig = private_key.sign(DIGEST, api_path)
44
+ signature = Base64.urlsafe_encode64(sig)
45
+
46
+ result = self.class.get("#{api_host}#{api_path}&amp_url_signature=#{signature}")
47
+ result.ok?
48
+ end
49
+
50
+ def short_content_type(type)
51
+ case type.to_sym
52
+ when :document
53
+ 'c'
54
+ when :image
55
+ 'i'
56
+ when :resource
57
+ 'r'
58
+ end
59
+ end
60
+
61
+ def format_domain(url)
62
+ url.gsub('-', '--').tr('.', '-')
63
+ end
64
+
65
+ def post(path, opts={})
66
+ response = self.class.post(path, opts)
67
+ response.parsed_response
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,7 @@
1
+ module Google
2
+ module AMP
3
+ module Cache
4
+ VERSION = "0.1.0"
5
+ end
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,155 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: google-amp-cache
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Richard Lee
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-06-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: multi_json
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: httparty
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 0.7.6
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 0.7.6
41
+ - !ruby/object:Gem::Dependency
42
+ name: rack
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.16'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.16'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: minitest
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '5.3'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '5.3'
97
+ - !ruby/object:Gem::Dependency
98
+ name: minitest-reporters
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '1.2'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '1.2'
111
+ description: A Ruby wrapper for Google AMP Cache API
112
+ email:
113
+ - rl@polydice.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - CHANGELOG.md
120
+ - Gemfile
121
+ - Gemfile.lock
122
+ - LICENSE.txt
123
+ - README.md
124
+ - Rakefile
125
+ - bin/console
126
+ - bin/setup
127
+ - google-amp-cache.gemspec
128
+ - lib/google/amp/cache.rb
129
+ - lib/google/amp/cache/client.rb
130
+ - lib/google/amp/cache/version.rb
131
+ homepage: https://github.com/polydice/google-amp-cache
132
+ licenses:
133
+ - MIT
134
+ metadata: {}
135
+ post_install_message:
136
+ rdoc_options: []
137
+ require_paths:
138
+ - lib
139
+ required_ruby_version: !ruby/object:Gem::Requirement
140
+ requirements:
141
+ - - ">="
142
+ - !ruby/object:Gem::Version
143
+ version: '0'
144
+ required_rubygems_version: !ruby/object:Gem::Requirement
145
+ requirements:
146
+ - - ">="
147
+ - !ruby/object:Gem::Version
148
+ version: '0'
149
+ requirements: []
150
+ rubyforge_project:
151
+ rubygems_version: 2.7.3
152
+ signing_key:
153
+ specification_version: 4
154
+ summary: A Ruby wrapper for Google AMP Cache API
155
+ test_files: []