pili 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6918ddfd702871e46a8c932baea8cbf0c16a457d
4
- data.tar.gz: b2617beca716642aea1c4e3f9a0edb89e591ef88
3
+ metadata.gz: 1493919dcc89f8b6bb9168d6a1c9d85705274015
4
+ data.tar.gz: d438490de6ce1e2bdb4943633c7d360bf3e7e037
5
5
  SHA512:
6
- metadata.gz: 02b7b8c625d8158361e548e301762057c78c2f7752e2cc2301a405453b1d15735c045df02ad5b798de5f46dadb4007cbcc3e3b7faca55da077048d15bd10c733
7
- data.tar.gz: ecdc2ab510ab493bc3f55ac63b4c61f538ffa28dbb9d03ee8b146bd3e48442990956bc8ab6d3dd656a3c095182980a1b830d41192624bbc19a1f533ea5e9e77a
6
+ metadata.gz: 5a0be7f785b1b5250557cd773008bb2612fc041fdb4bd7a5cb96ca0dc24d9a96abb2c7f8bb883b3dcb984361297c6a0226ccad980149cbe106f2d2078b4d6d56
7
+ data.tar.gz: 8fc2c7a27ed24045fbcf499ec20b2dd826f5e7403bbe09132a379f614c61cea9414fd4982c700badf64819a4ec9c248b01b49974806e20311ba7773ab2017d55
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in pili.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,23 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ pili (0.0.1)
5
+ httparty (~> 0.13.3)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ httparty (0.13.3)
11
+ json (~> 1.8)
12
+ multi_xml (>= 0.5.2)
13
+ json (1.8.2)
14
+ multi_xml (0.5.5)
15
+ rake (10.4.2)
16
+
17
+ PLATFORMS
18
+ ruby
19
+
20
+ DEPENDENCIES
21
+ bundler (~> 1.7)
22
+ pili!
23
+ rake (~> 10.0)
data/README.md CHANGED
@@ -1,4 +1,102 @@
1
- pili-ruby
1
+ # pili-ruby
2
2
  =========
3
3
 
4
4
  pili-io Ruby SDK
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'pili'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install pili
19
+
20
+ ## Usage
21
+
22
+ ### Initialize
23
+
24
+ require 'pili'
25
+
26
+ Pili.setup! access_key: '<YOUR_APP_ACCESS_KEY>',
27
+ secret_key: '<YOUR_APP_SECRET_KEY>'
28
+ #### with rails:
29
+
30
+ You'll need to configure it in config/initializes/pili.rb
31
+
32
+
33
+ ### Create Stream
34
+
35
+ Pili.create_stream(hub_name)
36
+
37
+ # with options
38
+ stream_options = {
39
+ title: "test_name", # optional, default is auto-generated
40
+ publish_key: "test_key", # optional, a secret key for signing the <publishToken>
41
+ publish_security: "dynamic" # optional, can be "dynamic" or "static", default is "dynamic"
42
+ }
43
+
44
+ Pili.create_stream(hub_name, stream_options)
45
+
46
+ ### Get Stream
47
+
48
+ Pili.create_stream(stream_id)
49
+
50
+ ### Update Stream
51
+
52
+ Pili.update_stream(stream_id, publish_key: publish_key, publish_security: publish_security)
53
+
54
+ ### Get Stream List
55
+
56
+ Pili.stream_list(hub)
57
+
58
+ # marker, limit: integer, optional
59
+ Pili.stream_list(hub, marker: marker, limit: limit)
60
+
61
+ ### Delete Stream
62
+
63
+ Pili.delete_stream(stream_id)
64
+
65
+ ### Get Stream Segments
66
+ Pili.get_stream_segments(stream_id)
67
+
68
+ # start, end: integer, optional
69
+ Pili.get_stream_segments(stream_id, start: timestamp, end: timestamp)
70
+
71
+ ### Get Stream Publish URL
72
+
73
+ Pili.get_stream_publish_url(stream_id, publish_key, publish_security)
74
+ Pili.get_stream_publish_url(stream_id, publish_key, publish_security, timestamp)
75
+
76
+ ### Get Stream RTMP Live URL
77
+
78
+ Pili.get_stream_rtmp_live_url(stream_id)
79
+ Pili.get_stream_rtmp_live_url(stream_id, preset)
80
+
81
+ ### Get Stream HLS Live URL
82
+
83
+ Pili.get_stream_hls_live_url(stream_id)
84
+ Pili.get_stream_hls_live_url(stream_id, preset)
85
+
86
+ ### Get Stream HLS Playback URL
87
+
88
+ Pili.get_stream_hls_playback_url(stream_id, start_second, end_second)
89
+ Pili.get_stream_hls_playback_url(stream_id, start_second, end_second, preset)
90
+
91
+
92
+ ## Contributing
93
+
94
+ 1. Fork it
95
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
96
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
97
+ 4. Push to the branch (`git push origin my-new-feature`)
98
+ 5. Create new Pull Request
99
+
100
+ ## Contributors
101
+
102
+ See the [Contributors List](https://github.com/pili-io/pili-sdk-ruby/graphs/contributors).
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/lib/pili/auth.rb ADDED
@@ -0,0 +1,55 @@
1
+ # coding: utf-8
2
+ require 'openssl'
3
+ require 'base64'
4
+ require 'uri'
5
+
6
+ module Pili
7
+ module Auth
8
+ class << self
9
+
10
+ def base64_url_encode(string)
11
+ Base64.encode64(string).tr("+/", "-_").gsub(/[\n\r]?/, "")
12
+ end
13
+
14
+ def digest(secret, bytes)
15
+ OpenSSL::HMAC.digest(OpenSSL::Digest.new('sha1'), secret, bytes)
16
+ end
17
+
18
+ def sign(secret, bytes)
19
+ base64_url_encode(digest(secret, bytes))
20
+ end
21
+
22
+ def generate_signature(options = {})
23
+ method = options[:method]
24
+ url = options[:url]
25
+ content_type = options[:content_type]
26
+ body = options[:body]
27
+
28
+ uri = URI.parse(url)
29
+
30
+ signature = "#{method} #{uri.path}"
31
+
32
+ query_string = uri.query
33
+
34
+ if !query_string.nil? && !query_string.empty?
35
+ signature += '?' + query_string
36
+ end
37
+
38
+ signature += "\nHost: #{uri.host}"
39
+
40
+ if !content_type.nil? && !content_type.empty? && content_type != "application/octet-stream"
41
+ signature += "\nContent-Type: #{content_type}"
42
+ end
43
+
44
+ signature += "\n\n"
45
+
46
+ if body.is_a?(Hash)
47
+ signature += body.to_json
48
+ end
49
+
50
+ return signature
51
+ end
52
+
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,57 @@
1
+ # coding: utf-8
2
+ module Pili
3
+ module Config
4
+ class << self
5
+
6
+ DEFAULT_OPTIONS = {
7
+ :api_version => "v1",
8
+ :api_scheme => "http",
9
+ :api_host => "pili.qiniuapi.com",
10
+
11
+ :rtmp_publish_host => "pub.z1.glb.pili.qiniup.com",
12
+ :rtmp_play_host => "live.z1.glb.pili.qiniucdn.com",
13
+ :hls_play_host => "hls.z1.glb.pili.qiniuapi.com"#,
14
+
15
+ # :access_key => "",
16
+ # :secret_key => ""
17
+ }
18
+
19
+ REQUIRED_OPTION_KEYS = [:access_key, :secret_key]
20
+
21
+ attr_reader :settings
22
+
23
+ def init(options = {})
24
+ @settings = DEFAULT_OPTIONS.merge!(options)
25
+ REQUIRED_OPTION_KEYS.each do |opt|
26
+ raise("You did not provide both required args. Please provide the #{opt}.") unless @settings.has_key?(opt)
27
+ end
28
+ @settings
29
+ end
30
+
31
+ def api_base_url
32
+ "#{settings[:api_scheme]}://#{settings[:api_host]}/#{settings[:api_version]}"
33
+ end
34
+
35
+ def access_key
36
+ settings[:access_key]
37
+ end
38
+
39
+ def secret_key
40
+ settings[:secret_key]
41
+ end
42
+
43
+ def rtmp_publish_host
44
+ settings[:rtmp_publish_host]
45
+ end
46
+
47
+ def rtmp_play_host
48
+ settings[:rtmp_play_host]
49
+ end
50
+
51
+ def hls_play_host
52
+ settings[:hls_play_host]
53
+ end
54
+
55
+ end
56
+ end
57
+ end
data/lib/pili/http.rb ADDED
@@ -0,0 +1,62 @@
1
+ # coding: utf-8
2
+ require 'httparty'
3
+
4
+ module Pili
5
+ class HTTP
6
+
7
+ include HTTParty
8
+ format :json
9
+
10
+ class << self
11
+
12
+ def api_get(url)
13
+ signature_options = {
14
+ :url => url,
15
+ :method => "GET"
16
+ }
17
+
18
+ encoded_sign = Auth.sign(Config.secret_key, Auth.generate_signature(signature_options))
19
+
20
+ headers = { "Authorization" => "Qiniu #{Config.access_key}:#{encoded_sign}" }
21
+
22
+ get(url, :headers => headers)
23
+ end
24
+
25
+
26
+ def api_post(url, body)
27
+ signature_options = {
28
+ :url => url,
29
+ :content_type => "application/json",
30
+ :method => "POST",
31
+ :body => body
32
+ }
33
+
34
+ encoded_sign = Auth.sign(Config.secret_key, Auth.generate_signature(signature_options))
35
+
36
+ headers = {
37
+ "Authorization" => "Qiniu #{Config.access_key}:#{encoded_sign}",
38
+ "Content-Type" => "application/json"
39
+ }
40
+
41
+ post(url, :headers => headers, :body => body.to_json)
42
+ end
43
+
44
+
45
+ def api_delete(url)
46
+ signature_options = {
47
+ :url => url,
48
+ :method => "DELETE"
49
+ }
50
+
51
+ encoded_sign = Auth.sign(Config.secret_key, Auth.generate_signature(signature_options))
52
+
53
+ headers = { "Authorization" => "Qiniu #{Config.access_key}:#{encoded_sign}" }
54
+
55
+ delete(url, :headers => headers)
56
+ end
57
+
58
+
59
+ end
60
+
61
+ end
62
+ end
data/lib/pili/utils.rb ADDED
@@ -0,0 +1,13 @@
1
+ # coding: utf-8
2
+
3
+ module Pili
4
+ module Utils
5
+ class << self
6
+
7
+ def blank?(arg)
8
+ arg.to_s.strip.length == 0
9
+ end
10
+
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,3 @@
1
+ module Pili
2
+ VERSION = "0.1.0"
3
+ end
data/lib/pili.rb ADDED
@@ -0,0 +1,142 @@
1
+ # coding: utf-8
2
+ require 'json'
3
+ require 'httparty'
4
+ require "pili/version"
5
+
6
+ module Pili
7
+ autoload :Auth, 'pili/auth'
8
+ autoload :Config, 'pili/config'
9
+ autoload :HTTP, 'pili/http'
10
+ autoload :Utils, 'pili/utils'
11
+
12
+ class << self
13
+
14
+ def setup!(options = {})
15
+ Config.init(options)
16
+ end
17
+
18
+
19
+ def stream_list(hub, options = {})
20
+ url = Config.api_base_url + "/streams?hub=#{hub}"
21
+
22
+ url += "&marker=#{options[:marker]}" if options[:marker].is_a?(Fixnum)
23
+ url += "&limit=#{options[:limit]}" if options[:limit].is_a?(Fixnum)
24
+
25
+ response = HTTP.api_get(url)
26
+ response.parsed_response
27
+ end
28
+
29
+
30
+ def create_stream(hub, options = {})
31
+ url = Config.api_base_url + "/streams"
32
+
33
+ body = {
34
+ :hub => hub,
35
+ :title => options[:title],
36
+ :publishKey => options[:publish_key],
37
+ :publishSecurity => options[:publish_security] == "static" ? "static" : "dynamic",
38
+ :clientIp => options[:client_ip]
39
+ }
40
+
41
+ body.delete_if { |k, v| v.nil? }
42
+
43
+ response = HTTP.api_post(url, body)
44
+ response.parsed_response
45
+ end
46
+
47
+
48
+ def get_stream(stream_id)
49
+ url = Config.api_base_url + "/streams/" + stream_id
50
+ response = HTTP.api_get(url)
51
+ response.parsed_response
52
+ end
53
+
54
+
55
+ def update_stream(stream_id, options = {})
56
+ url = Config.api_base_url + "/streams/" + stream_id
57
+
58
+ body = {
59
+ :publishKey => options[:publish_key],
60
+ :publishSecurity => options[:publish_security] == "static" ? "static" : "dynamic",
61
+ }
62
+
63
+ body.delete_if { |k, v| v.nil? }
64
+
65
+ response = HTTP.api_post(url, body)
66
+ response.parsed_response
67
+ end
68
+
69
+
70
+ def delete_stream(stream_id)
71
+ url = Config.api_base_url + "/streams/" + stream_id
72
+ response = HTTP.api_delete(url)
73
+ response.parsed_response
74
+ end
75
+
76
+
77
+ def get_stream_segments(stream_id, options = {})
78
+ url = Config.api_base_url + "/streams/#{stream_id}/segments"
79
+
80
+ url += "?start=#{options[:start]}" if options[:start].is_a?(Fixnum)
81
+ url += "&end=#{options[:end]}" if options[:end].is_a?(Fixnum)
82
+
83
+ response = HTTP.api_get(url)
84
+ response.parsed_response
85
+ end
86
+
87
+
88
+ def get_stream_publish_url(stream_id, publish_key, publish_security, nonce = nil)
89
+ nonce = nonce.to_i
90
+ nonce = (Time.now.to_f * 1000.0).to_i if nonce == 0
91
+
92
+ a = stream_id.split(".")
93
+ hub, title = a[1], a[2]
94
+
95
+ if publish_security == "static"
96
+ return "rtmp://#{Config.rtmp_publish_host}/#{hub}/#{title}?key=#{publish_key}"
97
+ else
98
+ token = Auth.sign(publish_key, "/#{hub}/#{title}?nonce=#{nonce}")
99
+ return "rtmp://#{Config.rtmp_publish_host}/#{hub}/#{title}?nonce=#{nonce}&token=#{token}"
100
+ end
101
+ end
102
+
103
+
104
+ def get_stream_rtmp_live_url(stream_id, preset = nil)
105
+ a = stream_id.split(".")
106
+ hub, title = a[1], a[2]
107
+
108
+ if Utils.blank? preset
109
+ return "rtmp://#{Config.rtmp_play_host}/#{hub}/#{title}"
110
+ else
111
+ return "rtmp://#{Config.rtmp_play_host}/#{hub}/#{title}@#{preset}"
112
+ end
113
+ end
114
+
115
+
116
+ def get_stream_hls_live_url(stream_id, preset = nil)
117
+ a = stream_id.split(".")
118
+ hub, title = a[1], a[2]
119
+
120
+ if Utils.blank? preset
121
+ return "http://#{Config.hls_play_host}/#{hub}/#{title}.m3u8"
122
+ else
123
+ return "http://#{Config.hls_play_host}/#{hub}/#{title}@#{preset}.m3u8"
124
+ end
125
+ end
126
+
127
+
128
+ def get_stream_hls_playback_url(stream_id, start_second, end_second, preset = nil)
129
+ a = stream_id.split(".")
130
+ hub, title = a[1], a[2]
131
+
132
+ if Utils.blank? preset
133
+ return "http://#{Config.hls_play_host}/#{hub}/#{title}.m3u8?start=#{start_second}&end=#{end_second}"
134
+ else
135
+ return "http://#{Config.hls_play_host}/#{hub}/#{title}@#{preset}.m3u8?start=#{start_second}&end=#{end_second}"
136
+ end
137
+ end
138
+
139
+
140
+ end
141
+
142
+ end
data/pili.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'pili/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "pili"
8
+ spec.version = Pili::VERSION
9
+ spec.authors = ["Miclle"]
10
+ spec.email = ["pili@qiniu.com"]
11
+ spec.summary = %q{Pili SDK for Ruby.}
12
+ spec.description = %q{Pili SDK for Ruby. https://github.com/pili-io/pili-sdk-ruby}
13
+ spec.homepage = "https://github.com/pili-io/pili-sdk-ruby"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+
24
+ spec.add_runtime_dependency 'httparty', '~> 0.13.3'
25
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pili
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Miclle
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-23 00:00:00.000000000 Z
11
+ date: 2015-04-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -38,7 +38,21 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '10.0'
41
- description: pili-io Ruby SDK.
41
+ - !ruby/object:Gem::Dependency
42
+ name: httparty
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.13.3
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.13.3
55
+ description: Pili SDK for Ruby. https://github.com/pili-io/pili-sdk-ruby
42
56
  email:
43
57
  - pili@qiniu.com
44
58
  executables: []
@@ -46,8 +60,18 @@ extensions: []
46
60
  extra_rdoc_files: []
47
61
  files:
48
62
  - ".gitignore"
63
+ - Gemfile
64
+ - Gemfile.lock
49
65
  - LICENSE
50
66
  - README.md
67
+ - Rakefile
68
+ - lib/pili.rb
69
+ - lib/pili/auth.rb
70
+ - lib/pili/config.rb
71
+ - lib/pili/http.rb
72
+ - lib/pili/utils.rb
73
+ - lib/pili/version.rb
74
+ - pili.gemspec
51
75
  homepage: https://github.com/pili-io/pili-sdk-ruby
52
76
  licenses:
53
77
  - MIT
@@ -71,5 +95,5 @@ rubyforge_project:
71
95
  rubygems_version: 2.4.5
72
96
  signing_key:
73
97
  specification_version: 4
74
- summary: pili-io Ruby SDK.
98
+ summary: Pili SDK for Ruby.
75
99
  test_files: []