mediaplug2-ruby 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.rdoc ADDED
@@ -0,0 +1,3 @@
1
+ == 2007-07-23
2
+
3
+ * Initial checkin, API is fully functional
data/MIT-LICENSE.rdoc ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 Intridea, Inc (http://www.intridea.com)
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.rdoc ADDED
@@ -0,0 +1,11 @@
1
+ = MediaPlug-Ruby
2
+
3
+ MediaPlug-Ruby is a Ruby wrapper for the MediaPlug (http://gomediaplug.com)
4
+ REST API. It provides a simple, chainable API syntax for the construction
5
+ of MediaPlug URLs.
6
+
7
+ == Example
8
+
9
+ mp = MediaPlug.new("mp.yourserver.com")
10
+ mp.image('http://some.url', :jpg).resize('>100x>100').crop('100x100').to_s
11
+ # => http://mp.yourserver.com/mp/get?mpsrc=http%3A%2F%2Fsome.url&mpaction=resize%20width%3D%3E100%20height%3D%3E100%20format%3Djpg%3Bcrop%20location%3Dcenter%20width%3D100%20height%3D100%20format%3Djpg
@@ -0,0 +1,24 @@
1
+ class MediaPlug
2
+ class InvalidAction < StandardError; end
3
+ attr_accessor :server
4
+
5
+ # Server is the domain of the MediaPlug server, e.g. "mp.yourdomain.com"
6
+ def initialize(server)
7
+ self.server = server
8
+ end
9
+
10
+ # Generates an image-formatted MediaPlugURL from the specified URL
11
+ def image(url, format='jpg')
12
+ MediaPlugURL.new(server,:image,url,format)
13
+ end
14
+
15
+ # Generates a video-formatted MediaPlugURL from the specified URL
16
+ def video(url, format='flv')
17
+ MediaPlugURL.new(server,:video,url,format)
18
+ end
19
+
20
+ # Generates an audio-formatted MediaPlugURL from the specified URL
21
+ def audio(url, format='mp3')
22
+ MediaPlugURL.new(server,:audio,url,format)
23
+ end
24
+ end
@@ -0,0 +1,372 @@
1
+ # The MediaPlugURL is a class that allows you to chain
2
+ # together MediaPlug actions and then get the resulting URL
3
+ # by using its to_s method. It should be instantiated through
4
+ # the MediaPlug class, not by itself:
5
+ class MediaPlugURL
6
+ def initialize(server, media_type, url, format)
7
+ @server = server
8
+ @media_type = media_type
9
+ @url = url
10
+ @format = format
11
+ @actions = []
12
+ end
13
+
14
+ # <b>Image Only:</b> Resize an image to the dimensions specified. The
15
+ # dimensions may be specified as a string, a Hash, or an Array.
16
+ #
17
+ # Example:
18
+ #
19
+ # image = mp.image('http://some.url/', :png)
20
+ # image.resize('100x100')
21
+ # image.resize(:width => 100, :height => 100)
22
+ # image.resize([100,100])
23
+ # # resize width to 128, height proportionally
24
+ # image.resize(:width => 128)
25
+ #
26
+ # # halve the image size
27
+ # image.resize(["50%","50%"])
28
+ def resize(string_or_hash_or_array)
29
+ argument = string_or_hash_or_array
30
+ raise MediaPlug::InvalidAction, "Only images may be resized." unless image?
31
+ action = "resize "
32
+
33
+ case argument
34
+ when String, Array
35
+ argument = argument.split("x") if argument.is_a?(String)
36
+ action << "width=#{argument[0]} height=#{argument[1]}"
37
+ when Hash
38
+ argument.keys.each{ |k| argument[k.to_s] = argument[k] }
39
+ action << "#{"width=#{argument["width"]}" if argument["width"]}#{" height=#{argument["height"]}" if argument["height"]}"
40
+ end
41
+
42
+ action << " format=#{@format}"
43
+
44
+ @actions << action
45
+ self
46
+ end
47
+
48
+ # <b>Image Only:</b> Crop an image based on the input provided. Since
49
+ # MediaPlug can crop according to gravity setting, default is center.
50
+ # the arguments you
51
+ # provide will determine which kind of cropping is employed.
52
+ #
53
+ # Cropping:
54
+ #
55
+ # image = mp.image('http://some.url/', :png)
56
+ # image.crop('100x100')
57
+ # image.crop(:width => 100, :height => 100)
58
+ # image.crop([100,100])
59
+
60
+ # Options:
61
+ #
62
+ # - <tt>:gravity</tt>: The position of crop. May be any of NorthWest, North, NorthEast, West, Center, East, SouthWest, South, SouthEast. Default is center.
63
+ # - <tt>:x-offset</tt>: x coordinate offset of the top-left corner related to center position set by the gravity parameter.
64
+ # - <tt>:y-offset</tt>: y coordinate offset of the top-left corner related to center position set by the gravity parameter.
65
+ #
66
+ #
67
+ def crop(string_or_hash_or_array)
68
+ argument = string_or_hash_or_array
69
+ raise MediaPlug::InvalidAction, "Only images may be cropped." unless image?
70
+ action = "crop "
71
+
72
+ case argument
73
+ when String, Array
74
+ argument = argument.split("x") if argument.is_a?(String)
75
+ action << "gravity=center width=#{argument[0]} height=#{argument[1]}"
76
+ when Hash
77
+ argument.keys.each{|k| argument[k.to_s] = argument.delete(k)}
78
+ action << "gravity=#{argument['gravity']} " if argument['gravity']
79
+ action << "x-offset=#{argument['x-offset']} " if argument['x-offset']
80
+ action << "y-offset=#{argument['y-offset']} " if argument['y-offset']
81
+ if argument["width"] && argument["height"]
82
+ action << " width=#{argument["width"]} height=#{argument["height"]}"
83
+ else
84
+ raise MediaPlug::InvalidAction, "Could not understand the options given to the crop command."
85
+ end
86
+ end
87
+
88
+ action << " format=#{@format}"
89
+ @actions << action
90
+ self
91
+ end
92
+
93
+ # <b>Image Only:</b> Rotate the image by the given number of degrees.
94
+ #
95
+ # Example:
96
+ #
97
+ # image = mp.image('http://some.url/')
98
+ # image.rotate(90)
99
+ def rotate(degree)
100
+ raise MediaPlug::InvalidAction, "Only images may be rotated." unless image?
101
+ argument = degree
102
+ action = "rotate "
103
+
104
+ case argument
105
+ when String, Integer
106
+ action << "degree=#{argument}"
107
+ when Hash
108
+ action << action_hash_to_string(argument)
109
+ end
110
+
111
+ action << " format=#{@format}"
112
+ @actions << action
113
+ self
114
+ end
115
+
116
+ FLIP_MAP = {"vertical" => "v",
117
+ "horizontal" => "h",
118
+ "both" => "b"}
119
+
120
+ # <b>Image Only:</b> Flip the image in the given direction.
121
+ #
122
+ # Valid Directions:
123
+ #
124
+ # - +:horizontal+ or +:h+
125
+ # - +:vertical+ or +:v+
126
+ # - +:both+ or +:b+
127
+ #
128
+ # Example:
129
+ #
130
+ # image = mp.image('http://some.url/')
131
+ # image.flip(:both)
132
+ def flip(direction)
133
+ raise MediaPlug::InvalidAction, "Only images may be flipped." unless image?
134
+ argument = direction
135
+ argument = FLIP_MAP[argument.to_s] if FLIP_MAP.key?(argument.to_s)
136
+ "flip #{argument} format=#{@format}"
137
+ end
138
+ # <b>Image Only:</b> Create SWIRLING effect on the image by the given number of amplitude and wavelength.
139
+ #
140
+ # Example:
141
+ #
142
+ # image = mp.image('http://some.url/')
143
+ # image.swirl(180) #create a swirling effect with degree = 180
144
+ def swirl(degree)
145
+ raise MediaPlug::InvalidAction, "Only images may be swirled." unless image?
146
+ action = "swirl "
147
+ action << "degree=#{degree}"
148
+
149
+ action << " format=#{@format}"
150
+ @actions << action
151
+ self
152
+ end
153
+ # <b>Image Only:</b> Create Polaroid Frame for the image.
154
+ #
155
+ # Options:
156
+ #
157
+ # - <tt>:angle</tt>: The tilting angle of the Polaroid Frame .
158
+ # - <tt>:caption</tt>: Text annontation that to be added at the bottom of the Polaroid Frame.
159
+ # Example:
160
+ #
161
+ # image = mp.image('http://some.url/')
162
+ # image.polaroid(:angle => '15', :caption => "Group Picture 3/15/2011")
163
+ def polaroid(options={})
164
+ raise MediaPlug::InvalidAction, "Only images may be swirled." unless image?
165
+ action = "polaroid "
166
+
167
+ action << "angle=#{options[:angle]} " if options[:angle]
168
+ action << "caption=#{options[:caption]} " if options[:caption]
169
+
170
+ action << " format=#{@format}"
171
+ @actions << action
172
+ self
173
+ end
174
+ # <b>Image Only:</b> Create Water Reflection Effect for the image.
175
+
176
+ # Example:
177
+ #
178
+ # image = mp.image('http://some.url/')
179
+ # image.water_reflection()
180
+ def water_reflection
181
+ raise MediaPlug::InvalidAction, "Only images may be swirled." unless image?
182
+ action = "water_reflection "
183
+
184
+ action << " format=#{@format}"
185
+ @actions << action
186
+ self
187
+ end
188
+ # <b>Image Only:</b> Apply Art-Like Filter to the image.
189
+ # Valid Filter Type:
190
+ # Vignette, OilPainting, Charcoal, Sketch, Emboss
191
+ # Options:
192
+ #
193
+ # - <tt>:level</tt>: The filter mask size, maybe low, medium, high.
194
+ # Example:
195
+ #
196
+ # image = mp.image('http://some.url/')
197
+ # image.filter('vignette')
198
+ def filter(filter_type, options={})
199
+ raise MediaPlug::InvalidAction, "Only images may be swirled." unless image?
200
+ action = "filter "
201
+ action << "filter_type=#{filter_type} "
202
+ action << "level=#{options[:level]} " if options[:level]
203
+ action << " format=#{@format}"
204
+ @actions << action
205
+ self
206
+ end
207
+ # <b>Image Only:</b> Create WAVE effect on the image by the given number of amplitude and wavelength.
208
+ #
209
+ # Example:
210
+ #
211
+ # image = mp.image('http://some.url/')
212
+ # image.wave(50,200) #create a wave effect with amplitude = 50 and wavelength = 200
213
+ def wave(amp, wl)
214
+ raise MediaPlug::InvalidAction, "Only images may be waved." unless image?
215
+ action = "wave "
216
+ action << "amplitude=#{amp} wavelength=#{wl}"
217
+
218
+ action << " format=#{@format}"
219
+ @actions << action
220
+ self
221
+ end
222
+ # <b>Image Only:</b> Create IMPLODE or EXPLODE effect on the image by the given number of factor.
223
+ #
224
+ # Valid impode factor: [-1..1]
225
+ # nagtive value: expolode effect, positive value: implode effect
226
+ # Example:
227
+ #
228
+ # image = mp.image('http://some.url/')
229
+ # image.implode(1)
230
+ def implode(factor)
231
+ raise MediaPlug::InvalidAction, "Only images may be imploded." unless image?
232
+ action = "implode "
233
+ action << "factor=#{factor}"
234
+
235
+ action << " format=#{@format}"
236
+ @actions << action
237
+ self
238
+ end
239
+ # <b>Image Only:</b> Watermark an image with text or with another image.
240
+ # The +watermark_type+ may be +:image+ or +:text+.
241
+ #
242
+ # Options:
243
+ #
244
+ # - <tt>:gravity</tt>: The position of the watermark. May be any of NorthWest, North, NorthEast, West, Center, East, SouthWest, South, SouthEast. Default is center.
245
+ # - <tt>:alpha</tt>: value from 0.0 to 1.0 indicating the opacity of the watermark. Default is 0.6.
246
+ #
247
+ # Text-Only Options
248
+ #
249
+ # - <tt>:weight</tt>: the weight of the text font, maybe All,Bold,Bolder,Lighter,Normal. Default is Normal.
250
+ # - <tt>:style</tt>: The style of the text font, maybe Any, Italic, Normal, Oblique
251
+ # - <tt>:font_size</tt>: Integer, pixel size of the font to use on the watermark.
252
+ def watermark(watermark_type, text_or_url, options = {})
253
+ raise MediaPlug::InvalidAction, "Only images may be watermarked." unless image?
254
+ action = "watermark "
255
+
256
+ case watermark_type
257
+ when :image
258
+ action << "url=#{text_or_url} "
259
+ when :text
260
+ action << "text=#{text_or_url.gsub(' ',"\\ ")} "
261
+ action << "style=#{options[:style]} " if options[:style]
262
+ action << "weight=#{options[:weight]} " if options[:weight]
263
+ action << "text-font-size=#{options[:font_size]} " if options[:font_size]
264
+ else
265
+ raise MediaPlug::InvalidAction, "The watermark_type must be :image or :text"
266
+ end
267
+
268
+ action << "gravity=#{options[:gravity]} " if options[:gravity]
269
+ action << "alpha=#{options[:alpha]} " if options[:alpha]
270
+ action << "format=#{@format}"
271
+
272
+ @actions << action
273
+ self
274
+ end
275
+
276
+ # Converts the source into the specified format, with the options provided
277
+ # (options vary based on media type).
278
+ #
279
+ # Allowable Formats:
280
+ #
281
+ # - <b>Video:</b> flv, ipod, psp, 3gpp
282
+ # - <b>Audio:</b> mp3
283
+ # - <b>Image:</b> jpg, png, gif
284
+ #
285
+ # Video Conversion Options:
286
+ #
287
+ # - <tt>:bitrate</tt>: The bitrate in which to encode (e.g. "200k")
288
+ # - <tt>:width</tt>: The width of encoded video
289
+ # - <tt>:height</tt>: The height of encoded video
290
+ #
291
+ # Audio Conversion Options:
292
+ #
293
+ # - <tt>:bitrate</tt>: The bitrate in which to encode (e.g. "64k")
294
+ # - <tt>:samplerate</tt>: The sample rate with which to encode (e.g. 128)
295
+ def convert(new_format, options = {})
296
+ if image?
297
+ action = "rotate 0 format=#{new_format}"
298
+ elsif audio?
299
+ action = "audio_convert format=#{new_format}"
300
+ action << " bitrate=#{options[:bitrate]}" if options[:bitrate]
301
+ action << " samplerate=#{options[:samplerate]}" if options[:samplerate]
302
+ elsif video?
303
+ action = "video_convert format=#{new_format}"
304
+ action << " bitrate=#{options[:bitrate]}" if options[:bitrate]
305
+ action << " width=#{options[:width]}" if options[:width]
306
+ action << " height=#{options[:height]}" if options[:height]
307
+ end
308
+ @actions << action
309
+ self
310
+ end
311
+
312
+ # <b>Video Only:</b> Generates a thumbnail of a video at the specified frame.
313
+ # Video Thumbnail Options:
314
+ #
315
+ # - <tt>:width</tt>: The width of thumbnail image
316
+ # - <tt>:height</tt>: The height of thumbnail image
317
+ def thumbnail(seek, options={})
318
+ raise MediaPlug::InvalidAction, "Only videos may be thumbnailed." unless video?
319
+ action = "thumbnail seek=#{seek}"
320
+ action << " width=#{options[:width]}" if options[:width]
321
+ action << " height=#{options[:height]}" if options[:height]
322
+ @media_type = :image
323
+ @actions << action
324
+ self
325
+ end
326
+
327
+ # <b>Video Only:</b> Generates an animated GIF image for video preview.
328
+ # Video Preview Options:
329
+ #
330
+ # Framestep is duration in seconds of per frame capture
331
+ # - <tt>:width</tt>: The width of thumbnail image
332
+ # - <tt>:height</tt>: The height of thumbnail image
333
+ def preview(framestep, options={})
334
+ raise MediaPlug::InvalidAction, "Only videos may be previewed." unless video?
335
+ action = "preview framestep=#{framestep}"
336
+ action << " width=#{options[:width]}" if options[:width]
337
+ action << " height=#{options[:height]}" if options[:height]
338
+ @media_type = :image
339
+ @actions << action
340
+ self
341
+ end
342
+ # Causes the resultant URL to be recached. Should not be called
343
+ # each time.
344
+ def recache!
345
+ @actions.unshift("recache")
346
+ self
347
+ end
348
+
349
+ # Generate a URL to retrieve the metadata about a file instead of the
350
+ # file itself.
351
+ def metadata!
352
+ @metadata = true
353
+ self
354
+ end
355
+
356
+ # Outputs the final URL taking into account all of the actions that
357
+ # have been chained onto this MediaPlugURL
358
+ def to_s
359
+ url = "http://#{@server}/mp/get?mpsrc=#{URI.escape(@url, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))}#{"&mpaction=#{URI.escape(@actions.join(","), Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))}" if @actions.any?}"
360
+ if @metadata
361
+ "http://#{@server}/mp/metadata?mpsrc=#{url}"
362
+ else
363
+ url
364
+ end
365
+ end
366
+
367
+ protected
368
+
369
+ def image?; @media_type == :image end
370
+ def video?; @media_type == :video end
371
+ def audio?; @media_type == :audio end
372
+ end
@@ -0,0 +1,2 @@
1
+ require 'mediaplug/mediaplug_url'
2
+ require 'mediaplug/mediaplug'
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mediaplug2-ruby
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 2
10
+ version: 0.0.2
11
+ platform: ruby
12
+ authors:
13
+ - Michael Bleigh
14
+ - Ping Yu
15
+ autorequire:
16
+ bindir: bin
17
+ cert_chain: []
18
+
19
+ date: 2011-03-23 00:00:00 -05:00
20
+ default_executable:
21
+ dependencies: []
22
+
23
+ description: A wrapper for the MediaPlug 2 API to give a simple syntax for MediaPlug URL generation.
24
+ email: ping@intridea.com
25
+ executables: []
26
+
27
+ extensions: []
28
+
29
+ extra_rdoc_files:
30
+ - CHANGELOG.rdoc
31
+ - MIT-LICENSE.rdoc
32
+ - README.rdoc
33
+ files:
34
+ - CHANGELOG.rdoc
35
+ - MIT-LICENSE.rdoc
36
+ - README.rdoc
37
+ - lib/mediaplug/mediaplug.rb
38
+ - lib/mediaplug/mediaplug_url.rb
39
+ - lib/mediaplug-ruby.rb
40
+ has_rdoc: true
41
+ homepage: http://github.com/intridea/mediaplug2-ruby
42
+ licenses: []
43
+
44
+ post_install_message:
45
+ rdoc_options:
46
+ - --main
47
+ - README.rdoc
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ hash: 3
56
+ segments:
57
+ - 0
58
+ version: "0"
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ hash: 3
65
+ segments:
66
+ - 0
67
+ version: "0"
68
+ requirements: []
69
+
70
+ rubyforge_project:
71
+ rubygems_version: 1.3.7
72
+ signing_key:
73
+ specification_version: 3
74
+ summary: A wrapper for the MediaPlug 2 API to give a simple syntax for MediaPlug URL generation.
75
+ test_files: []
76
+