optimizeplayer 0.0.1

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.
Files changed (49) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +9 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +4 -0
  5. data/Gemfile +4 -0
  6. data/LICENSE +22 -0
  7. data/README.md +26 -0
  8. data/Rakefile +6 -0
  9. data/bin/console +14 -0
  10. data/bin/setup +7 -0
  11. data/lib/optimize_player.rb +45 -0
  12. data/lib/optimize_player/account.rb +24 -0
  13. data/lib/optimize_player/api_object.rb +161 -0
  14. data/lib/optimize_player/asset.rb +5 -0
  15. data/lib/optimize_player/client.rb +177 -0
  16. data/lib/optimize_player/converter.rb +16 -0
  17. data/lib/optimize_player/encoding.rb +5 -0
  18. data/lib/optimize_player/errors/api_connection_error.rb +7 -0
  19. data/lib/optimize_player/errors/api_error.rb +7 -0
  20. data/lib/optimize_player/errors/bad_request.rb +7 -0
  21. data/lib/optimize_player/errors/connection_error.rb +7 -0
  22. data/lib/optimize_player/errors/forbidden.rb +7 -0
  23. data/lib/optimize_player/errors/method_not_allowed.rb +7 -0
  24. data/lib/optimize_player/errors/optimize_player_error.rb +20 -0
  25. data/lib/optimize_player/errors/resource_not_found.rb +7 -0
  26. data/lib/optimize_player/errors/socket_error.rb +7 -0
  27. data/lib/optimize_player/errors/unauthorized.rb +7 -0
  28. data/lib/optimize_player/errors/unhandled_error.rb +7 -0
  29. data/lib/optimize_player/errors/unprocessable_entity.rb +7 -0
  30. data/lib/optimize_player/folder.rb +5 -0
  31. data/lib/optimize_player/integration.rb +5 -0
  32. data/lib/optimize_player/media_info.rb +5 -0
  33. data/lib/optimize_player/project.rb +24 -0
  34. data/lib/optimize_player/proxies/account_proxy.rb +24 -0
  35. data/lib/optimize_player/proxies/asset_proxy.rb +9 -0
  36. data/lib/optimize_player/proxies/base_proxy.rb +36 -0
  37. data/lib/optimize_player/proxies/folder_proxy.rb +9 -0
  38. data/lib/optimize_player/proxies/integration_proxy.rb +9 -0
  39. data/lib/optimize_player/proxies/project_proxy.rb +14 -0
  40. data/lib/optimize_player/signer.rb +142 -0
  41. data/lib/optimize_player/version.rb +3 -0
  42. data/lib/optimizeplayer.rb +2 -0
  43. data/optimize_player.gemspec +29 -0
  44. data/spec/client_spec.rb +305 -0
  45. data/spec/converter_spec.rb +63 -0
  46. data/spec/optimize_player_spec.rb +7 -0
  47. data/spec/proxy_spec.rb +67 -0
  48. data/spec/spec_helper.rb +2 -0
  49. metadata +175 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f50c14cf61e2c561dbc3b3b8310c231d2e6e1652
4
+ data.tar.gz: 2b6daaac6d8e5216100e9493f66479fbf300690b
5
+ SHA512:
6
+ metadata.gz: c0bfd170656005386f7fdf8f6af7862889f2519e627e3e5579f9cea34a6d7542e7d3e4d8e23f16ed26472ce47620c6031021ad5a02b087140af51d0c0110fa23
7
+ data.tar.gz: 9d80be4410dc108070136edcb6215d29106377e052fa9851f462256ab0eeb9af73b86f1d31b7e71680110415ede6857d6dbca8d9b3b7ae60c43d00285d7fef9b
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ before_install: gem install bundler -v 1.10.3
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in optimize_player.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 OptimizePlayer, 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 all
13
+ 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 THE
21
+ SOFTWARE.
22
+
data/README.md ADDED
@@ -0,0 +1,26 @@
1
+ # OptimizePlayer
2
+
3
+ ## Installation
4
+
5
+ Add this line to your application's Gemfile:
6
+
7
+ ```ruby
8
+ gem 'optimizeplayer'
9
+ ```
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install optimizeplayer
18
+
19
+ ## Development
20
+
21
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
22
+
23
+ ## Contributing
24
+
25
+ Bug reports and pull requests are welcome on GitHub at https://github.com/optimizeplayer/optimizeplayer-ruby.
26
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "optimize_player"
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
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,45 @@
1
+ # Version
2
+ require "optimize_player/version"
3
+
4
+ require 'cgi'
5
+ require 'set'
6
+ require 'openssl'
7
+ require 'rack/utils'
8
+ require 'rest_client'
9
+ require 'json'
10
+
11
+ require 'optimize_player/client'
12
+ require 'optimize_player/converter'
13
+ require 'optimize_player/signer'
14
+
15
+ # Proxies
16
+ require 'optimize_player/proxies/base_proxy'
17
+ require 'optimize_player/proxies/account_proxy'
18
+ require 'optimize_player/proxies/folder_proxy'
19
+ require 'optimize_player/proxies/project_proxy'
20
+ require 'optimize_player/proxies/asset_proxy'
21
+ require 'optimize_player/proxies/integration_proxy'
22
+
23
+ # Resources
24
+ require 'optimize_player/api_object'
25
+ require 'optimize_player/account'
26
+ require 'optimize_player/folder'
27
+ require 'optimize_player/project'
28
+ require 'optimize_player/asset'
29
+ require 'optimize_player/integration'
30
+ require 'optimize_player/media_info'
31
+ require 'optimize_player/encoding'
32
+
33
+ # Errors
34
+ require 'optimize_player/errors/optimize_player_error'
35
+ require 'optimize_player/errors/api_connection_error'
36
+ require 'optimize_player/errors/api_error'
37
+ require 'optimize_player/errors/bad_request'
38
+ require 'optimize_player/errors/connection_error'
39
+ require 'optimize_player/errors/forbidden'
40
+ require 'optimize_player/errors/resource_not_found'
41
+ require 'optimize_player/errors/socket_error'
42
+ require 'optimize_player/errors/unauthorized'
43
+ require 'optimize_player/errors/unhandled_error'
44
+ require 'optimize_player/errors/unprocessable_entity'
45
+ require 'optimize_player/errors/method_not_allowed'
@@ -0,0 +1,24 @@
1
+ module OptimizePlayer
2
+ class Account < ApiObject
3
+ def save(opts={})
4
+ attrs = {}
5
+ @new_data.each do |n|
6
+ attrs[n] = @data[n]
7
+ end
8
+ attrs = attrs.merge(opts)
9
+
10
+ if attrs.any?
11
+ response = context.client.send_request(context.entity_name, :patch, attrs)
12
+ refresh_from(response)
13
+ end
14
+ self
15
+ end
16
+
17
+ def delete(opts={})
18
+ response = context.client.send_request(context.entity_name, :delete)
19
+ refresh_from(response)
20
+ self
21
+ end
22
+ alias_method :destroy, :delete
23
+ end
24
+ end
@@ -0,0 +1,161 @@
1
+ module OptimizePlayer
2
+ class ApiObject
3
+ @@permanent_attributes = Set.new([:id, :cid, :object, :created_at, :updated_at])
4
+
5
+ attr_reader :context
6
+
7
+ def initialize(context, attrs={})
8
+ @context = context
9
+ @data = attrs
10
+ @new_data = Set.new
11
+ end
12
+
13
+ def inspect()
14
+ id_string = (self.respond_to?(:id) && !self.id.nil?) ? " id=#{self.id}" : ""
15
+ "#<#{self.class}:0x#{self.object_id.to_s(16)}#{id_string}> JSON: " + JSON.pretty_generate(@data)
16
+ end
17
+
18
+ def metaclass
19
+ class << self; self; end
20
+ end
21
+
22
+ def self.construct_from(context, response)
23
+ api_object = self.new(context, response)
24
+ api_object.refresh
25
+
26
+ api_object
27
+ end
28
+
29
+ def to_hash
30
+ @data
31
+ end
32
+
33
+ def keys
34
+ @data.keys
35
+ end
36
+
37
+ def values
38
+ @data.values
39
+ end
40
+
41
+ def to_json(*a)
42
+ JSON.generate(@data)
43
+ end
44
+
45
+ def as_json(*a)
46
+ @data.as_json(*a)
47
+ end
48
+
49
+ def [](k)
50
+ @data[k]
51
+ end
52
+
53
+ def []=(k, v)
54
+ send(:"#{k}=", v)
55
+ end
56
+
57
+ def refresh
58
+ instance_eval do
59
+ add_accessors(@data.keys)
60
+ end
61
+
62
+ @data.each do |k, v|
63
+ @data[k] = Converter.convert_to_object(context, v)
64
+ end
65
+
66
+ self
67
+ end
68
+
69
+ def refresh_from(data)
70
+ removed = Set.new(@data.keys - data.keys)
71
+ added = Set.new(data.keys - @data.keys)
72
+
73
+ instance_eval do
74
+ remove_accessors(removed)
75
+ add_accessors(added)
76
+ end
77
+ removed.each do |k|
78
+ @data.delete(k)
79
+ @new_data.delete(k)
80
+ end
81
+ data.each do |k, v|
82
+ @data[k] = Converter.convert_to_object(context, v)
83
+ @new_data.delete(k)
84
+ end
85
+ end
86
+
87
+ def save(opts={})
88
+ attrs = {}
89
+ @new_data.each do |n|
90
+ attrs[n] = @data[n]
91
+ end
92
+ attrs = attrs.merge(opts)
93
+
94
+ if attrs.any?
95
+ response = context.client.send_request("#{context.entity_name}/#{id}", :patch, attrs)
96
+ refresh_from(response)
97
+ end
98
+ self
99
+ end
100
+
101
+ def delete(opts={})
102
+ response = context.client.send_request("#{context.entity_name}/#{id}", :delete)
103
+ refresh_from(response)
104
+ self
105
+ end
106
+ alias_method :destroy, :delete
107
+
108
+ def method_missing(name, *args)
109
+ if name.to_s.end_with?('=')
110
+ attr = name.to_s[0...-1].to_sym
111
+ add_accessors([attr])
112
+ begin
113
+ mth = method(name)
114
+ rescue NameError
115
+ raise NoMethodError.new("Cannot set #{attr} on this object")
116
+ end
117
+ return mth.call(args[0])
118
+ else
119
+ return @data[name.to_s] if @data.has_key?(name.to_s)
120
+ end
121
+
122
+ super
123
+ end
124
+
125
+ def respond_to_missing?(symbol, include_private = false)
126
+ @data.has_key?(symbol) || super
127
+ end
128
+
129
+ def add_accessors(keys)
130
+ metaclass.instance_eval do
131
+ keys.each do |k|
132
+ next if @@permanent_attributes.include?(k.to_sym)
133
+ k_eq = :"#{k}="
134
+ define_method(k) { @data[k] }
135
+ define_method(k_eq) do |v|
136
+ if v == ""
137
+ raise ArgumentError.new(
138
+ "You cannot set #{k} to an empty string." +
139
+ "We interpret empty strings as nil in requests." +
140
+ "You may set #{self}.#{k} = nil to delete the property.")
141
+ end
142
+
143
+ @data[k] = v
144
+ @new_data.add(k)
145
+ end
146
+ end
147
+ end
148
+ end
149
+
150
+ def remove_accessors(keys)
151
+ metaclass.instance_eval do
152
+ keys.each do |k|
153
+ next if @@permanent_attributes.include?(k.to_sym)
154
+ k_eq = :"#{k}="
155
+ remove_method(k) if method_defined?(k)
156
+ remove_method(k_eq) if method_defined?(k_eq)
157
+ end
158
+ end
159
+ end
160
+ end
161
+ end
@@ -0,0 +1,5 @@
1
+ module OptimizePlayer
2
+ class Asset < ApiObject
3
+
4
+ end
5
+ end
@@ -0,0 +1,177 @@
1
+ module OptimizePlayer
2
+ class Client
3
+ API_URI = "http://api.optimizeplayer.com/v1/"
4
+
5
+ def initialize(access_token, secret_key, api_endpoint=nil)
6
+ @api_endpoint = api_endpoint || API_URI
7
+ @access_token = access_token
8
+ @secret_key = secret_key
9
+ end
10
+
11
+ def account
12
+ @account ||= OptimizePlayer::Proxies::AccountProxy.new(self)
13
+ end
14
+
15
+ def projects
16
+ @projects ||= OptimizePlayer::Proxies::ProjectProxy.new(self)
17
+ end
18
+
19
+ def assets
20
+ @assets ||= OptimizePlayer::Proxies::AssetProxy.new(self)
21
+ end
22
+
23
+ def folders
24
+ @folders ||= OptimizePlayer::Proxies::FolderProxy.new(self)
25
+ end
26
+
27
+ def integrations
28
+ @integrations ||= OptimizePlayer::Proxies::IntegrationProxy.new(self)
29
+ end
30
+
31
+ def send_request(url, method, params = {})
32
+ headers = {:accept => :json}
33
+
34
+ url = @api_endpoint + url + "?access_token=#{@access_token}"
35
+
36
+ case method.to_s.downcase.to_sym
37
+ when :get, :delete
38
+ url += "#{URI.parse(url).query ? '&' : '?'}#{uri_encode(params)}" if params && params.any?
39
+ payload = nil
40
+ else
41
+ payload = params
42
+ end
43
+
44
+ url = OptimizePlayer::Signer.new.sign_url(url, @secret_key)
45
+
46
+ request_opts = {
47
+ :headers => request_headers.update(headers),
48
+ :method => method,
49
+ :open_timeout => 30,
50
+ :payload => payload,
51
+ :url => url,
52
+ :timeout => 80
53
+ }
54
+
55
+ begin
56
+ response = RestClient::Request.execute(request_opts)
57
+ rescue RestClient::BadRequest,
58
+ RestClient::Unauthorized,
59
+ RestClient::ResourceNotFound,
60
+ RestClient::Forbidden,
61
+ RestClient::UnprocessableEntity => e
62
+
63
+ json_obj = JSON.parse(e.http_body)
64
+ error = json_obj['error']
65
+ message = json_obj['message']
66
+ error_klass_name = e.class.name.split('::')[-1]
67
+ error_klass = Object.const_get("OptimizePlayer::Errors::#{error_klass_name}")
68
+
69
+ raise error_klass.new(e.http_code, error, message)
70
+
71
+ rescue RestClient::MethodNotAllowed => e
72
+ raise OptimizePlayer::Errors::MethodNotAllowed.new(405, 'MethodNotAllowed', 'Method Not Allowed')
73
+
74
+ rescue SocketError => e
75
+ message = "Unexpected error communicating when trying to connect to OptimizePlayer. " +
76
+ "You may be seeing this message because your DNS is not working."
77
+ raise OptimizePlayer::Errors::SocketError.new(nil, 'NetworkError', message)
78
+
79
+ rescue Errno::ECONNREFUSED => e
80
+ message = "Unexpected error communicating with OptimizePlayer."
81
+ raise OptimizePlayer::Errors::ConnectionError.new(nil, 'ConnectionError', message)
82
+
83
+ rescue RestClient::ServerBrokeConnection, RestClient::RequestTimeout => e
84
+ message = "Could not connect to OptimizePlayer. " +
85
+ "Please check your internet connection and try again."
86
+ raise OptimizePlayer::Errors::ConnectionError.new(nil, 'ConnectionError', message)
87
+
88
+ rescue RestClient::ExceptionWithResponse => e
89
+ if code = e.http_code and body = e.http_body
90
+ begin
91
+ json_obj = JSON.parse(body)
92
+ rescue JSON::ParserError
93
+ raise_api_error(code, body)
94
+ end
95
+ error = json_obj['error'] || 'error'
96
+ message = json_obj['message'] || 'Unexpected error'
97
+
98
+ raise OptimizePlayer::Errors::ApiError.new(code, error, message)
99
+ else
100
+ message = "Unexpected error communicating with OptimizePlayer."
101
+ raise OptimizePlayer::Errors::UnhandledError.new(nil, 'UnhandledError', message)
102
+ end
103
+ end
104
+
105
+ begin
106
+ JSON.parse(response.body)
107
+ rescue JSON::ParserError => e
108
+ raise_api_error(response.code, response.body)
109
+ end
110
+ end
111
+
112
+ private
113
+
114
+ def request_headers
115
+ headers = {
116
+ :user_agent => "OptimizePlayer/v1 RubyBindings/#{OptimizePlayer::VERSION}",
117
+ :content_type => 'application/json'
118
+ }
119
+
120
+ begin
121
+ headers.update(:x_optimize_player_client_user_agent => JSON.generate(user_agent))
122
+ rescue => e
123
+ headers.update(:x_optimize_player_client_raw_user_agent => user_agent.inspect,
124
+ :error => "#{e} (#{e.class})")
125
+ end
126
+ end
127
+
128
+ def raise_api_error(code, body)
129
+ message = "Invalid response object from API: #{body.inspect}"
130
+ raise OptimizePlayer::Errors::ApiError.new(code, 'InvalidResponse', message)
131
+ end
132
+
133
+ def uri_encode(params)
134
+ flatten_params(params).map { |k,v| "#{k}=#{url_encode(v)}" }.join('&')
135
+ end
136
+
137
+ def url_encode(key)
138
+ URI.escape(key.to_s, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
139
+ end
140
+
141
+ def flatten_params(params)
142
+ result = []
143
+ params.each do |key, value|
144
+ calculated_key = url_encode(key)
145
+ if value.is_a?(Hash)
146
+ result += flatten_params(value, calculated_key)
147
+ elsif value.is_a?(Array)
148
+ result += flatten_params_array(value, calculated_key)
149
+ else
150
+ result << [calculated_key, value]
151
+ end
152
+ end
153
+ result
154
+ end
155
+
156
+ def user_agent
157
+ @uname ||= get_uname
158
+ lang_version = "#{RUBY_VERSION} p#{RUBY_PATCHLEVEL} (#{RUBY_RELEASE_DATE})"
159
+
160
+ {
161
+ :bindings_version => OptimizePlayer::VERSION,
162
+ :lang => 'ruby',
163
+ :lang_version => lang_version,
164
+ :platform => RUBY_PLATFORM,
165
+ :publisher => 'optimizeplayer',
166
+ :uname => @uname
167
+ }
168
+
169
+ end
170
+
171
+ def get_uname
172
+ `uname -a 2>/dev/null`.strip if RUBY_PLATFORM =~ /linux|darwin/i
173
+ rescue Errno::ENOMEM => ex
174
+ "uname lookup failed"
175
+ end
176
+ end
177
+ end