xio 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d91bf05d24c78225d24b871a651e2f16490511ca
4
+ data.tar.gz: 8a56ae6132b59d92996366886053334c1ed7e2a3
5
+ SHA512:
6
+ metadata.gz: d6f622fd3c7eea662a7baff03a2e87ea01a199dbd0e19fc37c1c88cdb18b65f990f62e7b7fece4a9bb9feee32be152bb3124ca6863c09a46b37c870fa51889dc
7
+ data.tar.gz: eac7810ffdf6f92d795721d6cd589689c70fd67ac352447fcce773ce8f42ddbecb1368dfee40b910f1379ef53a081cf821ac02ad8fc28443e25f279bc7a582df
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (C) 2014 OTOY, Inc.
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.markdown ADDED
@@ -0,0 +1,3 @@
1
+ # X.IO
2
+
3
+ This needs to be filled out...
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'rake/testtask'
4
+
5
+ Rake::TestTask.new(:test) do |test|
6
+ test.libs << 'lib' << 'test'
7
+ test.pattern = 'test/**/*_test.rb'
8
+ test.verbose = true
9
+ end
10
+
11
+ task :default => :test
data/lib/xio.rb ADDED
@@ -0,0 +1,20 @@
1
+ # Standard Library
2
+ require 'cgi'
3
+ require 'net/https'
4
+ require 'timeout'
5
+
6
+ # JSON serialization
7
+ require 'multi_json'
8
+
9
+ # X.IO
10
+ require 'xio/version'
11
+ require 'xio/serializer'
12
+ require 'xio/xio'
13
+ require 'xio/resource'
14
+ require 'xio/http/net_http'
15
+ require 'xio/http/typhoeus'
16
+ require 'xio/http'
17
+ require 'xio/errors'
18
+ require 'xio/stream'
19
+ require 'xio/response'
20
+ require 'xio/version'
data/lib/xio/errors.rb ADDED
@@ -0,0 +1,45 @@
1
+ module Xio
2
+ class Error < StandardError
3
+
4
+ def initialize(error_or_message)
5
+ if error_or_message.is_a?(Exception)
6
+ @error = error_or_message
7
+ else
8
+ @message = error_or_message
9
+ end
10
+ end
11
+
12
+ def message
13
+ @message || "#{@error.class} (wrapped in a #{self.class}) - #{@error.message}"
14
+ end
15
+
16
+ def backtrace
17
+ if @error
18
+ error_backtrace = ["--- Backtrace from #{@error.class} ---"] + (@error.backtrace || [])
19
+ wrapper_backtrace = ["--- Backtrace from #{self.class} ---"] + (super || [])
20
+ error_backtrace + wrapper_backtrace
21
+ else
22
+ super
23
+ end
24
+ end
25
+
26
+ def inspect
27
+ if @error
28
+ "#{@error.inspect} (wrapped in a #{self.class})"
29
+ else
30
+ super
31
+ end
32
+ end
33
+
34
+ def to_s
35
+ if @error
36
+ "#{@error.class} (wrapped in a #{self.class}) - #{@error}"
37
+ else
38
+ super
39
+ end
40
+ end
41
+ end
42
+
43
+ class HTTPError < Error; end
44
+
45
+ end
data/lib/xio/http.rb ADDED
@@ -0,0 +1,99 @@
1
+ module Xio
2
+ class HTTP
3
+
4
+ include Xio::Serializer
5
+
6
+ attr_accessor :url, :options, :method
7
+
8
+ class << self
9
+ attr_accessor :default_options, :http_backend
10
+ end
11
+
12
+ self.http_backend = NetHTTP
13
+
14
+ self.default_options = {:timeout => 60000,
15
+ :headers => {'Accept' => 'application/json',
16
+ 'Content-Type' => 'application/json',
17
+ 'User-Agent' => "Xio-rb v#{Xio::GEM_VERSION}"}}
18
+
19
+ def initialize(method, url, options={})
20
+ self.method = method
21
+ self.url = url
22
+ self.options = options
23
+ end
24
+
25
+ def self.post(url, body, options={})
26
+ new(:post, url, options.merge(:body => body)).perform_method
27
+ end
28
+
29
+ def self.put(url, body, options={})
30
+ new(:put, url, options.merge(:body => body)).perform_method
31
+ end
32
+
33
+ def self.get(url, options={})
34
+ new(:get, url, options).perform_method
35
+ end
36
+
37
+ def self.delete(url, options={})
38
+ new(:delete, url, options).perform_method
39
+ end
40
+
41
+ def perform_method
42
+ process(http_backend.send(method, url, options))
43
+ rescue StandardError => e
44
+ raise HTTPError.new(e)
45
+ end
46
+
47
+ def options=(value)
48
+ value ||= {}
49
+
50
+ # Hacky, deeper hash merge
51
+ default_options.keys.each do |key|
52
+ if value.has_key?(key)
53
+ if value[key].is_a?(Hash) && default_options[key].is_a?(Hash)
54
+ value[key] = default_options[key].merge(value[key])
55
+ end
56
+ else
57
+ value[key] = default_options[key]
58
+ end
59
+ end
60
+
61
+ @options = value
62
+ end
63
+
64
+ def options
65
+ @options || self.options = default_options
66
+ end
67
+
68
+ def http_backend
69
+ self.class.http_backend
70
+ end
71
+
72
+ def default_options
73
+ self.class.default_options
74
+ end
75
+
76
+ def inspect
77
+ "#{method.to_s.upcase} #{url}\nOptions: " + options.inspect
78
+ end
79
+
80
+ protected
81
+
82
+ def process(http_response)
83
+ response = Response.new
84
+ response.request = self
85
+ response.code = http_response.code
86
+
87
+ begin
88
+ response.body = decode(http_response.body.to_s)
89
+ rescue StandardError # Hack! Returns different exceptions depending on the decoding engine
90
+ response.body = http_response.body
91
+ end
92
+
93
+ response.raw_body = http_response.body
94
+ response.raw_response = http_response
95
+ response
96
+ end
97
+
98
+ end
99
+ end
@@ -0,0 +1,132 @@
1
+ module Xio
2
+ class HTTP
3
+ class NetHTTP
4
+
5
+ attr_accessor :method, :url, :uri, :body, :params, :headers, :timeout, :skip_ssl_verify, :options, :ca_file, :ca_path
6
+
7
+ def initialize(method, url, options)
8
+ @method = method
9
+ @url = url
10
+ @options = options.dup
11
+ @body = @options.delete(:body)
12
+ @params = @options.delete(:params)
13
+ @headers = @options.delete(:headers)
14
+ @timeout = @options.delete(:timeout)
15
+ @skip_ssl_verify = @options.delete(:skip_ssl_verify)
16
+ @ca_file = @options.delete(:ca_file)
17
+ @ca_path = @options.delete(:ca_path)
18
+ end
19
+
20
+ def self.post(url, options={})
21
+ new(:post, url, options).perform
22
+ end
23
+
24
+ def self.put(url, options={})
25
+ new(:put, url, options).perform
26
+ end
27
+
28
+ def self.get(url, options={})
29
+ new(:get, url, options).perform
30
+ end
31
+
32
+ def self.delete(url, options={})
33
+ new(:delete, url, options).perform
34
+ end
35
+
36
+ def perform
37
+ deliver(http, request)
38
+ end
39
+
40
+
41
+ protected
42
+
43
+ def deliver(http, request)
44
+ if timeout
45
+ Timeout.timeout(timeout / 1000.0) do
46
+ http.request(request)
47
+ end
48
+ else
49
+ http.request(request)
50
+ end
51
+ end
52
+
53
+ def http
54
+ u = uri
55
+
56
+ http = Net::HTTP.new(u.host, u.port)
57
+
58
+ if u.scheme == 'https'
59
+ http.use_ssl = true
60
+
61
+ if skip_ssl_verify
62
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
63
+ else
64
+ http.verify_mode = OpenSSL::SSL::VERIFY_PEER
65
+
66
+ http.cert_store = OpenSSL::X509::Store.new
67
+ http.cert_store.set_default_paths
68
+
69
+ if defined?(OpenSSL::X509::V_FLAG_CRL_CHECK_ALL)
70
+ http.cert_store.flags = OpenSSL::X509::V_FLAG_CRL_CHECK_ALL
71
+ end
72
+
73
+ http.cert_store.add_file(ca_file) if ca_file
74
+ http.cert_store.add_path(ca_path) if ca_path
75
+ end
76
+ end
77
+
78
+ http
79
+ end
80
+
81
+ def request
82
+ r = request_class.new(path)
83
+ if body
84
+ r.body = body
85
+ elsif [:post, :put].include?(@method)
86
+ r.body = ""
87
+ end
88
+
89
+ if headers
90
+ headers.each do |header, value|
91
+ r.add_field(header.to_s, value.to_s)
92
+ end
93
+ end
94
+ r
95
+ end
96
+
97
+ def uri
98
+ u = URI.parse(url)
99
+
100
+ if params
101
+ params_as_query = params.map{|k,v| "#{CGI.escape(k.to_s)}=#{CGI.escape(v.to_s)}"}.join('&')
102
+ if u.query.to_s.empty?
103
+ u.query = params_as_query
104
+ else
105
+ u.query = [u.query.to_s, params_as_query].join('&')
106
+ end
107
+ end
108
+
109
+ u
110
+ end
111
+
112
+ def path
113
+ u = uri
114
+
115
+ if u.path.empty?
116
+ u.path = '/'
117
+ end
118
+
119
+ if u.query.to_s.empty?
120
+ u.path
121
+ else
122
+ u.path + '?' + u.query.to_s
123
+ end
124
+ end
125
+
126
+ def request_class
127
+ Net::HTTP.const_get(method.to_s.capitalize)
128
+ end
129
+
130
+ end
131
+ end
132
+ end
@@ -0,0 +1,40 @@
1
+ module Xio
2
+ class HTTP
3
+ class Typhoeus
4
+
5
+ def self.post(url, options={})
6
+ perform(:post, url, options)
7
+ end
8
+
9
+ def self.put(url, options={})
10
+ perform(:put, url, options)
11
+ end
12
+
13
+ def self.get(url, options={})
14
+ perform(:get, url, options)
15
+ end
16
+
17
+ def self.delete(url, options={})
18
+ perform(:delete, url, options)
19
+ end
20
+
21
+ def self.perform(method, url, options={})
22
+ options = options.dup
23
+ if options.delete(:skip_ssl_verify)
24
+ options[:disable_ssl_peer_verification] = true
25
+ end
26
+
27
+ if ca_file = options.delete(:ca_file)
28
+ options[:sslcert] = ca_file
29
+ end
30
+
31
+ if ca_path = options.delete(:ca_path)
32
+ options[:capath] = ca_path
33
+ end
34
+
35
+ ::Typhoeus::Request.send(method, url, options)
36
+ end
37
+
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,77 @@
1
+ module Xio
2
+ class Resource
3
+
4
+ include Xio::Serializer
5
+
6
+ def self.api_key_id
7
+ Xio.api_key_id
8
+ end
9
+
10
+ def self.api_secret_key
11
+ Xio.api_secret_key
12
+ end
13
+
14
+ def self.base_url
15
+ Xio.base_url
16
+ end
17
+
18
+ def self.post(path, params={}, options={})
19
+ options = options.dup
20
+ url = url_for(path, options)
21
+ body = encode(params)
22
+ options = add_api_key_header(options)
23
+ HTTP.post(url, body, options)
24
+ end
25
+
26
+ def self.put(path, params={}, options={})
27
+ options = options.dup
28
+ url = url_for(path, options)
29
+ body = encode(params)
30
+ options = add_api_key_header(options)
31
+ HTTP.put(url, body, options)
32
+ end
33
+
34
+ def self.get(path, options={})
35
+ options = options.dup
36
+ url = url_for(path, options)
37
+ options = add_api_key_header(options)
38
+ HTTP.get(url, options)
39
+ end
40
+
41
+ def self.delete(path, options={})
42
+ options = options.dup
43
+ url = url_for(path)
44
+ options = add_api_key_header(options)
45
+ HTTP.delete(url, options)
46
+ end
47
+
48
+
49
+ protected
50
+
51
+ def self.url_for(path, options={})
52
+ File.join((options[:base_url] || base_url).to_s, path.to_s)
53
+ end
54
+
55
+ def self.add_api_key_header(options)
56
+ if options[:headers]
57
+ options[:headers] = options[:headers].dup
58
+ else
59
+ options[:headers] = {}
60
+ end
61
+ options[:headers]["XIO-API-Key-ID"] = api_key_id
62
+ options[:headers]["XIO-API-Secret-Key"] = api_secret_key
63
+
64
+ options
65
+ end
66
+
67
+ def self.merge_params(options, params)
68
+ if options[:params]
69
+ options[:params] = options[:params].merge(params)
70
+ options
71
+ else
72
+ options.merge(:params => params)
73
+ end
74
+ end
75
+
76
+ end
77
+ end
@@ -0,0 +1,33 @@
1
+ module Xio
2
+ class Response
3
+
4
+ attr_accessor :request, :code, :body, :raw_body, :raw_response
5
+
6
+ def initialize(options={})
7
+ options.each do |k, v|
8
+ send("#{k}=", v) if respond_to?("#{k}=")
9
+ end
10
+ end
11
+
12
+ def success?
13
+ code.to_i > 199 && code.to_i < 300
14
+ end
15
+
16
+ def errors
17
+ if body.is_a?(Hash)
18
+ Array(body['errors']).compact
19
+ else
20
+ []
21
+ end
22
+ end
23
+
24
+ def body_without_wrapper
25
+ if body.is_a?(Hash) && body['api_response']
26
+ body['api_response']
27
+ else
28
+ body
29
+ end
30
+ end
31
+
32
+ end
33
+ end
@@ -0,0 +1,35 @@
1
+ module Xio
2
+ module Serializer
3
+
4
+ extend self
5
+
6
+ def self.included(klass)
7
+ klass.extend(self)
8
+ end
9
+
10
+ def encode(content)
11
+ if content.is_a?(String) || content.nil?
12
+ content
13
+ else
14
+ if MultiJson.respond_to?(:dump)
15
+ MultiJson.dump(content)
16
+ else
17
+ MultiJson.encode(content)
18
+ end
19
+ end
20
+ end
21
+
22
+ def decode(content)
23
+ if content.is_a?(String)
24
+ if MultiJson.respond_to?(:dump)
25
+ MultiJson.load(content)
26
+ else
27
+ MultiJson.decode(content)
28
+ end
29
+ else
30
+ content
31
+ end
32
+ end
33
+
34
+ end
35
+ end
data/lib/xio/stream.rb ADDED
@@ -0,0 +1,17 @@
1
+ module Xio
2
+ class Stream < Resource
3
+
4
+ def self.create(params={}, options={})
5
+ post("/streams", params, options)
6
+ end
7
+
8
+ def self.details(stream_id, options={})
9
+ get("/streams/#{stream_id}", options)
10
+ end
11
+
12
+ def self.terminate(stream_id, options={})
13
+ delete("/streams/#{stream_id}", nil, options)
14
+ end
15
+
16
+ end
17
+ end
@@ -0,0 +1,3 @@
1
+ module Xio
2
+ GEM_VERSION = '1.0.0'
3
+ end
data/lib/xio/xio.rb ADDED
@@ -0,0 +1,23 @@
1
+ module Xio
2
+
3
+ class << self
4
+ attr_accessor :api_key_id, :api_secret_key, :base_url
5
+ end
6
+
7
+ self.api_key_id = nil
8
+ self.api_secret_key = nil
9
+ self.base_url = 'https://api.x.io/v1'
10
+
11
+ def self.api_key_id
12
+ @api_key_id || ENV['XIO_API_KEY_ID']
13
+ end
14
+
15
+ def self.api_secret_key
16
+ @api_secret_key || ENV['XIO_API_SECRET_KEY']
17
+ end
18
+
19
+ def self.base_url(env=nil)
20
+ @base_url
21
+ end
22
+
23
+ end
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: xio
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Mike Christopher
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-08-05 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: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: X.IO <http://x.io> integration library.
28
+ email: help@x.io
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - LICENSE
34
+ - README.markdown
35
+ - Rakefile
36
+ - lib/xio.rb
37
+ - lib/xio/errors.rb
38
+ - lib/xio/http.rb
39
+ - lib/xio/http/net_http.rb
40
+ - lib/xio/http/typhoeus.rb
41
+ - lib/xio/resource.rb
42
+ - lib/xio/response.rb
43
+ - lib/xio/serializer.rb
44
+ - lib/xio/stream.rb
45
+ - lib/xio/version.rb
46
+ - lib/xio/xio.rb
47
+ homepage: https://github.com/appslingr/xio-rb
48
+ licenses: []
49
+ metadata: {}
50
+ post_install_message:
51
+ rdoc_options: []
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - '>='
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - '>='
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ requirements: []
65
+ rubyforge_project: xio
66
+ rubygems_version: 2.2.2
67
+ signing_key:
68
+ specification_version: 4
69
+ summary: X.IO <http://x.io> integration library.
70
+ test_files: []