keycdn 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 72aba56da0aa2dc7d2c0a6d32b53c9426f9bf3c6
4
+ data.tar.gz: 1916a8382c19fcd77a1a18b6123feda4ce7ade83
5
+ SHA512:
6
+ metadata.gz: a7af8dfdc66184ecda1aaac07769753693de24bb1556daae66b40ea86c0cd029c87075f9218a7736feb725e8da5bd7dfcf60390dfbcc3cc8ae6a0f19c0e068f0
7
+ data.tar.gz: e7feae3ab6cd50afea1f160870220fc347f5251fe77dc7af86ae07056e87f3bd6265657c1c70df2cb38b89f94d2856c44b37fe7bee8cb534b9fa67b620d47b5d
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ # bundler
2
+ .bundle
3
+ vendor
4
+
5
+ pkg
6
+ *.gem
7
+ *.lock
8
+ .DS_Store
9
+ npm-debug.log
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ MIT License
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,98 @@
1
+ # Ruby gem for the KeyCDN API
2
+
3
+
4
+ ## Installation
5
+
6
+ ``` bash
7
+ gem install keycdn
8
+ ```
9
+
10
+ > Requires Ruby 1.9.2+
11
+
12
+ #### With Bundler
13
+
14
+ ```
15
+ bundle init
16
+ echo "gem 'keycdn'" >> Gemfile
17
+ bundle install --path vendor/bundle
18
+ ```
19
+
20
+ ## Usage
21
+
22
+ ### Initial
23
+ ```ruby
24
+ require 'keycdn'
25
+
26
+ # $ export APIKEY=<your_api_key>
27
+ keycdn = KeyCDN::Client.new(ENV["APIKEY"])
28
+ ```
29
+
30
+ ### Get all zones
31
+ ```ruby
32
+ keycdn.get("zones.json")
33
+ ```
34
+
35
+ ### Get a specific zone
36
+ ```ruby
37
+ keycdn.get("zones/<zoneId>.json")
38
+ ```
39
+
40
+ ### Generate reports
41
+ ```ruby
42
+ time = Time.now.to_i
43
+
44
+ report = {
45
+ :start => time-3600,
46
+ :end => time
47
+ }
48
+
49
+ keycdn.get("reports/traffic.json", report)
50
+ keycdn.get("reports/storage.json", report)
51
+ keycdn.get("reports/credits.json", report)
52
+ ```
53
+
54
+ ### Add a new zone
55
+ ```ruby
56
+ zone = {
57
+ :name => @time.to_s,
58
+ :expire => 1234
59
+ }
60
+
61
+ keycdn.post("zones.json", zone)
62
+ ```
63
+
64
+ ### Edit a zone
65
+ ```ruby
66
+ zone_id = '<zoneId>'
67
+
68
+ zone = {
69
+ :name => '<name>',
70
+ :expire => 4321
71
+ }
72
+
73
+ keycdn.put("zones/#{zone_id}.json", zone)
74
+ ```
75
+
76
+ ### Purge zone cache
77
+ ```ruby
78
+ # purge zone cache
79
+ zone_id = '<zoneId>'
80
+
81
+ keycdn.get("zones/purge/#{zone_id}.json")
82
+ ```
83
+
84
+ ### Purge URLs
85
+ ```ruby
86
+ zone_id = '<zoneId>'
87
+
88
+ urls = {
89
+ 'urls[0]' => 'demo-1.kxcdn.com/lorem.css',
90
+ 'urls[1]' => 'demo-1.kxcdn.com/lorem.jpg'
91
+ };
92
+
93
+ keycdn.del("zones/purgeurl/#{zone_id}.json", urls)
94
+
95
+ ```
96
+
97
+
98
+ For more details visit https://www.keycdn.com
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env rake
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs << 'test'
6
+ end
7
+
8
+ desc "Run tests"
9
+ task :default => :test
data/examples/purge.rb ADDED
@@ -0,0 +1,17 @@
1
+ #!/usr/bin/env ruby
2
+ require File.join("./", File.dirname(__FILE__), "..","lib","keycdn")
3
+
4
+ if ENV["APIKEY"].nil?
5
+ puts '$ export APIKEY=<your_api_key>'
6
+ end
7
+
8
+ keycdn = KeyCDN::Client.new(ENV["APIKEY"])
9
+
10
+ zone_id = '<zoneId>';
11
+
12
+ urls = {
13
+ 'urls[0]' => 'demo-1.kxcdn.com/lorem.css',
14
+ 'urls[1]' => 'demo-1.kxcdn.com/lorem.jpg'
15
+ };
16
+
17
+ puts keycdn.del("zones/purgeurl/#{zone_id}.json", urls)
@@ -0,0 +1,21 @@
1
+ #!/usr/bin/env ruby
2
+ require File.join('./', File.dirname(__FILE__), '..','lib','keycdn')
3
+ #require "keycdn"
4
+
5
+ keycdn = KeyCDN::Client.new(ENV["APIKEY"])
6
+
7
+ puts 'GET /zones.json'
8
+ puts keycdn.get("zones.json")
9
+
10
+ puts 'GET /zones/<zoneId>.json'
11
+ puts keycdn.get("zones/<zoneId>.json")
12
+
13
+ time = Time.now.to_i
14
+
15
+ report = {
16
+ :start => time-3600,
17
+ :end => time
18
+ }
19
+
20
+ puts 'GET reports/credits.json'
21
+ puts keycdn.get("reports/credits.json", report)
data/keycdn.gemspec ADDED
@@ -0,0 +1,16 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/keycdn/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.name = "keycdn"
6
+ gem.homepage = "https://www.keycdn.com"
7
+ gem.version = KeyCDN::VERSION
8
+ gem.license = "MIT"
9
+ gem.files = `git ls-files`.split($\)
10
+ gem.require_paths = ['lib']
11
+ gem.summary = %Q{A Ruby gem for the KeyCDN API}
12
+ gem.description = %Q{This Ruby gem helps you to interact with the KeyCDN API}
13
+ gem.email = "support@keycdn.com"
14
+ gem.authors = ["KeyCDN"]
15
+ gem.add_dependency 'json' if RUBY_VERSION.start_with? "1.8"
16
+ end
@@ -0,0 +1,3 @@
1
+ module KeyCDN
2
+ VERSION = "0.1.0"
3
+ end
data/lib/keycdn.rb ADDED
@@ -0,0 +1,77 @@
1
+ require "uri"
2
+ require "net/http"
3
+ require "json"
4
+
5
+ module KeyCDN
6
+
7
+ class Client
8
+
9
+ ENDPOINT = 'https://www.keycdn.com/'
10
+
11
+ METHOD_MAP = {
12
+ :get => Net::HTTP::Get,
13
+ :post => Net::HTTP::Post,
14
+ :put => Net::HTTP::Put,
15
+ :delete => Net::HTTP::Delete
16
+ }
17
+
18
+ def initialize(api_key)
19
+ @api_key = api_key
20
+ end
21
+
22
+ def get(url, params = {})
23
+ call url, :get, params
24
+ end
25
+
26
+ def post(url, params = {})
27
+ call url, :post, params
28
+ end
29
+
30
+ def put(url, params = {})
31
+ call url, :put, params
32
+ end
33
+
34
+ def del(url, params = {})
35
+ call url, :delete, params
36
+ end
37
+
38
+ private
39
+
40
+ def call(url, method, params = {})
41
+ raise ArgumentError, "URL could not be empty" if url.nil? || url.empty?
42
+
43
+ url = ENDPOINT + url
44
+ uri = URI(url)
45
+
46
+ request = Net::HTTP.new(uri.host, uri.port)
47
+ request.use_ssl = true
48
+
49
+ case method
50
+ when :get
51
+ full_url = encode_path_params(url, params)
52
+ request_header = METHOD_MAP[method].new(full_url)
53
+ else
54
+ request_header = METHOD_MAP[method].new(uri.path)
55
+ request_header.set_form_data(params)
56
+ end
57
+
58
+ request_header.basic_auth @api_key, ''
59
+ response = request.start {|http| http.request(request_header) }
60
+
61
+ parse_response(response)
62
+ end
63
+
64
+ def encode_path_params(path, params)
65
+ encoded = URI.encode_www_form(params)
66
+ [path, encoded].join("?")
67
+ end
68
+
69
+ def parse_response(response)
70
+ body = response.body
71
+ json = JSON.parse(body, :symbolize_names => false)
72
+ json
73
+ end
74
+
75
+ end
76
+
77
+ end
@@ -0,0 +1,81 @@
1
+ require 'minitest/autorun'
2
+ require 'json'
3
+ require './lib/keycdn'
4
+
5
+ if (ENV["APIKEY"].nil?)
6
+ abort "Please export your APIKEY."
7
+ end
8
+
9
+ class ClientTest < Minitest::Unit::TestCase
10
+
11
+ def setup
12
+ @keycdn = KeyCDN::Client.new(ENV["APIKEY"])
13
+ @time = Time.now.to_i
14
+ @zone_id = '<zoneId>'
15
+ end
16
+
17
+ def test_get
18
+
19
+ [ "zonealiases.json",
20
+ "zonereferrers.json",
21
+ "zones.json"
22
+ ].each do |end_point|
23
+ key = end_point.include?("/") ? end_point.split("/")[1] : end_point.gsub(/\.json/, "")
24
+
25
+ assert @keycdn.get(end_point)["data"][key], "get #{key} with data"
26
+ end
27
+
28
+ end
29
+
30
+ def test_report
31
+
32
+ report = {
33
+ :start => @time-3600,
34
+ :end => @time
35
+ }
36
+
37
+ assert @keycdn.get("reports/credits.json", report)["data"]["stats"], "get credit stats"
38
+
39
+ end
40
+
41
+ def test_purge_zone
42
+
43
+ assert_equal "success", @keycdn.get("zones/purge/#{@zone_id}.json")["status"], "purge zone cache"
44
+
45
+ end
46
+
47
+ def test_purge_urls
48
+
49
+ urls = {
50
+ 'urls[0]' => 'demo-1.kxcdn.com/lorem.css',
51
+ 'urls[1]' => 'demo-1.kxcdn.com/lorem.jpg'
52
+ };
53
+
54
+ assert_equal "success", @keycdn.del("zones/purgeurl/#{@zone_id}.json", urls)["status"], "purge urls"
55
+
56
+ end
57
+
58
+ def test_post_and_del
59
+
60
+ zone = {
61
+ :name => @time.to_s,
62
+ :expire => 9999
63
+ }
64
+
65
+ zone_id = @keycdn.post("zones.json", zone)["data"]["zone"]["id"]
66
+ assert zone_id, "post/add zone"
67
+
68
+ assert_equal "success", @keycdn.del("zones/#{zone_id}.json")["status"], "delete zone"
69
+ end
70
+
71
+ def test_put
72
+
73
+ zone = {
74
+ :name => 'demo',
75
+ :expire => 8888
76
+ }
77
+
78
+ assert_equal @zone_id, @keycdn.put("zones/#{@zone_id}.json", zone)["data"]["zone"]["id"], "put/edit"
79
+ end
80
+
81
+ end
metadata ADDED
@@ -0,0 +1,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: keycdn
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - KeyCDN
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-04-15 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: This Ruby gem helps you to interact with the KeyCDN API
14
+ email: support@keycdn.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - .gitignore
20
+ - Gemfile
21
+ - LICENSE
22
+ - README.md
23
+ - Rakefile
24
+ - examples/purge.rb
25
+ - examples/simple.rb
26
+ - keycdn.gemspec
27
+ - lib/keycdn.rb
28
+ - lib/keycdn/version.rb
29
+ - test/test_keycdn.rb
30
+ homepage: https://www.keycdn.com
31
+ licenses:
32
+ - MIT
33
+ metadata: {}
34
+ post_install_message:
35
+ rdoc_options: []
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - '>='
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ requirements: []
49
+ rubyforge_project:
50
+ rubygems_version: 2.0.14
51
+ signing_key:
52
+ specification_version: 4
53
+ summary: A Ruby gem for the KeyCDN API
54
+ test_files: []