flickrcaptionr 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -15,3 +15,4 @@ spec/reports
15
15
  test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
+ spec/flickrcaptionr.yml
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --format html
3
+ --out spec/reports/index.html
@@ -0,0 +1,14 @@
1
+ # Changelog
2
+
3
+ ## 1.1.0 - 1st July 2012
4
+
5
+ ### Bugfixes
6
+
7
+ * Flickr fetcher was greedily picking up on some non-flickr URLs
8
+ * Spaces in image names no longer result in abject failure
9
+ * Quotes are now properly escaped in text (single and double)
10
+ * OEmbed API fetcher wouldn't work for some response types
11
+
12
+ ### Features
13
+
14
+ * Tests - RSpec suite to cover the basics added (fetcher, processor)
data/README.md CHANGED
@@ -8,7 +8,7 @@ You will need your own API key for flickr if you want to get images from there.
8
8
 
9
9
  ## Dependencies
10
10
 
11
- flickrcaptionr has no non-gem dependencies other than ImageMagick's convert tool, Ruby 1.9 or greater (1.9.3 recommended) and the standard Ruby net/http library.
11
+ flickrcaptionr has no non-gem dependencies other than ImageMagick's convert tool, Ruby 1.9.2 or greater (1.9.3 recommended) and the standard Ruby net/http library.
12
12
 
13
13
  On Ubuntu systems, ImageMagick can be installed with:
14
14
 
@@ -20,6 +20,8 @@ On Mac systems, ImageMagick can be installed with:
20
20
 
21
21
  You can check everything is working by running `convert -version`. If you see something like `Version: ImageMagick 6.6.0-4 2012-04-30 Q16 http://www.imagemagick.org`, everything is probably just fine. Greater version numbers are untested, but anything newer than 6.4.x the line *should* work fine.
22
22
 
23
+ Gem dependencies are pulled in by installation of the gem or by inclusion with bundler. If checking out the source to develop, `bundle install` in the source directory will set you up.
24
+
23
25
 
24
26
  ## Installation
25
27
 
@@ -45,7 +47,7 @@ To provide a web service, spin up the webapp with:
45
47
 
46
48
  $ flickrcaptionr-web [options]
47
49
 
48
- Point your browser at the URL given in the output, typically `http://localhost:4567`. You'll probably want to read the help on these.
50
+ Point your browser at the URL given in the output, typically `http://localhost:4567`. You'll probably want to read the help on these commands.
49
51
 
50
52
  Configuration for flickrcaptionr is stored in a Yaml file, by default at ~/.flickrcaptionr.yml. You can change this if you like. The CLI or web tools can create this file for you, and set options in it. For instance, to set your Flickr API key:
51
53
 
@@ -58,6 +60,54 @@ A more comprehensive example of the CLI follows:
58
60
  $ flickrcaptionr-cli --caption-font-size=48 --caption "BLUE. BLUE EVERYWHERE." \
59
61
  --resize 400x400 "http://assets.talkunafraid.co.uk/img/IMG_0311.jpg"
60
62
 
63
+ ### HTTP API
64
+
65
+ There is a web-based user interface to allow end users to easily generate images, located at the root of the web server URL.
66
+
67
+ You can make simple GET-based requests for resizing and the like to the web server. Here are some examples:
68
+
69
+ # No resizing
70
+ /get/flickr-id-or-encoded-url
71
+ # No resizing, add caption
72
+ /get/flickr-id-or-encoded-url/caption
73
+ # Resize to 400x300, no caption
74
+ /get/flickr-id-or-encoded-url/400/300
75
+ # Resize to 400x300, caption
76
+ /get/flickr-id-or-encoded-url/400/300/caption
77
+
78
+ You can append the `?redirect=true` option if you'd like to be redirected to a direct image path instead of being served the image directly from that URL.
79
+
80
+ ### Ruby API
81
+
82
+ Building applications around Flickrcaptionr is fairly straightforward. You'll need to initialize the config class, fetcher, and optionally the processor:
83
+
84
+ c = Flickrcaptionr::Config.new(config_path, {:output_path => 'optionally override', :flickr_api_key=>'optionally override'})
85
+ # Note the options hash to the config class is optional.
86
+ f = Flickrcaptionr::Fetcher.new
87
+ # And only if you need to resize or add text:
88
+ p = Flickrcaptionr::Processor.new
89
+
90
+ Once you've got the framework set up, you can fetch images:
91
+
92
+ output_path = f.fetch(some_url)
93
+
94
+ Resize images:
95
+
96
+ output_path = p.resize!(some_path, width, height)
97
+
98
+ And overlay text:
99
+
100
+ output_path = p.add_text!(some_path, text, opts)
101
+
102
+ When adding text, opts is an optional hash that lets you set `:font_size`, `:font_stroke`, and `:font_path` to override font behaviour.
103
+
104
+ ## Tests
105
+
106
+ A test suite is included (RSpec based). You must configure the test suite with a Flickr API key in the spec/flickrcaptionr.yml file. An example file is provided in that folder.
107
+
108
+ To run all tests:
109
+
110
+ rake spec
61
111
 
62
112
  ## Contributing
63
113
 
@@ -67,6 +117,13 @@ A more comprehensive example of the CLI follows:
67
117
  4. Push to the branch (`git push origin my-new-feature`)
68
118
  5. Create new Pull Request
69
119
 
70
- ## License
120
+ ## License and Credits
121
+
122
+ Copyright (c) 2012, James Harrison. All rights reserved.
123
+
124
+ See the LICENSE file. Long story short, the code is MIT, go crazy.
125
+
126
+ The included Coda-Heavy font is licensed under the SIL Open Font License. See the fonts/LICENSE file for details.
127
+
128
+ The Twitter Bootstrap framework is licensed under the Apache License 2.0.
71
129
 
72
- See the LICENSE file. Long story short, MIT, go crazy.
data/Rakefile CHANGED
@@ -1,2 +1,8 @@
1
1
  #!/usr/bin/env rake
2
2
  require "bundler/gem_tasks"
3
+ require 'rspec/core/rake_task'
4
+
5
+ RSpec::Core::RakeTask.new('spec')
6
+
7
+ # If you want to make this the default task
8
+ task :default => :spec
@@ -21,4 +21,5 @@ Gem::Specification.new do |gem|
21
21
  gem.add_dependency("sinatra", ">= 1.3.2")
22
22
  gem.add_dependency("haml", ">= 3.1.6")
23
23
  gem.add_dependency("thin", ">= 1.3.1")
24
+ gem.add_development_dependency("rspec", ">= 2.10.0")
24
25
  end
@@ -0,0 +1,94 @@
1
+ Copyright (c) 2010, Vernon Adams (vern@newtypography.co.uk),
2
+ with Reserved Font Name Coda.
3
+
4
+ This Font Software is licensed under the SIL Open Font License, Version 1.1.
5
+ This license is copied below, and is also available with a FAQ at:
6
+ http://scripts.sil.org/OFL
7
+
8
+
9
+ -----------------------------------------------------------
10
+ SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
11
+ -----------------------------------------------------------
12
+
13
+ PREAMBLE
14
+ The goals of the Open Font License (OFL) are to stimulate worldwide
15
+ development of collaborative font projects, to support the font creation
16
+ efforts of academic and linguistic communities, and to provide a free and
17
+ open framework in which fonts may be shared and improved in partnership
18
+ with others.
19
+
20
+ The OFL allows the licensed fonts to be used, studied, modified and
21
+ redistributed freely as long as they are not sold by themselves. The
22
+ fonts, including any derivative works, can be bundled, embedded,
23
+ redistributed and/or sold with any software provided that any reserved
24
+ names are not used by derivative works. The fonts and derivatives,
25
+ however, cannot be released under any other type of license. The
26
+ requirement for fonts to remain under this license does not apply
27
+ to any document created using the fonts or their derivatives.
28
+
29
+ DEFINITIONS
30
+ "Font Software" refers to the set of files released by the Copyright
31
+ Holder(s) under this license and clearly marked as such. This may
32
+ include source files, build scripts and documentation.
33
+
34
+ "Reserved Font Name" refers to any names specified as such after the
35
+ copyright statement(s).
36
+
37
+ "Original Version" refers to the collection of Font Software components as
38
+ distributed by the Copyright Holder(s).
39
+
40
+ "Modified Version" refers to any derivative made by adding to, deleting,
41
+ or substituting -- in part or in whole -- any of the components of the
42
+ Original Version, by changing formats or by porting the Font Software to a
43
+ new environment.
44
+
45
+ "Author" refers to any designer, engineer, programmer, technical
46
+ writer or other person who contributed to the Font Software.
47
+
48
+ PERMISSION & CONDITIONS
49
+ Permission is hereby granted, free of charge, to any person obtaining
50
+ a copy of the Font Software, to use, study, copy, merge, embed, modify,
51
+ redistribute, and sell modified and unmodified copies of the Font
52
+ Software, subject to the following conditions:
53
+
54
+ 1) Neither the Font Software nor any of its individual components,
55
+ in Original or Modified Versions, may be sold by itself.
56
+
57
+ 2) Original or Modified Versions of the Font Software may be bundled,
58
+ redistributed and/or sold with any software, provided that each copy
59
+ contains the above copyright notice and this license. These can be
60
+ included either as stand-alone text files, human-readable headers or
61
+ in the appropriate machine-readable metadata fields within text or
62
+ binary files as long as those fields can be easily viewed by the user.
63
+
64
+ 3) No Modified Version of the Font Software may use the Reserved Font
65
+ Name(s) unless explicit written permission is granted by the corresponding
66
+ Copyright Holder. This restriction only applies to the primary font name as
67
+ presented to the users.
68
+
69
+ 4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
70
+ Software shall not be used to promote, endorse or advertise any
71
+ Modified Version, except to acknowledge the contribution(s) of the
72
+ Copyright Holder(s) and the Author(s) or with their explicit written
73
+ permission.
74
+
75
+ 5) The Font Software, modified or unmodified, in part or in whole,
76
+ must be distributed entirely under this license, and must not be
77
+ distributed under any other license. The requirement for fonts to
78
+ remain under this license does not apply to any document created
79
+ using the Font Software.
80
+
81
+ TERMINATION
82
+ This license becomes null and void if any of the above conditions are
83
+ not met.
84
+
85
+ DISCLAIMER
86
+ THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
87
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
88
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
89
+ OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
90
+ COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
91
+ INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
92
+ DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
93
+ FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
94
+ OTHER DEALINGS IN THE FONT SOFTWARE.
@@ -1,2 +1,5 @@
1
1
  class Flickrcaptionr::RequestNotFetchableException < ArgumentError; end
2
- class Flickrcaptionr::FetcherNotConfiguredException < ArgumentError; end
2
+ class Flickrcaptionr::FetcherNotConfiguredException < ArgumentError; end
3
+ class Flickrcaptionr::ResizeFailedException < ArgumentError; end
4
+ class Flickrcaptionr::TextGenerationFailedException < ArgumentError; end
5
+ class Flickrcaptionr::CompositionFailedException < ArgumentError; end
@@ -5,7 +5,7 @@ require 'json'
5
5
  class Flickrcaptionr::Fetchers::Flickr < Flickrcaptionr::Fetchers::Base
6
6
  def initialize
7
7
  @@api_key = Flickrcaptionr::Config.flickr_api_key
8
- @@flickr_regexes = [/(?!www\.flickr.com|secure\.flickr.com|flickr.com).+\/(\d+)\/.*/,/^(\d+)$/]
8
+ @@flickr_regexes = [/(?:www\.flickr.com|secure\.flickr.com|flickr.com)+\/.+\/(\d+)\/.*$/,/^(\d+)$/]
9
9
  end
10
10
  def fetch(url)
11
11
  unless @@api_key
@@ -9,11 +9,8 @@ class Flickrcaptionr::Fetchers::OEmbed < Flickrcaptionr::Fetchers::Base
9
9
  resp = OEmbed::Providers.get(url)
10
10
  # Now figure out what the heck in our oembed response is actually the image we're seeking
11
11
  if resp
12
- for key in %w(url thumbnail_url)
13
- if resp[key] and resp[key] != nil
14
- return resp[key]
15
- end
16
- end
12
+ return resp.url if (resp.url rescue nil)
13
+ return resp.thumbnail_url if (resp.thumbnail_url rescue nil)
17
14
  end
18
15
  # If we're here, we either didn't retrieve any suitable image URL, or our provider broke
19
16
  raise Flickrcaptionr::RequestNotFetchableException, "Could not retrieve #{url}"
@@ -13,7 +13,10 @@ class Flickrcaptionr::Processor
13
13
  puts "Not resizing, #{out_filename} already exists"
14
14
  else
15
15
  puts "Resizing #{path} to #{width.to_i.to_s}x#{height.to_i.to_s} at #{out_filename}"
16
- res = `convert #{path} -resize #{width.to_i.to_s}x#{height.to_i.to_s}^ -gravity center -extent #{width.to_i.to_s}x#{height.to_i.to_s} #{out_filename}`
16
+ res = `convert '#{path}' -resize #{width.to_i.to_s}x#{height.to_i.to_s}^ -gravity center -extent #{width.to_i.to_s}x#{height.to_i.to_s} '#{out_filename}'`
17
+ if !File.exists?(out_filename)
18
+ raise Flickrcaptionr::ResizeFailedException, "Failed to write output file, check your ImageMagick installation and output path setting"
19
+ end
17
20
  end
18
21
  return out_filename
19
22
  end
@@ -27,10 +30,18 @@ class Flickrcaptionr::Processor
27
30
  if File.exists?(out_filename)
28
31
  puts "Already added text to this image, not doing it again"
29
32
  else
30
- puts "Adding text '#{text}' to #{path}"
31
- `convert -background none -fill white -font "#{opts[:font_path] ? opts[:font_path] : (File.join(File.dirname(__FILE__), '..', '..', 'fonts', 'Coda-Heavy.ttf' ))}" -stroke black -strokewidth #{opts[:font_stroke] ? opts[:font_stroke].to_s : 2.to_s} -pointsize #{opts[:font_size] ? opts[:font_size].to_s : 36.to_s} -size #{((Dimensions.width(path)-10).to_s)} -gravity Center caption:'#{text.gsub(/[^A-Za-z0-9 \-"\.,\?\!]/,"")}' caption-tmp.png`
32
- `composite caption-tmp.png #{path} -compose atop -gravity South #{out_filename}`
33
- `rm -rf caption-tmp.png`
33
+ escaped_text = text.gsub('"',"''").gsub(/[^A-Za-z0-9 '\-\.,\?\!]/,"")
34
+ puts "Adding text '#{escaped_text}' to #{path} (original text '#{text}')"
35
+
36
+ `convert -background none -fill white -font "#{opts[:font_path] ? opts[:font_path] : (File.join(File.dirname(__FILE__), '..', '..', 'fonts', 'Coda-Heavy.ttf' ))}" -stroke black -strokewidth #{opts[:font_stroke] ? opts[:font_stroke].to_s : 2.to_s} -pointsize #{opts[:font_size] ? opts[:font_size].to_s : 36.to_s} -size #{((Dimensions.width(path)-10).to_s)} -gravity Center caption:"#{escaped_text}" /tmp/caption-tmp.png`
37
+ if !File.exists?('/tmp/caption-tmp.png')
38
+ raise Flickrcaptionr::TextGenerationFailedException, "Couldn't generate text to overlay! Check your ImageMagick installation and that /tmp is writeable."
39
+ end
40
+ `composite /tmp/caption-tmp.png '#{path}' -compose atop -gravity South '#{out_filename}'`
41
+ if !File.exists?(out_filename)
42
+ raise Flickrcaptionr::CompositionFailedException, "Failed to write output composite file, check your ImageMagick installation and output path setting"
43
+ end
44
+ `rm -rf /tmp/caption-tmp.png`
34
45
  end
35
46
  return out_filename
36
47
  end
@@ -1,3 +1,3 @@
1
1
  module Flickrcaptionr
2
- VERSION = "1.0.0"
2
+ VERSION = "1.1.0"
3
3
  end
@@ -0,0 +1,29 @@
1
+ require 'spec_helper'
2
+ describe Flickrcaptionr::Fetcher do
3
+ before(:each) do
4
+ @fetcher = Flickrcaptionr::Fetcher.new
5
+ end
6
+ it "fetches an image given a Flickr photo ID" do
7
+ path = @fetcher.fetch("6630238837")
8
+ File.exists?(path).should be_true
9
+ File.delete(path)
10
+ end
11
+
12
+ it "fetches an image given a Flickr photo URL" do
13
+ path = @fetcher.fetch("http://flickr.com/photos/james_harrison/6630238837/in/photostream")
14
+ File.exists?(path).should be_true
15
+ File.delete(path)
16
+ end
17
+
18
+ it "fetches an image given a direct image URL" do
19
+ path = @fetcher.fetch("http://assets.talkunafraid.co.uk/img/wish_you_were_partying_by_purpletinker-d3fjcgw.png.jpg")
20
+ File.exists?(path).should be_true
21
+ File.delete(path)
22
+ end
23
+
24
+ it "fetches an image given an OEmbed API supported address" do
25
+ path = @fetcher.fetch("http://www.youtube.com/watch?v=UzsxfO7dBlA")
26
+ File.exists?(path).should be_true
27
+ File.delete(path)
28
+ end
29
+ end
@@ -0,0 +1,3 @@
1
+ ---
2
+ :flickr_api_key: SETMETOSOEMTHING
3
+ :output_path: /tmp
@@ -0,0 +1,53 @@
1
+ require 'spec_helper'
2
+ describe Flickrcaptionr::Processor do
3
+ before(:each) do
4
+ @fetcher = Flickrcaptionr::Fetcher.new
5
+ @processor = Flickrcaptionr::Processor.new
6
+ @path = @fetcher.fetch("6630238837")
7
+ end
8
+ after(:all) do
9
+ File.delete(@path)
10
+ end
11
+
12
+ it "can resize an image to a smaller size" do
13
+ path = @processor.resize!(@path, 320, 240)
14
+ File.exists?(path).should be_true
15
+ Dimensions.width(path).should eq(320)
16
+ Dimensions.height(path).should eq(240)
17
+ File.delete(path)
18
+ end
19
+
20
+ it "can enlarge an image" do
21
+ path = @processor.resize!(@path, 3200, 2400)
22
+ File.exists?(path).should be_true
23
+ Dimensions.width(path).should eq(3200)
24
+ Dimensions.height(path).should eq(2400)
25
+ File.delete(path)
26
+ end
27
+
28
+ it "can overlay text on an image" do
29
+ path = @processor.add_text!(@path, "Hello, world!")
30
+ File.exists?(path).should be_true
31
+ end
32
+
33
+ it "can overlay text with double quotes on an image" do
34
+ path = @processor.add_text!(@path, "\"Hello, world!\"")
35
+ File.exists?(path).should be_true
36
+ end
37
+
38
+ it "can overlay text with single quotes on an image" do
39
+ path = @processor.add_text!(@path, "'Hello, world!'")
40
+ File.exists?(path).should be_true
41
+ end
42
+
43
+ it "can overlay text on an image when faced with blank input" do
44
+ path = @processor.add_text!(@path, "")
45
+ File.exists?(path).should be_true
46
+ end
47
+
48
+ it "can overlay text on an image when faced with nasty characters" do
49
+ path = @processor.add_text!(@path, '#\{32asdAS~!~5"l}')
50
+ File.exists?(path).should be_true
51
+ end
52
+
53
+ end
@@ -0,0 +1,8 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+
4
+ require 'flickrcaptionr'
5
+
6
+ RSpec.configure do |config|
7
+ config.before(:suite) { Flickrcaptionr::Config.new(File.expand_path('../flickrcaptionr.yml', __FILE__)) }
8
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: flickrcaptionr
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-06-30 00:00:00.000000000 Z
12
+ date: 2012-07-01 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: ruby-oembed
@@ -107,6 +107,22 @@ dependencies:
107
107
  - - ! '>='
108
108
  - !ruby/object:Gem::Version
109
109
  version: 1.3.1
110
+ - !ruby/object:Gem::Dependency
111
+ name: rspec
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: 2.10.0
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: 2.10.0
110
126
  description: flickrcaptionr generates images, given a Flickr or other oEmbed-supported
111
127
  source, at the resolution of your choice and optionally with image-macro-style captions
112
128
  email:
@@ -118,6 +134,8 @@ extensions: []
118
134
  extra_rdoc_files: []
119
135
  files:
120
136
  - .gitignore
137
+ - .rspec
138
+ - CHANGELOG.md
121
139
  - Gemfile
122
140
  - LICENSE
123
141
  - README.md
@@ -126,6 +144,7 @@ files:
126
144
  - bin/flickrcaptionr-web
127
145
  - flickrcaptionr.gemspec
128
146
  - fonts/Coda-Heavy.ttf
147
+ - fonts/LICENSE
129
148
  - lib/flickrcaptionr.rb
130
149
  - lib/flickrcaptionr/app.rb
131
150
  - lib/flickrcaptionr/config.rb
@@ -137,6 +156,10 @@ files:
137
156
  - lib/flickrcaptionr/processor.rb
138
157
  - lib/flickrcaptionr/version.rb
139
158
  - pub/bootstrap.min.css
159
+ - spec/fetcher_spec.rb
160
+ - spec/flickrcaptionr.yml.example.yml
161
+ - spec/processor_spec.rb
162
+ - spec/spec_helper.rb
140
163
  - views/index.haml
141
164
  homepage: http://github.com/JamesHarrison/flickrcaptionr
142
165
  licenses: []
@@ -162,4 +185,8 @@ rubygems_version: 1.8.24
162
185
  signing_key:
163
186
  specification_version: 3
164
187
  summary: flickrcaptionr turns Flickr images into (optionally) captioned thumbnails
165
- test_files: []
188
+ test_files:
189
+ - spec/fetcher_spec.rb
190
+ - spec/flickrcaptionr.yml.example.yml
191
+ - spec/processor_spec.rb
192
+ - spec/spec_helper.rb