edge_cast 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +12 -0
- data/LICENSE +22 -0
- data/README.md +54 -0
- data/lib/edge_cast/client/media/cache/management.rb +27 -0
- data/lib/edge_cast/client/media/cache/settings/compression.rb +40 -0
- data/lib/edge_cast/client/media/cache/settings/query_string_caching.rb +24 -0
- data/lib/edge_cast/client/media/cache/settings/query_string_logging.rb +26 -0
- data/lib/edge_cast/client/media/cache/settings.rb +26 -0
- data/lib/edge_cast/client/media/log/settings/format.rb +23 -0
- data/lib/edge_cast/client/media/log/settings/storage.rb +23 -0
- data/lib/edge_cast/client/media/token.rb +15 -0
- data/lib/edge_cast/client/media.rb +34 -0
- data/lib/edge_cast/client.rb +46 -0
- data/lib/edge_cast/config.rb +58 -0
- data/lib/edge_cast/connection.rb +45 -0
- data/lib/edge_cast/core_ext/hash.rb +29 -0
- data/lib/edge_cast/core_ext/string.rb +27 -0
- data/lib/edge_cast/error/bad_gateway.rb +7 -0
- data/lib/edge_cast/error/bad_request.rb +7 -0
- data/lib/edge_cast/error/client_error.rb +7 -0
- data/lib/edge_cast/error/forbidden.rb +7 -0
- data/lib/edge_cast/error/internal_server_error.rb +7 -0
- data/lib/edge_cast/error/method_not_allowed.rb +7 -0
- data/lib/edge_cast/error/not_acceptable.rb +7 -0
- data/lib/edge_cast/error/not_found.rb +7 -0
- data/lib/edge_cast/error/server_error.rb +7 -0
- data/lib/edge_cast/error/service_unavailable.rb +7 -0
- data/lib/edge_cast/error/unauthorized.rb +7 -0
- data/lib/edge_cast/error.rb +17 -0
- data/lib/edge_cast/request/authorization.rb +20 -0
- data/lib/edge_cast/request.rb +42 -0
- data/lib/edge_cast/response/raise_client_error.rb +50 -0
- data/lib/edge_cast/response/raise_server_error.rb +20 -0
- data/lib/edge_cast/version.rb +3 -0
- data/lib/edge_cast.rb +24 -0
- metadata +160 -0
data/CHANGELOG.md
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
### 0.0.1 - 12 November, 2012
|
2
|
+
|
3
|
+
### New features
|
4
|
+
|
5
|
+
- Cache compression settings. ([@rymai][])
|
6
|
+
- Cache query string caching settings. ([@rymai][])
|
7
|
+
- Cache query string logging settings. ([@rymai][])
|
8
|
+
- Cache management. ([@rymai][])
|
9
|
+
- Token-Based Authentication. ([@rymai][])
|
10
|
+
|
11
|
+
<!--- The following link definition list is generated by PimpMyChangelog --->
|
12
|
+
[@rymai]: https://github.com/rymai
|
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Rémy Coutable
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
# EdgeCast
|
2
|
+
|
3
|
+
EdgeCast Web Services REST API Ruby wrapper.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'edge_cast'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install edge_cast
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
edge_cast = EdgeCast.new(:account_number => '1234', :api_token => 'abcd1234')
|
23
|
+
|
24
|
+
# Cache compression settings
|
25
|
+
edge_cast.compression(:http_small_object)
|
26
|
+
edge_cast.enable_compression(:http_small_object, ['text/javascript'])
|
27
|
+
edge_cast.disable_compression(:http_small_object, ['text/javascript'])
|
28
|
+
|
29
|
+
# Cache query string caching settings
|
30
|
+
edge_cast.query_string_caching(:http_small_object)
|
31
|
+
edge_cast.update_query_string_caching(:http_small_object, 'no-cache')
|
32
|
+
|
33
|
+
|
34
|
+
# Cache query string logging settings
|
35
|
+
edge_cast.query_string_logging(:http_small_object)
|
36
|
+
edge_cast.update_query_string_logging(:http_small_object, 'no-log')
|
37
|
+
|
38
|
+
|
39
|
+
# Cache management
|
40
|
+
edge_cast.load(:http_small_object, '/foo/bar.js')
|
41
|
+
edge_cast.purge(:http_small_object, '/foo/bar.js')
|
42
|
+
|
43
|
+
# Token-Based Authentication
|
44
|
+
edge_cast.encrypt_token_data(:key => 'abcd1234', :token_parameter => 'ec_expire=1356955200&ec_country_deny=CA&ec_country_allow=US,MX')
|
45
|
+
|
46
|
+
```
|
47
|
+
|
48
|
+
## Contributing
|
49
|
+
|
50
|
+
1. Fork it
|
51
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
52
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
53
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
54
|
+
5. Create new Pull Request
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'edge_cast/core_ext/hash'
|
2
|
+
|
3
|
+
module EdgeCast
|
4
|
+
class Client
|
5
|
+
module Media
|
6
|
+
module Cache
|
7
|
+
module Management
|
8
|
+
|
9
|
+
def load(media_type, path)
|
10
|
+
put('edge/load', management_params(media_type, path))
|
11
|
+
end
|
12
|
+
|
13
|
+
def purge(media_type, path)
|
14
|
+
put('edge/purge', management_params(media_type, path))
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def management_params(media_type, path)
|
20
|
+
{ :media_type => Media.from_key(media_type.to_sym)[:code], :media_path => path }.to_api!
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'edge_cast/core_ext/hash'
|
2
|
+
|
3
|
+
module EdgeCast
|
4
|
+
class Client
|
5
|
+
module Media
|
6
|
+
module Cache
|
7
|
+
module Settings
|
8
|
+
module Compression
|
9
|
+
|
10
|
+
def compression(media_type = nil)
|
11
|
+
response = get("compression#{settings_query_string(media_type)}")
|
12
|
+
|
13
|
+
(response.is_a?(Array) ? response : [response]).inject({}) do |hash, r|
|
14
|
+
return hash unless Media.valid_type?(r['MediaTypeId'])
|
15
|
+
|
16
|
+
Media.from_code(r['MediaTypeId'])[:keys].each { |key| hash[key] = r.to_result! }
|
17
|
+
hash
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def enable_compression(media_type, content_types)
|
22
|
+
update_compression(media_type, content_types, 1)
|
23
|
+
end
|
24
|
+
|
25
|
+
def disable_compression(media_type, content_types)
|
26
|
+
update_compression(media_type, content_types, 0)
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
def update_compression(media_type, content_types, status)
|
32
|
+
put('compression', { :media_type_id => Media.from_key(media_type)[:code], :status => status, :content_types => content_types }.to_api!)
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module EdgeCast
|
2
|
+
class Client
|
3
|
+
module Media
|
4
|
+
module Cache
|
5
|
+
module Settings
|
6
|
+
module QueryStringCaching
|
7
|
+
|
8
|
+
require 'edge_cast/client/media/cache/settings'
|
9
|
+
include EdgeCast::Client::Media::Cache::Settings
|
10
|
+
|
11
|
+
def query_string_caching(media_type = nil)
|
12
|
+
wrap_field_per_media(get("querystringcaching#{settings_query_string(media_type)}"), :query_string_caching)
|
13
|
+
end
|
14
|
+
|
15
|
+
def update_query_string_caching(media_type, caching)
|
16
|
+
put('querystringcaching', { :media_type_id => Media.from_key(media_type)[:code], :query_string_caching => caching }.to_api!)
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'edge_cast/core_ext/hash'
|
2
|
+
|
3
|
+
module EdgeCast
|
4
|
+
class Client
|
5
|
+
module Media
|
6
|
+
module Cache
|
7
|
+
module Settings
|
8
|
+
module QueryStringLogging
|
9
|
+
|
10
|
+
require 'edge_cast/client/media/cache/settings'
|
11
|
+
include EdgeCast::Client::Media::Cache::Settings
|
12
|
+
|
13
|
+
def query_string_logging(media_type = nil)
|
14
|
+
wrap_field_per_media(get("querystringlogging#{settings_query_string(media_type)}"), :query_string_logging)
|
15
|
+
end
|
16
|
+
|
17
|
+
def update_query_string_logging(media_type, logging)
|
18
|
+
put('querystringlogging', { :media_type_id => Media.from_key(media_type)[:code], :query_string_logging => logging }.to_api!)
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'edge_cast/core_ext/string'
|
2
|
+
|
3
|
+
module EdgeCast
|
4
|
+
class Client
|
5
|
+
module Media
|
6
|
+
module Cache
|
7
|
+
module Settings
|
8
|
+
|
9
|
+
private
|
10
|
+
|
11
|
+
def settings_query_string(media_type)
|
12
|
+
media_type ? "?mediatypeid=#{EdgeCast::Client::Media.from_key(media_type)[:code]}" : nil
|
13
|
+
end
|
14
|
+
|
15
|
+
def wrap_field_per_media(response, field)
|
16
|
+
(response.is_a?(Array) ? response : [response]).inject({}) do |hash, r|
|
17
|
+
Media.from_code(r['MediaTypeId'])[:keys].each { |key| hash[key] = r[field.camelize] }
|
18
|
+
hash
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'edge_cast/core_ext/hash'
|
2
|
+
|
3
|
+
module EdgeCast
|
4
|
+
class Client
|
5
|
+
module Media
|
6
|
+
module Log
|
7
|
+
module Settings
|
8
|
+
module Format
|
9
|
+
|
10
|
+
def log_format
|
11
|
+
get('logformat').to_result!
|
12
|
+
end
|
13
|
+
|
14
|
+
def update_log_format(params = {})
|
15
|
+
put('logformat', params.to_api!)
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'edge_cast/core_ext/hash'
|
2
|
+
|
3
|
+
module EdgeCast
|
4
|
+
class Client
|
5
|
+
module Media
|
6
|
+
module Log
|
7
|
+
module Settings
|
8
|
+
module Storage
|
9
|
+
|
10
|
+
def log_storage
|
11
|
+
get('logstorage').to_result!
|
12
|
+
end
|
13
|
+
|
14
|
+
def update_log_storage(params = {})
|
15
|
+
put('logstorage', params.to_api!)
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module EdgeCast
|
2
|
+
class Client
|
3
|
+
module Media
|
4
|
+
|
5
|
+
TYPES = [
|
6
|
+
{ :name => :wms, :keys => [:windows_media_streaming, :wms], :code => 1 },
|
7
|
+
{ :name => :fms, :keys => [:flash_media_streaming, :fms], :code => 2 },
|
8
|
+
{ :name => :hlo, :keys => [:http_large_object, :hlo], :code => 3 },
|
9
|
+
{ :name => :hso, :keys => [:http_small_object, :hso], :code => 8 },
|
10
|
+
{ :name => :adn, :keys => [:application_delivery_network, :adn], :code => 14 },
|
11
|
+
]
|
12
|
+
|
13
|
+
def self.from_code(code)
|
14
|
+
TYPES.find { |type| type[:code] == code }
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.valid_type?(media_type, action = nil)
|
18
|
+
case action
|
19
|
+
when :load
|
20
|
+
TYPES.select { |t| [:fms, :hlo, :hso, :adn].include?(t[:name]) }.any? { |t| t[:keys].include?(media_type) }
|
21
|
+
when :purge
|
22
|
+
TYPES.select { |t| [:wms, :fms, :hlo, :hso, :adn].include?(t[:name]) }.any? { |t| t[:keys].include?(media_type) }
|
23
|
+
else
|
24
|
+
!(from_key(media_type).nil? && from_code(media_type).nil?)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.from_key(media_type)
|
29
|
+
TYPES.find { |type| type[:keys].include?(media_type) }
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'edge_cast/config'
|
2
|
+
require 'edge_cast/connection'
|
3
|
+
require 'edge_cast/request'
|
4
|
+
|
5
|
+
module EdgeCast
|
6
|
+
# Wrapper for the EdgeCast REST API
|
7
|
+
#
|
8
|
+
# @note All methods have been separated into modules and follow the same grouping used in the EdgeCast API Documentation.
|
9
|
+
class Client
|
10
|
+
|
11
|
+
include EdgeCast::Connection
|
12
|
+
include EdgeCast::Request
|
13
|
+
|
14
|
+
require 'edge_cast/client/media'
|
15
|
+
require 'edge_cast/client/media/cache/management'
|
16
|
+
include EdgeCast::Client::Media::Cache::Management
|
17
|
+
require 'edge_cast/client/media/cache/settings/compression'
|
18
|
+
include EdgeCast::Client::Media::Cache::Settings::Compression
|
19
|
+
require 'edge_cast/client/media/cache/settings/query_string_caching'
|
20
|
+
include EdgeCast::Client::Media::Cache::Settings::QueryStringCaching
|
21
|
+
require 'edge_cast/client/media/cache/settings/query_string_logging'
|
22
|
+
include EdgeCast::Client::Media::Cache::Settings::QueryStringLogging
|
23
|
+
|
24
|
+
require 'edge_cast/client/media/log/settings/format'
|
25
|
+
include EdgeCast::Client::Media::Log::Settings::Format
|
26
|
+
require 'edge_cast/client/media/log/settings/storage'
|
27
|
+
include EdgeCast::Client::Media::Log::Settings::Storage
|
28
|
+
|
29
|
+
require 'edge_cast/client/media/token'
|
30
|
+
include EdgeCast::Client::Media::Token
|
31
|
+
|
32
|
+
attr_accessor *Config::VALID_OPTIONS_KEYS
|
33
|
+
|
34
|
+
# Initializes a new API object
|
35
|
+
#
|
36
|
+
# @param attrs [Hash]
|
37
|
+
# @return [EdgeCast::Client]
|
38
|
+
def initialize(attrs = {})
|
39
|
+
attrs = EdgeCast.options.merge(attrs)
|
40
|
+
Config::VALID_OPTIONS_KEYS.each do |key|
|
41
|
+
instance_variable_set("@#{key}".to_sym, attrs[key])
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'edge_cast/version'
|
2
|
+
|
3
|
+
module EdgeCast
|
4
|
+
# Defines constants and methods related to configuration
|
5
|
+
module Config
|
6
|
+
|
7
|
+
# The HTTP connection adapter that will be used to connect if none is set
|
8
|
+
DEFAULT_ADAPTER = :net_http
|
9
|
+
|
10
|
+
# The Faraday connection options if none is set
|
11
|
+
DEFAULT_CONNECTION_OPTIONS = {}
|
12
|
+
|
13
|
+
DEFAULT_HOST = 'api.edgecast.com'
|
14
|
+
|
15
|
+
# The oauth token if none is set
|
16
|
+
DEFAULT_API_TOKEN = nil
|
17
|
+
|
18
|
+
# The value sent in the 'User-Agent' header if none is set
|
19
|
+
DEFAULT_USER_AGENT = "EdgeCast Ruby Gem #{EdgeCast::VERSION}"
|
20
|
+
|
21
|
+
# An array of valid keys in the options hash when configuring a {EdgeCast::Client}
|
22
|
+
VALID_OPTIONS_KEYS = [
|
23
|
+
:adapter,
|
24
|
+
:connection_options,
|
25
|
+
:account_number,
|
26
|
+
:api_token,
|
27
|
+
:user_agent
|
28
|
+
]
|
29
|
+
|
30
|
+
attr_accessor *VALID_OPTIONS_KEYS
|
31
|
+
|
32
|
+
# When this module is extended, set all configuration options to their default values
|
33
|
+
def self.extended(base)
|
34
|
+
base.reset
|
35
|
+
end
|
36
|
+
|
37
|
+
# Convenience method to allow configuration options to be set in a block
|
38
|
+
def configure
|
39
|
+
yield self
|
40
|
+
self
|
41
|
+
end
|
42
|
+
|
43
|
+
# Create a hash of options and their values
|
44
|
+
def options
|
45
|
+
VALID_OPTIONS_KEYS.inject({}) { |opts, k| opts[k] = send(k); opts }
|
46
|
+
end
|
47
|
+
|
48
|
+
# Reset all configuration options to defaults
|
49
|
+
def reset
|
50
|
+
self.adapter = DEFAULT_ADAPTER
|
51
|
+
self.connection_options = DEFAULT_CONNECTION_OPTIONS
|
52
|
+
self.api_token = DEFAULT_API_TOKEN
|
53
|
+
self.user_agent = DEFAULT_USER_AGENT
|
54
|
+
self
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
require 'faraday_middleware'
|
3
|
+
require 'edge_cast/request/authorization'
|
4
|
+
require 'edge_cast/response/raise_client_error'
|
5
|
+
require 'edge_cast/response/raise_server_error'
|
6
|
+
|
7
|
+
module EdgeCast
|
8
|
+
module Connection
|
9
|
+
|
10
|
+
def endpoint
|
11
|
+
"https://#{EdgeCast::Config::DEFAULT_HOST}/v2/mcc/customers/#{account_number}"
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
# Returns a Faraday::Connection object
|
17
|
+
#
|
18
|
+
# @param options [Hash] A hash of options
|
19
|
+
# @return [Faraday::Connection]
|
20
|
+
def connection(options = {})
|
21
|
+
default_options = {
|
22
|
+
:headers => {
|
23
|
+
:accept => 'application/json',
|
24
|
+
:user_agent => user_agent,
|
25
|
+
:host => EdgeCast::Config::DEFAULT_HOST
|
26
|
+
},
|
27
|
+
:ssl => { :verify => false },
|
28
|
+
:url => endpoint,
|
29
|
+
}
|
30
|
+
|
31
|
+
@connection ||= Faraday.new(default_options.merge(connection_options)) do |builder|
|
32
|
+
builder.request :auth, api_token
|
33
|
+
builder.request :json
|
34
|
+
builder.request :multipart
|
35
|
+
builder.request :url_encoded
|
36
|
+
|
37
|
+
builder.response :client_error
|
38
|
+
builder.response :json
|
39
|
+
builder.response :server_error
|
40
|
+
|
41
|
+
builder.adapter(adapter)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'edge_cast/core_ext/string'
|
2
|
+
|
3
|
+
class Hash
|
4
|
+
|
5
|
+
def symbolize_keys!
|
6
|
+
self.keys.each do |key|
|
7
|
+
value = delete(key)
|
8
|
+
self[key.to_sym] = value.respond_to?(:symbolize_keys!) ? value.symbolize_keys! : value
|
9
|
+
end
|
10
|
+
self
|
11
|
+
end
|
12
|
+
|
13
|
+
def to_result!
|
14
|
+
keys.each do |key|
|
15
|
+
value = delete(key)
|
16
|
+
self[key.to_s.underscore.to_sym] = value.respond_to?(:to_result!) ? value.to_result! : value
|
17
|
+
end
|
18
|
+
self
|
19
|
+
end
|
20
|
+
|
21
|
+
def to_api!
|
22
|
+
keys.each do |key|
|
23
|
+
value = delete(key)
|
24
|
+
self[key.to_s.camelize] = value.respond_to?(:to_api!) ? value.to_api! : value
|
25
|
+
end
|
26
|
+
self
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
class String
|
2
|
+
|
3
|
+
def underscore
|
4
|
+
word = self.to_s.dup
|
5
|
+
word.gsub!(/::/, '/')
|
6
|
+
word.gsub!(/([A-Z\d]+)([A-Z][a-z])/,'\1_\2')
|
7
|
+
word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
|
8
|
+
word.tr!("-", "_")
|
9
|
+
word.downcase!
|
10
|
+
word
|
11
|
+
end
|
12
|
+
|
13
|
+
def camelize
|
14
|
+
string = self.to_s
|
15
|
+
string = string.sub(/^[a-z\d]*/) { $&.capitalize }
|
16
|
+
string.gsub(/(?:_|(\/))([a-z\d]*)/i) { "#{$1}#{$2.capitalize}" }.gsub('/', '::')
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
class Symbol
|
22
|
+
|
23
|
+
def camelize
|
24
|
+
self.to_s.camelize
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module EdgeCast
|
2
|
+
# Custom error class for rescuing from all EdgeCast errors
|
3
|
+
class Error < StandardError
|
4
|
+
attr_reader :http_headers
|
5
|
+
|
6
|
+
# Initializes a new Error object
|
7
|
+
#
|
8
|
+
# @param message [String]
|
9
|
+
# @param http_headers [Hash]
|
10
|
+
# @return [EdgeCast::Error]
|
11
|
+
def initialize(message, http_headers)
|
12
|
+
@http_headers = Hash[http_headers]
|
13
|
+
super(message)
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
|
3
|
+
module EdgeCast
|
4
|
+
module Request
|
5
|
+
class Authorization < Faraday::Middleware
|
6
|
+
Faraday.register_middleware :request, :auth => lambda { Authorization }
|
7
|
+
|
8
|
+
def initialize(app, api_token)
|
9
|
+
@app, @api_token = app, api_token
|
10
|
+
end
|
11
|
+
|
12
|
+
def call(env)
|
13
|
+
env[:request_headers]['Authorization'] = "TOK:#{@api_token}"
|
14
|
+
|
15
|
+
@app.call(env)
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
module EdgeCast
|
2
|
+
# Defines HTTP request methods
|
3
|
+
module Request
|
4
|
+
# Perform an HTTP GET request
|
5
|
+
def get(path, params = {}, options = {})
|
6
|
+
request(:get, path, params, options)
|
7
|
+
end
|
8
|
+
|
9
|
+
# Perform an HTTP POST request
|
10
|
+
def post(path, params = {}, options = {})
|
11
|
+
request(:post, path, params, options)
|
12
|
+
end
|
13
|
+
|
14
|
+
# Perform an HTTP PUT request
|
15
|
+
def put(path, params = {}, options = {})
|
16
|
+
request(:put, path, params, options)
|
17
|
+
end
|
18
|
+
|
19
|
+
# Perform an HTTP DELETE request
|
20
|
+
def delete(path, params = {}, options = {})
|
21
|
+
request(:delete, path, params, options)
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
# Perform an HTTP request
|
27
|
+
def request(method, path, params, options)
|
28
|
+
response = connection(options).run_request(method, nil, nil, nil) do |request|
|
29
|
+
request.options[:raw] = true if options[:raw]
|
30
|
+
case method.to_sym
|
31
|
+
when :delete, :get
|
32
|
+
request.url(path, params)
|
33
|
+
when :post, :put
|
34
|
+
request.path = path
|
35
|
+
request.body = params unless params.empty?
|
36
|
+
end
|
37
|
+
end
|
38
|
+
options[:raw] ? response : response.body
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
require 'edge_cast/error/bad_request'
|
3
|
+
require 'edge_cast/error/forbidden'
|
4
|
+
require 'edge_cast/error/method_not_allowed'
|
5
|
+
require 'edge_cast/error/not_acceptable'
|
6
|
+
require 'edge_cast/error/not_found'
|
7
|
+
require 'edge_cast/error/unauthorized'
|
8
|
+
|
9
|
+
module EdgeCast
|
10
|
+
module Response
|
11
|
+
class RaiseClientError < Faraday::Response::Middleware
|
12
|
+
Faraday.register_middleware :response, :client_error => lambda { RaiseClientError }
|
13
|
+
|
14
|
+
def on_complete(env)
|
15
|
+
case env[:status].to_i
|
16
|
+
when 400
|
17
|
+
raise EdgeCast::Error::BadRequest.new(error_body(env[:body]), env[:response_headers])
|
18
|
+
when 401
|
19
|
+
raise EdgeCast::Error::Unauthorized.new(error_body(env[:body]), env[:response_headers])
|
20
|
+
when 403
|
21
|
+
raise EdgeCast::Error::Forbidden.new(error_body(env[:body]), env[:response_headers])
|
22
|
+
when 404
|
23
|
+
raise EdgeCast::Error::NotFound.new(error_body(env[:body]), env[:response_headers])
|
24
|
+
when 405
|
25
|
+
raise EdgeCast::Error::MethodNotAllowed.new(error_body(env[:body]), env[:response_headers])
|
26
|
+
when 406
|
27
|
+
raise EdgeCast::Error::NotAcceptable.new(error_body(env[:body]), env[:response_headers])
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
def error_body(body)
|
34
|
+
if body.nil?
|
35
|
+
''
|
36
|
+
elsif body['error']
|
37
|
+
body['error']
|
38
|
+
elsif body['errors']
|
39
|
+
first = Array(body['errors']).first
|
40
|
+
if first.kind_of?(Hash)
|
41
|
+
first['message'].chomp
|
42
|
+
else
|
43
|
+
first.chomp
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
require 'edge_cast/error/bad_gateway'
|
3
|
+
require 'edge_cast/error/internal_server_error'
|
4
|
+
require 'edge_cast/error/service_unavailable'
|
5
|
+
|
6
|
+
module EdgeCast
|
7
|
+
module Response
|
8
|
+
class RaiseServerError < Faraday::Response::Middleware
|
9
|
+
Faraday.register_middleware :response, :server_error => lambda { RaiseServerError }
|
10
|
+
|
11
|
+
def on_complete(env)
|
12
|
+
case env[:status].to_i
|
13
|
+
when 500
|
14
|
+
raise EdgeCast::Error::InternalServerError.new("Something is technically wrong.", env[:response_headers])
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
data/lib/edge_cast.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'edge_cast/client'
|
2
|
+
require 'edge_cast/config'
|
3
|
+
|
4
|
+
module EdgeCast
|
5
|
+
extend Config
|
6
|
+
class << self
|
7
|
+
# Alias for EdgeCast::Client.new
|
8
|
+
#
|
9
|
+
# @return [EdgeCast::Client]
|
10
|
+
def new(options = {})
|
11
|
+
EdgeCast::Client.new(options)
|
12
|
+
end
|
13
|
+
|
14
|
+
# Delegate to EdgeCast::Client
|
15
|
+
def method_missing(method, *args, &block)
|
16
|
+
return super unless new.respond_to?(method)
|
17
|
+
new.send(method, *args, &block)
|
18
|
+
end
|
19
|
+
|
20
|
+
def respond_to?(method, include_private=false)
|
21
|
+
new.respond_to?(method, include_private) || super(method, include_private)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,160 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: edge_cast
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Rémy Coutable
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-11-12 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: faraday
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0.8'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0.8'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: faraday_middleware
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0.8'
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0.8'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rspec
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ~>
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '2.11'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '2.11'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: vcr
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ~>
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '2.3'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ~>
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '2.3'
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: webmock
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ~>
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '1.9'
|
86
|
+
type: :development
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ~>
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '1.9'
|
94
|
+
description: EdgeCast Web Services REST API Ruby wrapper.
|
95
|
+
email:
|
96
|
+
- remy@jilion.com
|
97
|
+
executables: []
|
98
|
+
extensions: []
|
99
|
+
extra_rdoc_files: []
|
100
|
+
files:
|
101
|
+
- lib/edge_cast/client/media/cache/management.rb
|
102
|
+
- lib/edge_cast/client/media/cache/settings/compression.rb
|
103
|
+
- lib/edge_cast/client/media/cache/settings/query_string_caching.rb
|
104
|
+
- lib/edge_cast/client/media/cache/settings/query_string_logging.rb
|
105
|
+
- lib/edge_cast/client/media/cache/settings.rb
|
106
|
+
- lib/edge_cast/client/media/log/settings/format.rb
|
107
|
+
- lib/edge_cast/client/media/log/settings/storage.rb
|
108
|
+
- lib/edge_cast/client/media/token.rb
|
109
|
+
- lib/edge_cast/client/media.rb
|
110
|
+
- lib/edge_cast/client.rb
|
111
|
+
- lib/edge_cast/config.rb
|
112
|
+
- lib/edge_cast/connection.rb
|
113
|
+
- lib/edge_cast/core_ext/hash.rb
|
114
|
+
- lib/edge_cast/core_ext/string.rb
|
115
|
+
- lib/edge_cast/error/bad_gateway.rb
|
116
|
+
- lib/edge_cast/error/bad_request.rb
|
117
|
+
- lib/edge_cast/error/client_error.rb
|
118
|
+
- lib/edge_cast/error/forbidden.rb
|
119
|
+
- lib/edge_cast/error/internal_server_error.rb
|
120
|
+
- lib/edge_cast/error/method_not_allowed.rb
|
121
|
+
- lib/edge_cast/error/not_acceptable.rb
|
122
|
+
- lib/edge_cast/error/not_found.rb
|
123
|
+
- lib/edge_cast/error/server_error.rb
|
124
|
+
- lib/edge_cast/error/service_unavailable.rb
|
125
|
+
- lib/edge_cast/error/unauthorized.rb
|
126
|
+
- lib/edge_cast/error.rb
|
127
|
+
- lib/edge_cast/request/authorization.rb
|
128
|
+
- lib/edge_cast/request.rb
|
129
|
+
- lib/edge_cast/response/raise_client_error.rb
|
130
|
+
- lib/edge_cast/response/raise_server_error.rb
|
131
|
+
- lib/edge_cast/version.rb
|
132
|
+
- lib/edge_cast.rb
|
133
|
+
- CHANGELOG.md
|
134
|
+
- LICENSE
|
135
|
+
- README.md
|
136
|
+
homepage: https://github.com/jilion/edge_cast
|
137
|
+
licenses: []
|
138
|
+
post_install_message:
|
139
|
+
rdoc_options: []
|
140
|
+
require_paths:
|
141
|
+
- lib
|
142
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
143
|
+
none: false
|
144
|
+
requirements:
|
145
|
+
- - ! '>='
|
146
|
+
- !ruby/object:Gem::Version
|
147
|
+
version: '0'
|
148
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
149
|
+
none: false
|
150
|
+
requirements:
|
151
|
+
- - ! '>='
|
152
|
+
- !ruby/object:Gem::Version
|
153
|
+
version: '0'
|
154
|
+
requirements: []
|
155
|
+
rubyforge_project:
|
156
|
+
rubygems_version: 1.8.23
|
157
|
+
signing_key:
|
158
|
+
specification_version: 3
|
159
|
+
summary: EdgeCast Web Services REST API Ruby wrapper.
|
160
|
+
test_files: []
|