streamio 0.5.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.
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source :rubygems
2
+
3
+ gemspec
data/HISTORY ADDED
@@ -0,0 +1,3 @@
1
+ == 0.5.0 2011-06-15
2
+
3
+ * Basic api functionality for Videos, Images and Encoding Profiles
data/README.rdoc ADDED
@@ -0,0 +1,35 @@
1
+ = The Streamio Gem
2
+
3
+ Official ruby wrapper for the http://streamio.com API. Integrating video in your application has never been more awesome.
4
+
5
+ == Installation
6
+
7
+ gem install streamio
8
+
9
+ == Usage
10
+
11
+ Load it.
12
+
13
+ require 'rubygems'
14
+ require 'streamio'
15
+
16
+ Configure it.
17
+
18
+ Streamio.configure do |config|
19
+ config.username = "your_account_name"
20
+ config.password = "your_api_private_key"
21
+ end
22
+
23
+ Use it.
24
+
25
+ # Fetch an array of videos
26
+ videos = Streamio::Video.all
27
+
28
+ # Pass in parameters as specified in the API docs
29
+ # This fetches the 5 most played videos tagged with Nature or Sports
30
+ videos = Streamio::Video.all(:tags => ["Nature", "Sports"], :limit => 5, :order => "plays.desc")
31
+
32
+ # Find a video by id
33
+ video = Streamio::Video.find("4c57f3975412901427000005")
34
+
35
+ Same principles work for the other availible models (Streamio::Image and Streamio::EncodingProfile).
@@ -0,0 +1,23 @@
1
+ module Streamio
2
+ class EncodingProfile < Model
3
+ def self.resource
4
+ RestClient::Resource.new("#{Streamio.authenticated_api_base}/encoding_profiles", :headers => {:accept => :json})
5
+ end
6
+
7
+ CREATEABLE_ATTRIBUTES = []
8
+ ACCESSABLE_ATTRIBUTES = %w(title tags width desired_video_bitrate frame_rate audio_bitrate audio_sample_rate audio_channels)
9
+ READABLE_ATTRIBUTES = %w(id created_at updated_at account_id)
10
+
11
+ (CREATEABLE_ATTRIBUTES + ACCESSABLE_ATTRIBUTES + READABLE_ATTRIBUTES - CASTED_ATTRIBUTES).each do |attribute|
12
+ define_method(attribute) do
13
+ @attributes[attribute]
14
+ end
15
+ end
16
+
17
+ (CREATEABLE_ATTRIBUTES + ACCESSABLE_ATTRIBUTES).each do |attribute|
18
+ define_method("#{attribute}=") do |value|
19
+ @attributes[attribute] = value
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,23 @@
1
+ module Streamio
2
+ class Image < Model
3
+ def self.resource
4
+ RestClient::Resource.new("#{Streamio.authenticated_api_base}/images", :headers => {:accept => :json})
5
+ end
6
+
7
+ CREATEABLE_ATTRIBUTES = %w(file)
8
+ ACCESSABLE_ATTRIBUTES = %w(title tags)
9
+ READABLE_ATTRIBUTES = %w(id created_at updated_at account_id transcodings)
10
+
11
+ (CREATEABLE_ATTRIBUTES + ACCESSABLE_ATTRIBUTES + READABLE_ATTRIBUTES - CASTED_ATTRIBUTES).each do |attribute|
12
+ define_method(attribute) do
13
+ @attributes[attribute]
14
+ end
15
+ end
16
+
17
+ (CREATEABLE_ATTRIBUTES + ACCESSABLE_ATTRIBUTES).each do |attribute|
18
+ define_method("#{attribute}=") do |value|
19
+ @attributes[attribute] = value
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,109 @@
1
+ module Streamio
2
+ class Model
3
+ class << self
4
+ def find(id)
5
+ parse_response(resource["#{id}"].get)
6
+ end
7
+
8
+ def all(parameters = {})
9
+ sanitize_parameters(parameters)
10
+ parse_response(resource.get(:params => parameters))
11
+ end
12
+
13
+ protected
14
+ def parse_response(response)
15
+ response = JSON.parse(response.body)
16
+ if response.instance_of?(Array)
17
+ response.collect do |attributes|
18
+ new(attributes)
19
+ end
20
+ else
21
+ new(response)
22
+ end
23
+ end
24
+
25
+ def sanitize_parameters(params)
26
+ params.each do |key, value|
27
+ params[key] = value.join(",") if value.instance_of?(Array)
28
+ end
29
+ end
30
+ end
31
+
32
+ CASTED_ATTRIBUTES = %w(tags created_at updated_at)
33
+
34
+ attr_reader :attributes, :errors
35
+
36
+ def initialize(attributes = {})
37
+ @errors = {}
38
+ @attributes = attributes.inject(Hash.new) do |options, (key, value)|
39
+ options[key.to_s] = value
40
+ options
41
+ end
42
+ end
43
+
44
+ def save
45
+ if persisted?
46
+ update
47
+ else
48
+ persist
49
+ end
50
+ rescue RestClient::UnprocessableEntity => e
51
+ @errors = JSON.parse(e.response)
52
+ false
53
+ end
54
+
55
+ def destroy
56
+ self.class.resource[id].delete
57
+ @attributes.freeze
58
+ true
59
+ end
60
+
61
+ def persisted?
62
+ !destroyed? && !id.nil?
63
+ end
64
+
65
+ def destroyed?
66
+ @attributes.frozen?
67
+ end
68
+
69
+ def tags
70
+ @attributes["tags"] = [] if @attributes["tags"].nil?
71
+ @attributes["tags"]
72
+ end
73
+
74
+ def created_at
75
+ return nil unless @attributes["created_at"]
76
+ Time.parse(@attributes["created_at"])
77
+ end
78
+
79
+ def updated_at
80
+ return nil unless @attributes["updated_at"]
81
+ Time.parse(@attributes["updated_at"])
82
+ end
83
+
84
+ private
85
+ def update
86
+ parameters = {}
87
+ (self.class::ACCESSABLE_ATTRIBUTES).each do |key|
88
+ parameters[key] = @attributes[key] if @attributes.has_key?(key)
89
+ end
90
+
91
+ self.class.resource[id].put(parameters)
92
+ true
93
+ end
94
+
95
+ def persist
96
+ parameters = {}
97
+ (self.class::CREATEABLE_ATTRIBUTES + self.class::ACCESSABLE_ATTRIBUTES).each do |key|
98
+ parameters[key] = @attributes[key] if @attributes.has_key?(key)
99
+ end
100
+
101
+ new_attributes = JSON.parse(self.class.resource.post(attributes).body)
102
+
103
+ (self.class::ACCESSABLE_ATTRIBUTES + self.class::READABLE_ATTRIBUTES).each do |attribute|
104
+ @attributes[attribute] = new_attributes[attribute]
105
+ end
106
+ true
107
+ end
108
+ end
109
+ end
@@ -0,0 +1,3 @@
1
+ module Streamio
2
+ VERSION = "0.5.0"
3
+ end
@@ -0,0 +1,37 @@
1
+ module Streamio
2
+ class Video < Model
3
+ def self.resource
4
+ RestClient::Resource.new("#{Streamio.authenticated_api_base}/videos", :headers => {:accept => :json})
5
+ end
6
+
7
+ CREATEABLE_ATTRIBUTES = %w(file encoding_profile_ids encoding_profile_tags skip_default_encoding_profiles use_original_as_transcoding)
8
+ ACCESSABLE_ATTRIBUTES = %w(title description tags image_id)
9
+ READABLE_ATTRIBUTES = %w(id state progress aspect_ratio_multiplier plays duration created_at updated_at account_id transcodings)
10
+
11
+ (CREATEABLE_ATTRIBUTES + ACCESSABLE_ATTRIBUTES + READABLE_ATTRIBUTES - CASTED_ATTRIBUTES).each do |attribute|
12
+ define_method(attribute) do
13
+ @attributes[attribute]
14
+ end
15
+ end
16
+
17
+ (CREATEABLE_ATTRIBUTES + ACCESSABLE_ATTRIBUTES).each do |attribute|
18
+ define_method("#{attribute}=") do |value|
19
+ @attributes[attribute] = value
20
+ end
21
+ end
22
+
23
+ def add_transcoding(parameters = {})
24
+ self.class.resource["#{id}/transcodings"].post(parameters)
25
+ true
26
+ rescue RestClient::Exception
27
+ false
28
+ end
29
+
30
+ def delete_transcoding(transcoding_id)
31
+ self.class.resource["#{id}/transcodings/#{transcoding_id}"].delete
32
+ true
33
+ rescue RestClient::Exception
34
+ false
35
+ end
36
+ end
37
+ end
data/lib/streamio.rb ADDED
@@ -0,0 +1,41 @@
1
+ require "rest_client"
2
+ require "json"
3
+ require "time"
4
+
5
+ require "streamio/version"
6
+ require "streamio/model"
7
+ require "streamio/video"
8
+ require "streamio/image"
9
+ require "streamio/encoding_profile"
10
+
11
+ module Streamio
12
+ class << self
13
+ attr_accessor :username
14
+ attr_accessor :password
15
+ attr_accessor :use_ssl
16
+ attr_accessor :host
17
+
18
+ def configure
19
+ yield self
20
+ end
21
+
22
+ def use_ssl
23
+ return true if @use_ssl.nil?
24
+ @use_ssl
25
+ end
26
+ alias :use_ssl? :use_ssl
27
+
28
+ def protocol
29
+ use_ssl? ? "https://" : "http://"
30
+ end
31
+
32
+ def host
33
+ return "streamio.com" if @host.nil?
34
+ @host
35
+ end
36
+
37
+ def authenticated_api_base
38
+ "#{protocol}#{username}:#{password}@#{host}/api/v1"
39
+ end
40
+ end
41
+ end
data/streamio.gemspec ADDED
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "streamio/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "streamio"
7
+ s.version = Streamio::VERSION
8
+ s.authors = ["David Backeus"]
9
+ s.email = ["david@streamio.se"]
10
+ s.homepage = "http://github.com/streamio/streamio-rb"
11
+ s.summary = %q{Ruby wrapper for the Streamio api.}
12
+ s.description = %q{Ruby wrapper for Streamios api.}
13
+
14
+ s.files = Dir.glob("lib/**/*") + %w(Gemfile streamio.gemspec HISTORY README.rdoc)
15
+
16
+ s.add_dependency("rest-client", "~> 1.6.1")
17
+ s.add_dependency("json", ">= 1.4", "< 1.6")
18
+
19
+ s.add_development_dependency("rspec", "~> 2.6.0")
20
+ s.add_development_dependency("webmock", "~> 1.6.4")
21
+ end
metadata ADDED
@@ -0,0 +1,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: streamio
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.5.0
6
+ platform: ruby
7
+ authors:
8
+ - David Backeus
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-06-15 00:00:00 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rest-client
17
+ prerelease: false
18
+ requirement: &id001 !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ~>
22
+ - !ruby/object:Gem::Version
23
+ version: 1.6.1
24
+ type: :runtime
25
+ version_requirements: *id001
26
+ - !ruby/object:Gem::Dependency
27
+ name: json
28
+ prerelease: false
29
+ requirement: &id002 !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: "1.4"
35
+ - - <
36
+ - !ruby/object:Gem::Version
37
+ version: "1.6"
38
+ type: :runtime
39
+ version_requirements: *id002
40
+ - !ruby/object:Gem::Dependency
41
+ name: rspec
42
+ prerelease: false
43
+ requirement: &id003 !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ~>
47
+ - !ruby/object:Gem::Version
48
+ version: 2.6.0
49
+ type: :development
50
+ version_requirements: *id003
51
+ - !ruby/object:Gem::Dependency
52
+ name: webmock
53
+ prerelease: false
54
+ requirement: &id004 !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ~>
58
+ - !ruby/object:Gem::Version
59
+ version: 1.6.4
60
+ type: :development
61
+ version_requirements: *id004
62
+ description: Ruby wrapper for Streamios api.
63
+ email:
64
+ - david@streamio.se
65
+ executables: []
66
+
67
+ extensions: []
68
+
69
+ extra_rdoc_files: []
70
+
71
+ files:
72
+ - lib/streamio/encoding_profile.rb
73
+ - lib/streamio/image.rb
74
+ - lib/streamio/model.rb
75
+ - lib/streamio/version.rb
76
+ - lib/streamio/video.rb
77
+ - lib/streamio.rb
78
+ - Gemfile
79
+ - streamio.gemspec
80
+ - HISTORY
81
+ - README.rdoc
82
+ homepage: http://github.com/streamio/streamio-rb
83
+ licenses: []
84
+
85
+ post_install_message:
86
+ rdoc_options: []
87
+
88
+ require_paths:
89
+ - lib
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ none: false
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: "0"
96
+ required_rubygems_version: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: "0"
102
+ requirements: []
103
+
104
+ rubyforge_project:
105
+ rubygems_version: 1.7.2
106
+ signing_key:
107
+ specification_version: 3
108
+ summary: Ruby wrapper for the Streamio api.
109
+ test_files: []
110
+