ooyala_fb 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/README.md +11 -0
  2. data/Rakefile +2 -0
  3. data/lib/ooyala_fb.rb +131 -0
  4. metadata +66 -0
data/README.md ADDED
@@ -0,0 +1,11 @@
1
+ # Ooyala Fb
2
+
3
+ ## easilly enables meta tag creation on your ruby/rails/rack app for Ooyala Facebook sharing
4
+
5
+ this is the code found on: http://www.ooyala.com/support/docs/facebook_sharing_sdk
6
+ repackaged as a gem
7
+
8
+ - lib/ooyala_fb.rb contains the source code for the facebook sharing sdk.
9
+ - examples/simple.rb contains sample usage for this sdk.
10
+
11
+ You can find your Partner and Secret codes under the Developers area of the Backlot Account tab.
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler.setup :default
data/lib/ooyala_fb.rb ADDED
@@ -0,0 +1,131 @@
1
+
2
+ # The FacebookSharing kit is a set of methods which enable publishers to insert the meta tags
3
+ # required by all videos being uploaded to Facebook.
4
+ # The Develop Kit contains a primary class FacebookSharing, and one primary method: header().
5
+
6
+
7
+ require "base64"
8
+ require "cgi"
9
+ require "digest/sha2"
10
+ require "open-uri"
11
+ require 'net/http'
12
+ require 'rexml/document'
13
+ require 'logger'
14
+
15
+ # The logging level is set to INFO by default. Change it to Logger::ERROR if
16
+ # info messages about missing attribute values need not be logged in the file
17
+ # in the initialize method.
18
+
19
+ class OoyalaFb
20
+
21
+ QUERY_EXPIRES_IN = 1000
22
+
23
+ # Maximum width allowed for facebook videos. If the video being uploaded
24
+ # has a width greater than this, it will be reduced to make the width
25
+ # the same as FACEBOOK_MAX_WIDTH while maintaining the same aspect ratio
26
+ # between height and width.
27
+
28
+ FACEBOOK_MAX_WIDTH = 420
29
+
30
+ def initialize (partner_code, secret_code)
31
+ @partner_code = partner_code
32
+ @secret_code = secret_code
33
+
34
+ # Create a logger which ages logfile once it reaches
35
+ # a certain size. Leave 10 "old log files" and each file is about 1,024,000 bytes.
36
+ $LOG = Logger.new('ooyala_fb.log', 10, 1024000)
37
+
38
+ # Set the default logging level to INFO. So it will print messages
39
+ # about missing attribute values in the FacebookSharing.log file.
40
+ # Change this to Logger:ERROR if the messages should not be logged.
41
+ $LOG.sev_threshold = Logger::INFO
42
+
43
+ end
44
+
45
+ # Returns the string containing meta tags required by videos being uploaded to facebook
46
+ # for the given embed code, partner code and secret code video.
47
+
48
+ def header (embed_code)
49
+ # Get the url for the passed in partner_code, secret_code and embed_code
50
+ url = get_url('embedCode'=> embed_code)
51
+
52
+ # Get the xml data for the url.
53
+ xml_data = Net::HTTP.get_response(URI.parse(url)).body
54
+
55
+ # Parse the xml document to get the values for creating meta tags
56
+ doc = REXML::Document.new(xml_data)
57
+
58
+ # Fill the hash map with the key value pairs for the required meta tags
59
+ # by getting the values from the parsed xml
60
+ tags = ['title', 'description', 'thumbnail', 'height', 'width', 'embedCode']
61
+ metadata = {}
62
+ tags.map{|tag| metadata[tag] = get_node_value(doc, tag, embed_code)}
63
+
64
+ # Adjust video width to max allowed by Facebook, if required.
65
+ if metadata['width'] != nil
66
+ if Integer(metadata['width']) > FACEBOOK_MAX_WIDTH
67
+ metadata['height'] = get_adjusted_height(Integer(metadata['width']), Integer(metadata['height']))
68
+ metadata['width'] = FACEBOOK_MAX_WIDTH
69
+ end
70
+ end
71
+
72
+ # Construct the meta tags string by substituting the values from the metadata hashmap.
73
+ meta_tags = %Q{
74
+ <meta name="medium" content="video" />
75
+ <meta name="title" content="#{metadata['title']}" />
76
+ <meta name="description" content="#{metadata['description']}" />
77
+ <link rel="image_src" href="#{metadata['thumbnail']}" />
78
+ <link rel="video_src" href="http://player.ooyala.com/player.swf?\
79
+ embedCode=#{metadata['embedCode']}&keepEmbedCode=true" />
80
+ <meta name="video_height" content="#{metadata['height']}" />
81
+ <meta name="video_width" content="#{metadata['width']}" />
82
+ <meta name="video_type" content="application/x-shockwave-flash" />
83
+ }
84
+
85
+ # return the meta tags string with the values retrieved for the embedCode
86
+ return meta_tags
87
+ end
88
+
89
+ private
90
+
91
+ # Gets the value for the node passed in from the xml document passed in.
92
+ def get_node_value (doc, node_name, embed_code)
93
+
94
+ node_value = doc.get_elements("list/item/#{node_name}")[0].get_text().value rescue nil
95
+
96
+ if @node_value == nil
97
+ $LOG.info("Value not found for: #{node_name}, embed_code: #{embed_code}")
98
+ end
99
+
100
+ return node_value
101
+ end
102
+
103
+ def get_url (params)
104
+ # Gets the url for the partner_code, secret_code and embedCode
105
+ # passed in while creating an object of FacebookSharing class.
106
+ params["expires"] ||= (Time.now.to_i + QUERY_EXPIRES_IN).to_s
107
+
108
+ string_to_sign = @secret_code
109
+ url = "http://api.ooyala.com/partner/query?pcode=#{@partner_code}"
110
+
111
+ params.keys.sort.each do |key|
112
+ string_to_sign += "#{key}=#{params[key]}"
113
+ url += "&#{CGI.escape(key)}=#{CGI.escape(params[key])}"
114
+ end
115
+
116
+ digest = Digest::SHA256.digest(string_to_sign)
117
+ signature = Base64::encode64(digest).chomp.gsub(/=+$/, '')
118
+ url += "&signature=#{CGI.escape(signature)}"
119
+ return url
120
+ end
121
+
122
+ # returns the new height for any video which has width > FACEBOOK_MAX_WIDTH
123
+ # by mainting the same aspect ratio, considering that the width will be
124
+ # brought down to FACEBOOK_MAX_WIDTH
125
+ def get_adjusted_height (width, height)
126
+ return width == 0 ? 0 : (height.to_f * FACEBOOK_MAX_WIDTH.to_f / width.to_f).to_i
127
+ end
128
+
129
+ end
130
+
131
+
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ooyala_fb
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 2
8
+ - 1
9
+ version: 0.2.1
10
+ platform: ruby
11
+ authors:
12
+ - Ooyala! (modified by makevoid)
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-11-09 00:00:00 +01:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: "easilly enables meta tag creation on your ruby/rails/rack app for Ooyala Facebook sharing - more infos: http://www.ooyala.com/support/docs/facebook_sharing_sdk"
22
+ email: makevoid@gmail.com
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files: []
28
+
29
+ files:
30
+ - README.md
31
+ - Rakefile
32
+ - lib/ooyala_fb.rb
33
+ has_rdoc: true
34
+ homepage: http://www.makevoid.com
35
+ licenses: []
36
+
37
+ post_install_message:
38
+ rdoc_options: []
39
+
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ segments:
48
+ - 0
49
+ version: "0"
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ segments:
56
+ - 0
57
+ version: "0"
58
+ requirements: []
59
+
60
+ rubyforge_project:
61
+ rubygems_version: 1.3.7
62
+ signing_key:
63
+ specification_version: 3
64
+ summary: easilly enables meta tag creation on your ruby/rails/rack app for Ooyala Facebook sharing
65
+ test_files: []
66
+