aviary 1.0.1

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.
Files changed (43) hide show
  1. data/.gitignore +4 -0
  2. data/Gemfile +3 -0
  3. data/LICENSE +21 -0
  4. data/README.md +110 -0
  5. data/Rakefile +10 -0
  6. data/aviary.gemspec +32 -0
  7. data/bin/aviary +87 -0
  8. data/generator/_assets/aviary.css +95 -0
  9. data/generator/_assets/aviary.js +38 -0
  10. data/generator/_assets/loader.gif +0 -0
  11. data/generator/template.erb +45 -0
  12. data/lib/aviary.rb +28 -0
  13. data/lib/aviary/configuration.rb +36 -0
  14. data/lib/aviary/generator.rb +40 -0
  15. data/lib/aviary/image_host.rb +74 -0
  16. data/lib/aviary/image_host/flickr.rb +53 -0
  17. data/lib/aviary/image_host/plixi.rb +27 -0
  18. data/lib/aviary/image_host/twitpic.rb +13 -0
  19. data/lib/aviary/image_host/yfrog.rb +13 -0
  20. data/lib/aviary/page.rb +26 -0
  21. data/lib/aviary/paginator.rb +54 -0
  22. data/lib/aviary/search.rb +34 -0
  23. data/lib/aviary/site.rb +52 -0
  24. data/lib/aviary/version.rb +3 -0
  25. data/test/aviary/configuration_test.rb +30 -0
  26. data/test/aviary/generator_test.rb +33 -0
  27. data/test/aviary/image_host/flickr_test.rb +60 -0
  28. data/test/aviary/image_host/plixi_test.rb +30 -0
  29. data/test/aviary/image_host/twitpic_test.rb +15 -0
  30. data/test/aviary/image_host/yfrog_test.rb +15 -0
  31. data/test/aviary/image_host_test.rb +65 -0
  32. data/test/aviary/page_test.rb +29 -0
  33. data/test/aviary/paginator_test.rb +48 -0
  34. data/test/aviary/search_test.rb +43 -0
  35. data/test/aviary/site_test.rb +66 -0
  36. data/test/fixtures/flickr.xml +19 -0
  37. data/test/fixtures/plixi.xml +1 -0
  38. data/test/fixtures/source/_assets/static +0 -0
  39. data/test/fixtures/source/_assets/subdir/static +0 -0
  40. data/test/fixtures/source/template.erb +5 -0
  41. data/test/fixtures/twitter.json +1 -0
  42. data/test/helper.rb +9 -0
  43. metadata +271 -0
@@ -0,0 +1,3 @@
1
+ module Aviary
2
+ VERSION = '1.0.1'
3
+ end
@@ -0,0 +1,30 @@
1
+ require File.expand_path('../../helper', __FILE__)
2
+
3
+ class ConfigurationTest < MiniTest::Unit::TestCase
4
+ def teardown
5
+ ImageHost::Flickr.instance_variable_set('@api_key', nil)
6
+ end
7
+
8
+ def test_default_configuration
9
+ config = Configuration.new(:test)
10
+ assert_equal Dir.pwd, config[:source]
11
+ assert_equal File.join(Dir.pwd, '_site'), config[:dest]
12
+ end
13
+
14
+ def test_configuration_with_per_page_sets_per_page
15
+ assert_equal 5, Configuration.new(:test, {:per_page => 5})[:per_page]
16
+ end
17
+
18
+ def test_configuration_with_limit_sets_limit
19
+ assert_equal 100, Configuration.new(:test, {:limit => 100})[:limit]
20
+ end
21
+
22
+ def test_configuration_with_hashtag_sets_hashtag
23
+ assert_equal 'cat', Configuration.new(:test, {:hashtag => 'cat'})[:hashtag]
24
+ end
25
+
26
+ def test_configuration_with_flickr_api_key_sets_api_key
27
+ Configuration.new(:test, {:flickr_api_key => 'secret'})
28
+ assert_equal 'secret', ImageHost::Flickr.api_key
29
+ end
30
+ end
@@ -0,0 +1,33 @@
1
+ require File.expand_path('../../helper', __FILE__)
2
+
3
+ class GeneratorTest < MiniTest::Unit::TestCase
4
+ def setup
5
+ config = Aviary::Configuration.new(:default, :source => source_dir)
6
+ @generator = Generator.new(:source => config[:source], :hashtag => "cat")
7
+ end
8
+
9
+ def teardown
10
+ FileUtils.rm_rf(source_dir)
11
+ end
12
+
13
+ def source_dir
14
+ File.expand_path('../../fixtures/_source', __FILE__)
15
+ end
16
+
17
+ def test_copy_template_should_copy_template_to_source
18
+ @generator.copy_template
19
+ assert File.exists?(File.join(source_dir, '_assets', 'aviary.css'))
20
+ assert File.exists?(File.join(source_dir, '_assets', 'aviary.js'))
21
+ assert File.exists?(File.join(source_dir, '_assets', 'loader.gif'))
22
+
23
+ html = Nokogiri::HTML(File.read(File.join(source_dir, 'template.erb')))
24
+ assert_equal "#cat", html.css('title').text
25
+ assert_equal "#cat", html.css('h1').text
26
+ end
27
+
28
+ def test_migrate_should_create_database_and_schema
29
+ FileUtils.mkdir_p(source_dir)
30
+ @generator.migrate
31
+ assert File.exists?(File.join(source_dir, 'db.sqlite3'))
32
+ end
33
+ end
@@ -0,0 +1,60 @@
1
+ require File.expand_path('../../../helper', __FILE__)
2
+
3
+ class FlickrTest < MiniTest::Unit::TestCase
4
+ def setup
5
+ Aviary::Configuration.new(:test)
6
+ ImageHost::Flickr.api_key('secret')
7
+ end
8
+
9
+ def teardown
10
+ ImageHost::Flickr.instance_variable_set("@api_key", nil)
11
+ ImageHost::Flickr.destroy
12
+ end
13
+
14
+ def stub_get(url)
15
+ stub_request(:get, url).
16
+ to_return(:status => 200, :body => File.read(File.expand_path('../../../fixtures/flickr.xml', __FILE__)), :headers => {:content_type => "application/xml; charset=utf-8"})
17
+ end
18
+
19
+ def create_flickr
20
+ stub_get("http://api.flickr.com/services/rest/?method=flickr.photos.getInfo&api_key=secret&photo_id=5332667092")
21
+ @flickr = ImageHost::Flickr.create(:token => '98ej95')
22
+ end
23
+
24
+ def test_get_and_set_api_key
25
+ ImageHost::Flickr.instance_variable_set('@api_key', nil)
26
+ assert_nil ImageHost::Flickr.api_key
27
+
28
+ ImageHost::Flickr.api_key('secret')
29
+ assert_equal 'secret', ImageHost::Flickr.api_key
30
+ end
31
+
32
+ def test_available_should_be_true_when_api_key
33
+ assert ImageHost::Flickr.available?
34
+
35
+ ImageHost::Flickr.instance_variable_set('@api_key', nil)
36
+ assert !ImageHost::Flickr.available?
37
+ end
38
+
39
+ def test_match_and_create_long_url_should_encode_id_to_token_and_create_record
40
+ stub_get("http://api.flickr.com/services/rest/?method=flickr.photos.getInfo&api_key=secret&photo_id=5332667092")
41
+ ImageHost::Flickr.match_and_create(Struct::Status.new("Check out my photo http://www.flickr.com/photos/tatejohnson/5332667092/"))
42
+ assert_equal '98ej95', ImageHost::Flickr.first.token
43
+ end
44
+
45
+ def test_match_and_create_short_url_should_create_record
46
+ stub_get("http://api.flickr.com/services/rest/?method=flickr.photos.getInfo&api_key=secret&photo_id=5332667092")
47
+ ImageHost::Flickr.match_and_create(Struct::Status.new("Check out my photo http://www.flic.kr/p/98ej95"))
48
+ assert_equal '98ej95', ImageHost::Flickr.first.token
49
+ end
50
+
51
+ def test_href
52
+ create_flickr
53
+ assert_equal "http://flic.kr/p/98ej95", @flickr.href
54
+ end
55
+
56
+ def test_src
57
+ create_flickr
58
+ assert_equal "http://farm6.static.flickr.com/5010/5332667092_41a375fca3_z.jpg", @flickr.src
59
+ end
60
+ end
@@ -0,0 +1,30 @@
1
+ require File.expand_path('../../../helper', __FILE__)
2
+
3
+ class PlixiTest < MiniTest::Unit::TestCase
4
+ def setup
5
+ Aviary::Configuration.new(:test)
6
+ stub_request(:get, "http://api.plixi.com/api/tpapi.svc/photos/74226939").
7
+ to_return(:status => 200, :body => File.read(File.expand_path('../../../fixtures/plixi.xml', __FILE__)))
8
+ @plixie = ImageHost::Plixi.create(:token => '74226939')
9
+ end
10
+
11
+ def teardown
12
+ ImageHost::Plixi.destroy
13
+ end
14
+
15
+ def test_href
16
+ assert_equal "http://plixi.com/p/74226939", @plixie.href
17
+ end
18
+
19
+ def test_src
20
+ assert_equal "http://c0013634.cdn1.cloudfiles.rackspacecloud.com/x2_46c9cfb", @plixie.src
21
+ end
22
+
23
+ def test_set_meta
24
+ assert_equal "http://c0013633.cdn1.cloudfiles.rackspacecloud.com/x2_46c9cfb", @plixie.meta[:big_image_url]
25
+ assert_equal "http://c0013633.cdn1.cloudfiles.rackspacecloud.com/x2_46c9cfb", @plixie.meta[:large_image_url]
26
+ assert_equal "http://c0013634.cdn1.cloudfiles.rackspacecloud.com/x2_46c9cfb", @plixie.meta[:medium_image_url]
27
+ assert_equal "http://c0013636.cdn1.cloudfiles.rackspacecloud.com/x2_46c9cfb", @plixie.meta[:small_image_url]
28
+ assert_equal "http://c0013637.cdn1.cloudfiles.rackspacecloud.com/x2_46c9cfb", @plixie.meta[:thumbnail_image_url]
29
+ end
30
+ end
@@ -0,0 +1,15 @@
1
+ require File.expand_path('../../../helper', __FILE__)
2
+
3
+ class TwitpicTest < MiniTest::Unit::TestCase
4
+ def setup
5
+ @twitpic = ImageHost::Twitpic.new(:token => '3pbkz3')
6
+ end
7
+
8
+ def test_href
9
+ assert_equal 'http://twitpic.com/3pbkz3', @twitpic.href
10
+ end
11
+
12
+ def test_src
13
+ assert_equal 'http://twitpic.com/show/large/3pbkz3.jpg', @twitpic.src
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ require File.expand_path('../../../helper', __FILE__)
2
+
3
+ class YfrogTest < MiniTest::Unit::TestCase
4
+ def setup
5
+ @yfrog = ImageHost::Yfrog.new(:token => 'h4tsukzj')
6
+ end
7
+
8
+ def test_href
9
+ assert_equal 'http://yfrog.com/h4tsukzj', @yfrog.href
10
+ end
11
+
12
+ def test_src
13
+ assert_equal 'http://yfrog.com/h4tsukzj:medium', @yfrog.src
14
+ end
15
+ end
@@ -0,0 +1,65 @@
1
+ require File.expand_path('../../helper', __FILE__)
2
+
3
+ class ImageHostTest < MiniTest::Unit::TestCase
4
+ def setup
5
+ Aviary::Configuration.new(:test)
6
+ end
7
+
8
+ def teardown
9
+ ImageHost.destroy
10
+ ImageHost.instance_variable_set("@matches", nil)
11
+ ImageHost.instance_variable_set("@available", nil)
12
+ ImageHost::Flickr.instance_variable_set("@api_key", nil)
13
+ end
14
+
15
+ def test_available_should_have_four_descendants_with_flickr_api_key
16
+ ImageHost::Flickr.api_key('secret')
17
+ assert_equal [ImageHost::Flickr, ImageHost::Plixi, ImageHost::Twitpic, ImageHost::Yfrog], ImageHost.available
18
+ end
19
+
20
+ def test_available_should_have_three_descendants
21
+ assert_equal [ImageHost::Plixi, ImageHost::Twitpic, ImageHost::Yfrog], ImageHost.available
22
+ end
23
+
24
+ def test_available_should_be_true
25
+ assert ImageHost.available?
26
+ end
27
+
28
+ def test_get_and_set_matches
29
+ ImageHost.matches /foo/
30
+ assert_equal [/foo/], ImageHost.matches
31
+ end
32
+
33
+ def test_match_should_be_captures
34
+ ImageHost.matches /foo/
35
+ ImageHost.matches /bar/
36
+ assert_equal [], ImageHost.match("alice and bob")
37
+ assert_equal ["foo"], ImageHost.match("foo before baz")
38
+ assert_equal ["foo", "bar", "foo"], ImageHost.match("foo, bar and another foo")
39
+ end
40
+
41
+ def test_match_and_create_should_create_records
42
+ ImageHost.matches /foo/
43
+ ImageHost.match_and_create(Struct::Status.new("alice and bob"))
44
+ assert_equal 0, ImageHost.count
45
+
46
+ ImageHost.match_and_create(Struct::Status.new("foo before baz"))
47
+ assert_equal 1, ImageHost.count
48
+ end
49
+
50
+ def test_match_and_create_should_not_duplicate_records
51
+ ImageHost.matches /foo/
52
+ ImageHost.matches /bar/
53
+ ImageHost.match_and_create(Struct::Status.new("foo, bar and a duplicate foo"))
54
+ assert_equal 2, ImageHost.count
55
+ assert_equal ["foo", "bar"], ImageHost.all.map { |im| im.token }
56
+ end
57
+
58
+ def test_href
59
+ assert_raises(NotImplementedError) { ImageHost.new.href }
60
+ end
61
+
62
+ def test_src
63
+ assert_raises(NotImplementedError) { ImageHost.new.src }
64
+ end
65
+ end
@@ -0,0 +1,29 @@
1
+ require File.expand_path('../../helper', __FILE__)
2
+
3
+ class PageTest < MiniTest::Unit::TestCase
4
+ def setup
5
+ Aviary::Configuration.new(:test)
6
+ 10.times { |n| ImageHost.create(:token => n) }
7
+ @page = Page.new(Paginator.new(5))
8
+ end
9
+
10
+ def teardown
11
+ ImageHost.destroy
12
+ end
13
+
14
+ def test_binding_should_be_public
15
+ assert @page.binding
16
+ end
17
+
18
+ def test_paginator_should_be_a_paginator
19
+ assert_instance_of Paginator, @page.paginator
20
+ end
21
+
22
+ def test_image_hosts_should_have_five_image_hosts
23
+ assert_equal 5, @page.image_hosts.size
24
+ end
25
+
26
+ def test_h_should_escape_unsafe_characters
27
+ assert_equal "&lt;script&gt;", @page.h("<script>")
28
+ end
29
+ end
@@ -0,0 +1,48 @@
1
+ require File.expand_path('../../helper', __FILE__)
2
+
3
+ class PaginatorTest < MiniTest::Unit::TestCase
4
+ def setup
5
+ Aviary::Configuration.new(:test)
6
+ 10.times { |n| ImageHost.create(:token => n) }
7
+ @paginator = Paginator.new(5)
8
+ end
9
+
10
+ def teardown
11
+ ImageHost.destroy
12
+ end
13
+
14
+ def test_query_options_should_limit_results
15
+ assert_equal({:limit => 5, :offset => 0}, @paginator.query_options)
16
+
17
+ @paginator.next_page!
18
+ assert_equal({:limit => 5, :offset => 5}, @paginator.query_options)
19
+ end
20
+
21
+ def test_next_page_boolean
22
+ assert @paginator.next_page?
23
+
24
+ @paginator.instance_variable_set("@current_page", @paginator.last_page)
25
+ assert !@paginator.next_page?
26
+ end
27
+
28
+ def test_next_page_should_be_current_page_plus_one
29
+ assert_equal 2, @paginator.next_page
30
+ end
31
+
32
+ def test_next_page_bang_should_set_current_page
33
+ @paginator.next_page!
34
+ assert_equal 2, @paginator.current_page
35
+ end
36
+
37
+ def test_prev_page_boolean
38
+ assert !@paginator.prev_page?
39
+
40
+ @paginator.instance_variable_set("@current_page", @paginator.last_page)
41
+ assert @paginator.prev_page?
42
+ end
43
+
44
+ def test_prev_page_should_be_current_page_less_one
45
+ @paginator.instance_variable_set("@current_page", @paginator.last_page)
46
+ assert_equal 1, @paginator.prev_page
47
+ end
48
+ end
@@ -0,0 +1,43 @@
1
+ require File.expand_path('../../helper', __FILE__)
2
+
3
+ class SearchTest < MiniTest::Unit::TestCase
4
+ def setup
5
+ Aviary::Configuration.new(:test)
6
+ stub_get("https://search.twitter.com/search.json?q=filter:links%20-rt%20%23cat&rpp=100")
7
+ stub_get("https://search.twitter.com/search.json?max_id=34911824591724544&page=2&q=filter:links%20-rt%20%23cat&rpp=100")
8
+ stub_get("https://search.twitter.com/search.json?max_id=34911824591724544&page=3&q=filter:links%20-rt%20%23cat&rpp=100")
9
+ end
10
+
11
+ def teardown
12
+ ImageHost.destroy
13
+ end
14
+
15
+ def stub_get(url)
16
+ stub_request(:get, url).
17
+ to_return(:status => 200, :body => File.read(File.expand_path('../../fixtures/twitter.json', __FILE__)), :headers => {:content_type => "application/json; charset=utf-8"})
18
+ end
19
+
20
+ def test_process_should_match_and_create_records
21
+ Search.new(:hashtag => 'cat', :limit => 3).process
22
+ assert_equal 2, ImageHost::Yfrog.count
23
+ assert_equal 13, ImageHost::Twitpic.count
24
+ end
25
+
26
+ def test_process_should_finish_at_page_3
27
+ search = Search.new(:hashtag => 'cat', :limit => 3)
28
+ search.process
29
+ assert_equal 3, search.current_page
30
+ end
31
+
32
+ def test_next_page_boolean_should_be_false_when_at_limit
33
+ assert !Search.new(:hashtag => 'cat', :limit => 1).next_page?
34
+ end
35
+
36
+ def test_next_page_bang_should_increment_current_page
37
+ search = Search.new(:hashtag => 'cat')
38
+ assert_equal 1, search.current_page
39
+
40
+ search.next_page!
41
+ assert_equal 2, search.current_page
42
+ end
43
+ end
@@ -0,0 +1,66 @@
1
+ require File.expand_path('../../helper', __FILE__)
2
+
3
+ class SiteTest < MiniTest::Unit::TestCase
4
+ def setup
5
+ Aviary::Configuration.new(:test)
6
+ 10.times { |n| ImageHost.create(:token => n) }
7
+ @site = Site.new(:source => source_dir, :dest => dest_dir, :per_page => 5)
8
+ end
9
+
10
+ def teardown
11
+ ImageHost.destroy
12
+ FileUtils.rm_rf(dest_dir)
13
+ end
14
+
15
+ def source_dir
16
+ File.expand_path('../../fixtures/source', __FILE__)
17
+ end
18
+
19
+ def dest_dir
20
+ File.join(source_dir, '_site')
21
+ end
22
+
23
+ def test_process_should_create_dest
24
+ assert !File.exists?(dest_dir)
25
+
26
+ @site.process
27
+ assert File.exists?(dest_dir)
28
+ end
29
+
30
+ def test_process_should_generate_gallery
31
+ @site.process
32
+ assert File.exists?(File.join(dest_dir, 'index.htm'))
33
+ assert File.exists?(File.join(dest_dir, 'static'))
34
+ assert File.exists?(File.join(dest_dir, 'subdir', 'static'))
35
+ assert File.exists?(File.join(dest_dir, 'page1', 'index.htm'))
36
+ assert File.exists?(File.join(dest_dir, 'page2', 'index.htm'))
37
+ end
38
+
39
+ def test_render_should_generate_current_page
40
+ @site.render
41
+ assert File.exists?(File.join(dest_dir, 'page1', 'index.htm'))
42
+
43
+ @site.paginator.next_page!
44
+ @site.render
45
+ assert File.exists?(File.join(dest_dir, 'page2', 'index.htm'))
46
+ end
47
+
48
+ def test_copy_index_should_copy_page1_to_index
49
+ @site.render
50
+ @site.copy_index
51
+ assert File.exists?(File.join(dest_dir, 'index.htm'))
52
+ end
53
+
54
+ def test_copy_assets_should_copy_assets_into_dest
55
+ @site.copy_assets
56
+ assert File.exists?(File.join(dest_dir, 'static'))
57
+ assert File.exists?(File.join(dest_dir, 'subdir', 'static'))
58
+ end
59
+
60
+ def test_current_page_path_should_be_path_to_current_page
61
+ assert_equal File.join(dest_dir, 'page1'), @site.current_page_path
62
+
63
+ @site.paginator.next_page!
64
+ assert_equal File.join(dest_dir, 'page2'), @site.current_page_path
65
+ end
66
+ end