mira 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ viddler.credentials
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm ruby-1.8.7-p334@mira
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in mira.gemspec
4
+ gemspec
@@ -0,0 +1,13 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ # copied from RSpec :-p
5
+ require 'rspec'
6
+ require 'rspec/core'
7
+ require 'rspec/core/rake_task'
8
+ desc "Run all examples"
9
+ RSpec::Core::RakeTask.new(:spec) do |t|
10
+ t.rspec_path = 'rspec'
11
+ t.rspec_opts = %w[--color]
12
+ t.verbose = false
13
+ end
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'lib/mira'
3
+ require 'yaml'
4
+
5
+ viddler = Mira::ViddlerClient.new(YAML.load(File.open("viddler.credentials")))
6
+ viddler.upload(File.new("sample.mov"),
7
+ :title => "sample",
8
+ :tags => "your mom",
9
+ :description => "whatever")
10
+
@@ -0,0 +1,8 @@
1
+ module Mira
2
+ require 'json'
3
+ require 'rest_client'
4
+ require 'active_support'
5
+
6
+ require File.dirname(__FILE__) + '/mira/embed_code'
7
+ require File.dirname(__FILE__) + '/mira/upload'
8
+ end
@@ -0,0 +1,78 @@
1
+ module Mira
2
+ module Viddler
3
+ # attribution: this code is based very heavily on the original `viddler` gem
4
+
5
+ # Returns proper HTML code for embedding
6
+ #
7
+ # <tt>options</tt> hash could contain:
8
+ # * <tt>id:</tt> Viddler ID of the video to display (required);
9
+ # * <tt>player_type:</tt> The type of player to embed, either "simple" or "player" (default);
10
+ # * <tt>width:</tt> The width of the player (default is 437);
11
+ # * <tt>height:</tt> The height of the player (default is 370);
12
+ # * <tt>autoplay:</tt> Whether or not to autoplay the video, either
13
+ # "t" or "f" (default is "f");
14
+ # * <tt>playAll:</tt> Set to "true" to enable play all player (requires
15
+ # player_type to be "player");
16
+ #
17
+ # Any additional options passed to the method will be added as flashvars
18
+ #
19
+ # Example:
20
+ #
21
+ # Mira::Viddler.embed_code(:player_type => 'simple',
22
+ # :width => 300,
23
+ # :height => 300,
24
+ # :autoplay => 't',
25
+ # :id => "e75e65b2")
26
+ #
27
+ # Returns embed code for auto playing video e75e65b2 in simple player with 300px width and height
28
+ #
29
+ def self.embed_code(options={})
30
+ raise "hell" unless options[:id]
31
+ options = {
32
+ :player_type => 'player',
33
+ :width => 437,
34
+ :height => 370,
35
+ :autoplay => 'f',
36
+ :wmode => 'transparent'
37
+ }.merge(options)
38
+
39
+ # get non flashvars from options
40
+ player_type = options.delete(:player_type)
41
+ width = options.delete(:width)
42
+ height = options.delete(:height)
43
+ wmode = options.delete(:wmode)
44
+ id = options.delete(:id)
45
+
46
+ flashvars = options.collect{|key,value| "#{key}=#{value}"}.join('&')
47
+
48
+ html = <<-CODE
49
+ <!--[if IE]>
50
+ <object width="#{width}" height="#{height}" id="viddlerOuter-#{id}" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000">
51
+ <param name="movie" value="http://www.viddler.com/#{player_type}/#{id}/">
52
+ <param name="allowScriptAccess" value="always">
53
+ <param name="allowNetworking" value="all">
54
+ <param name="allowFullScreen" value="true">
55
+ <param name="wmode" value="#{wmode}" />
56
+ <param name="flashVars" value="#{flashvars}">
57
+ <object id="viddlerInner-#{id}">
58
+ <video id="viddlerVideo-#{id}" src="http://www.viddler.com/file/#{id}/html5mobile/" type="video/mp4" width="#{width}" height="#{height}" poster="http://www.viddler.com/thumbnail/#{id}/" controls="controls"></video>
59
+ </object>
60
+ </object>
61
+ <![endif]-->
62
+ <!--[if !IE]> <!-->
63
+ <object width="#{width}" height="#{height}" id="viddlerOuter-#{id}" type="application/x-shockwave-flash" data="http://www.viddler.com/player/#{id}/">
64
+ <param name="movie" value="http://www.viddler.com/#{player_type}/#{id}/">
65
+ <param name="allowScriptAccess" value="always">
66
+ <param name="allowNetworking" value="all">
67
+ <param name="allowFullScreen" value="true">
68
+ <param name="wmode" value="#{wmode}" />
69
+ <param name="flashVars" value="#{flashvars}">
70
+ <object id="viddlerInner-#{id}">
71
+ <video id="viddlerVideo-#{id}" src="http://www.viddler.com/file/#{id}/html5mobile/" type="video/mp4" width="#{width}" height="#{height}" poster="http://www.viddler.com/thumbnail/#{id}/" controls="controls"></video>
72
+ </object>
73
+ </object>
74
+ <!--<![endif]-->
75
+ CODE
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,79 @@
1
+ module Mira
2
+ class ViddlerClient
3
+
4
+ # there's a certain amount of stupidity in this code, because I built it
5
+ # against specs cribbed from the official Viddler gem, which supports a
6
+ # much broader range of functionality, so this code uses a slightly inflated
7
+ # set of assumptions.
8
+
9
+ # that being said, here's documentation mostly copied from viddler-ruby...
10
+
11
+ # Upload a video to the Viddler API.
12
+ #
13
+ # file - The File you are uploading
14
+ # arguments - The Hash of arguments for the video
15
+ # :title - The String title of the video
16
+ # :tags - The String of tags for the video
17
+ # :description - The String description of the video
18
+ # :make_public - The Boolean to make the video public on
19
+ # upload. Please note that if set to false, it
20
+ # will not make your video private.
21
+ #
22
+ # Examples
23
+ #
24
+ # viddler.upload File.open('myvideo.avi'), :title => "My Video",
25
+ # :tags => "viddler, ruby",
26
+ # :description => "This is cool"
27
+ #
28
+ # Returns a Hash containing the API response.
29
+
30
+ def upload(file, params = {})
31
+ json = self.get('viddler.videos.prepareUpload')
32
+ endpoint = json["upload"]["endpoint"]
33
+ token = json["upload"]["token"]
34
+ uploaded = RestClient.post(endpoint,
35
+ upload_params(file,
36
+ params.merge!(:token => token)))
37
+ JSON.parse uploaded
38
+ end
39
+
40
+ def get(api_method, params = {})
41
+ url = 'http://api.viddler.com/api/v2/' + api_method + '.json'
42
+ JSON.parse RestClient.get(url,
43
+ :params => params.merge!(:sessionid => auth,
44
+ :method => api_method,
45
+ :api_key => @api_key))
46
+ end
47
+
48
+ def auth
49
+ return @session_id if @session_id
50
+
51
+ session_request = RestClient.get('http://api.viddler.com/api/v2/viddler.users.auth.json',
52
+ :params => {:user => @username,
53
+ :password => @password,
54
+ :api_key => @api_key})
55
+ @session_id = JSON.parse(session_request)["auth"]["sessionid"]
56
+ end
57
+
58
+ def initialize(credentials)
59
+ @username = credentials[:username]
60
+ @password = credentials[:password]
61
+ @api_key = credentials[:api_key]
62
+ end
63
+
64
+ def upload_params(file, params = {})
65
+ raise "file argument must be a File" unless file.is_a? File
66
+ ordered_arguments = ActiveSupport::OrderedHash.new
67
+
68
+ params.each {|k,v| ordered_arguments[k] = v}
69
+
70
+ ordered_arguments[:api_key] = @api_key
71
+ ordered_arguments[:sessionid] = auth
72
+ ordered_arguments[:file] = file
73
+
74
+ ordered_arguments
75
+ end
76
+
77
+ end
78
+ end
79
+
@@ -0,0 +1,3 @@
1
+ module Mira
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,36 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "mira/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "mira"
7
+ s.version = Mira::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Giles Bowkett"]
10
+ s.email = ["gilesb@gmail.com"]
11
+ s.homepage = ""
12
+ s.summary = s.description = %q{Bare minimum Viddler API v2}
13
+
14
+ s.rubyforge_project = "mira"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ # specs
22
+ s.add_development_dependency 'rake', '0.8.7'
23
+ s.add_development_dependency 'rspec'
24
+
25
+ # I don't understand how people can tolerate IRB without my improvements to it. I
26
+ # get that this is arrogant, but it's also absolutely sincere.
27
+ s.add_development_dependency 'utility_belt'
28
+
29
+ # api methods
30
+ s.add_runtime_dependency 'json'
31
+ s.add_runtime_dependency 'rest-client'
32
+
33
+ # ordered hash
34
+ s.add_runtime_dependency 'activesupport'
35
+ end
36
+
@@ -0,0 +1,48 @@
1
+ Mira
2
+ ====
3
+
4
+ Mira is a minimal Viddler client. I named it Mira because I had just created another gem with a Spanish name (buscando_el_viento) and I figured I might as well keep the momentum going. "Mira" in Spanish means "look."
5
+
6
+ I created Mira because the official Viddler gem `viddler-ruby` didn't work out of the box, and fixing whatever was wrong with it seemed like a lot more work than just writing my own.
7
+
8
+ Mira supports only a tiny subset of the Viddler API's functionality - namely the ability to upload a video, and the ability to obtain an existing video's Flash video player embed code.
9
+
10
+ code copying
11
+ ------------
12
+
13
+ embed code copied from the original `viddler` gem (which was not actually written by Viddler); upload functionality and specs adapted from Viddler's official `viddler-ruby` gem.
14
+
15
+ test it out
16
+ -----------
17
+
18
+ create a `viddler.credentials` yaml file:
19
+
20
+ :api_key: 1234123412341234
21
+ :username: your_username
22
+ :password: your_password
23
+
24
+ use the `example.rb` script, or, to see every single step, pop into IRB and do this manually:
25
+
26
+ # setup
27
+ require 'yaml'
28
+ viddler = Mira::ViddlerClient.new(YAML.load(File.open("viddler.credentials")))
29
+
30
+ # optional, just if you want to see everything happen
31
+ viddler.auth
32
+ viddler.get('viddler.videos.prepareUpload')
33
+
34
+ # money shot
35
+ viddler.upload(File.new("sample.mov"),
36
+ :title => "sample",
37
+ :tags => "your mom",
38
+ :description => "whatever")
39
+
40
+ some or all of `:title`, `:tags`, and `:description` are required. API docs are not forthcoming on the subject and I couldn't care less. just throw them all in there and it'll work.
41
+
42
+ note that after uploading, Viddler will lag before displaying your video, and will not display any kind of "uploaded but still processing..." message. so you basically have to wait a minute with your dick in your hand.
43
+
44
+ TODO
45
+ ====
46
+
47
+ add specs about that whole requiring title, tags, and/or description shit.
48
+
Binary file
@@ -0,0 +1,36 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe Mira::Viddler do
4
+ it "generates embed code" do
5
+ embed_code = <<-CODE
6
+ <!--[if IE]>
7
+ <object width="437" height="370" id="viddlerOuter-e75e65b2" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000">
8
+ <param name="movie" value="http://www.viddler.com/player/e75e65b2/">
9
+ <param name="allowScriptAccess" value="always">
10
+ <param name="allowNetworking" value="all">
11
+ <param name="allowFullScreen" value="true">
12
+ <param name="wmode" value="transparent" />
13
+ <param name="flashVars" value="autoplay=f">
14
+ <object id="viddlerInner-e75e65b2">
15
+ <video id="viddlerVideo-e75e65b2" src="http://www.viddler.com/file/e75e65b2/html5mobile/" type="video/mp4" width="437" height="370" poster="http://www.viddler.com/thumbnail/e75e65b2/" controls="controls"></video>
16
+ </object>
17
+ </object>
18
+ <![endif]-->
19
+ <!--[if !IE]> <!-->
20
+ <object width="437" height="370" id="viddlerOuter-e75e65b2" type="application/x-shockwave-flash" data="http://www.viddler.com/player/e75e65b2/">
21
+ <param name="movie" value="http://www.viddler.com/player/e75e65b2/">
22
+ <param name="allowScriptAccess" value="always">
23
+ <param name="allowNetworking" value="all">
24
+ <param name="allowFullScreen" value="true">
25
+ <param name="wmode" value="transparent" />
26
+ <param name="flashVars" value="autoplay=f">
27
+ <object id="viddlerInner-e75e65b2">
28
+ <video id="viddlerVideo-e75e65b2" src="http://www.viddler.com/file/e75e65b2/html5mobile/" type="video/mp4" width="437" height="370" poster="http://www.viddler.com/thumbnail/e75e65b2/" controls="controls"></video>
29
+ </object>
30
+ </object>
31
+ <!--<![endif]-->
32
+ CODE
33
+ Mira::Viddler.embed_code(:id => "e75e65b2").should == embed_code
34
+ end
35
+ end
36
+
@@ -0,0 +1,3 @@
1
+ require File.expand_path("lib/mira")
2
+ %w{rubygems rest_client}.each {|lib| require lib}
3
+
@@ -0,0 +1,78 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ # attribution: these specs were very closely adapted from the official Viddler gem.
4
+ # however the implementation is a good deal looser.
5
+
6
+ describe Mira::ViddlerClient, "auth" do
7
+ # this example goes in a different describe block because the step it specs
8
+ # is stubbed out in the before(:each) for every other example
9
+ it "gets the session id from the Viddler API" do
10
+ @viddler_client = Mira::ViddlerClient.new(:username => "user",
11
+ :password => "s3kr3t",
12
+ :api_key => "12345")
13
+ json = '{"auth":{"sessionid":"15e3c113126b488654849545245434f52444b"}}'
14
+ RestClient.should_receive(:get).with(anything, anything).and_return(json)
15
+ @viddler_client.auth.should eq("15e3c113126b488654849545245434f52444b")
16
+ end
17
+ end
18
+
19
+ describe Mira::ViddlerClient, "upload" do
20
+ before(:each) do
21
+ @file = File.new(File.expand_path("mira.gemspec"))
22
+ @endpoint = "http://upload.viddler.com/upload.json"
23
+ @viddler_client = Mira::ViddlerClient.new(:username => "user",
24
+ :password => "s3kr3t",
25
+ :api_key => "12345")
26
+
27
+ RestClient.stub!(:post).and_return('{"response":["hello","howdy"]}')
28
+ @viddler_client.stub!(:get).and_return({"upload" => {"endpoint" => @endpoint}})
29
+ @viddler_client.should_receive(:auth).and_return('some_session')
30
+ end
31
+
32
+ it "calls get with viddler.videos.prepareUpload" do
33
+ @viddler_client.should_receive(:get).with('viddler.videos.prepareUpload')
34
+ @viddler_client.upload @file,
35
+ :param1 => 'asdf',
36
+ :param2 => true # FIXME: WTF
37
+ end
38
+
39
+ it "calls RestClient.post with endpoint, params, and file" do
40
+ RestClient.should_receive(:post).with(@endpoint,
41
+ hash_including(:param1 => 'asdf',
42
+ :param2 => true,
43
+ :file => @file))
44
+ @viddler_client.upload @file, :param1 => 'asdf', :param2 => true
45
+ end
46
+
47
+ it "includes sessionid" do
48
+ RestClient.should_receive(:post).with(anything, hash_including(:sessionid => 'some_session'))
49
+ @viddler_client.upload @file, :param1 => 'asdf', :param2 => true
50
+ end
51
+
52
+ it "includes API key" do
53
+ RestClient.should_receive(:post).with(anything, hash_including(:api_key => '12345'))
54
+ @viddler_client.upload @file, :param1 => 'asdf', :param2 => true
55
+ end
56
+
57
+ it "returns result of JSON.parse" do
58
+ JSON.stub!(:parse).and_return('asdfasdf')
59
+ @viddler_client.upload(@file, :param1 => 'asdf').should == 'asdfasdf'
60
+ end
61
+
62
+ it "submits the file argument to the API last" do
63
+ acceptable_params = ActiveSupport::OrderedHash.new
64
+ acceptable_params[:argle] = :bargle
65
+ acceptable_params[:api_key] = '12345'
66
+ acceptable_params[:sessionid] = 'some_session'
67
+ acceptable_params[:file] = @file
68
+
69
+ @viddler_client.upload_params(@file, :argle => :bargle).should == acceptable_params
70
+ end
71
+
72
+ it "requires an actual File" do
73
+ lambda {@viddler_client.upload_params(@file)}.should_not raise_error
74
+ lambda {@viddler_client.upload_params("file")}.should raise_error
75
+ end
76
+
77
+ end
78
+
metadata ADDED
@@ -0,0 +1,167 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mira
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Giles Bowkett
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-07-14 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ version_requirements: &id001 !ruby/object:Gem::Requirement
22
+ none: false
23
+ requirements:
24
+ - - "="
25
+ - !ruby/object:Gem::Version
26
+ hash: 49
27
+ segments:
28
+ - 0
29
+ - 8
30
+ - 7
31
+ version: 0.8.7
32
+ requirement: *id001
33
+ name: rake
34
+ prerelease: false
35
+ type: :development
36
+ - !ruby/object:Gem::Dependency
37
+ version_requirements: &id002 !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ hash: 3
43
+ segments:
44
+ - 0
45
+ version: "0"
46
+ requirement: *id002
47
+ name: rspec
48
+ prerelease: false
49
+ type: :development
50
+ - !ruby/object:Gem::Dependency
51
+ version_requirements: &id003 !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ hash: 3
57
+ segments:
58
+ - 0
59
+ version: "0"
60
+ requirement: *id003
61
+ name: utility_belt
62
+ prerelease: false
63
+ type: :development
64
+ - !ruby/object:Gem::Dependency
65
+ version_requirements: &id004 !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ hash: 3
71
+ segments:
72
+ - 0
73
+ version: "0"
74
+ requirement: *id004
75
+ name: json
76
+ prerelease: false
77
+ type: :runtime
78
+ - !ruby/object:Gem::Dependency
79
+ version_requirements: &id005 !ruby/object:Gem::Requirement
80
+ none: false
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ hash: 3
85
+ segments:
86
+ - 0
87
+ version: "0"
88
+ requirement: *id005
89
+ name: rest-client
90
+ prerelease: false
91
+ type: :runtime
92
+ - !ruby/object:Gem::Dependency
93
+ version_requirements: &id006 !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ hash: 3
99
+ segments:
100
+ - 0
101
+ version: "0"
102
+ requirement: *id006
103
+ name: activesupport
104
+ prerelease: false
105
+ type: :runtime
106
+ description: Bare minimum Viddler API v2
107
+ email:
108
+ - gilesb@gmail.com
109
+ executables: []
110
+
111
+ extensions: []
112
+
113
+ extra_rdoc_files: []
114
+
115
+ files:
116
+ - .gitignore
117
+ - .rvmrc
118
+ - Gemfile
119
+ - Rakefile
120
+ - example.rb
121
+ - lib/mira.rb
122
+ - lib/mira/embed_code.rb
123
+ - lib/mira/upload.rb
124
+ - lib/mira/version.rb
125
+ - mira.gemspec
126
+ - readme.md
127
+ - sample.mov
128
+ - spec/embed_code_spec.rb
129
+ - spec/spec_helper.rb
130
+ - spec/upload_spec.rb
131
+ homepage: ""
132
+ licenses: []
133
+
134
+ post_install_message:
135
+ rdoc_options: []
136
+
137
+ require_paths:
138
+ - lib
139
+ required_ruby_version: !ruby/object:Gem::Requirement
140
+ none: false
141
+ requirements:
142
+ - - ">="
143
+ - !ruby/object:Gem::Version
144
+ hash: 3
145
+ segments:
146
+ - 0
147
+ version: "0"
148
+ required_rubygems_version: !ruby/object:Gem::Requirement
149
+ none: false
150
+ requirements:
151
+ - - ">="
152
+ - !ruby/object:Gem::Version
153
+ hash: 3
154
+ segments:
155
+ - 0
156
+ version: "0"
157
+ requirements: []
158
+
159
+ rubyforge_project: mira
160
+ rubygems_version: 1.8.3
161
+ signing_key:
162
+ specification_version: 3
163
+ summary: Bare minimum Viddler API v2
164
+ test_files:
165
+ - spec/embed_code_spec.rb
166
+ - spec/spec_helper.rb
167
+ - spec/upload_spec.rb