random_jpg 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .rspec
19
+ .rvmrc
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.2
4
+ - 1.9.3
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in random_jpg.gemspec
4
+ gemspec
5
+
6
+ gem "rspec"
7
+ gem "rake"
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Łukasz Adamczak
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,56 @@
1
+ # random_jpg
2
+
3
+ random_jpg is a tool for easy downloading **random images from the web** for use in scripts, application test data etc.
4
+ It runs silently in the background feeding random [Flickr](http://www.flickr.com) images to a [named pipe](http://en.wikipedia.org/wiki/Named_pipe) at a specified location, by default `/tmp/random.jpg`. By using a constant location, this simplifies a number of tasks related to downloading images.
5
+
6
+ ## Installation
7
+
8
+ Install using:
9
+
10
+ $ gem install random_jpg
11
+
12
+ ## Requirements
13
+
14
+ [![Build Status](https://secure.travis-ci.org/czak/random_jpg.png?branch=master)](http://travis-ci.org/czak/random_jpg)
15
+
16
+
17
+ random_jpg is verified to run under:
18
+
19
+ * Ruby 1.9.2
20
+ * Ruby 1.9.3
21
+
22
+ I'm using OS X and believe any Unix-based system will work.
23
+ I don't expect it to work under Windows, since I'm using `mkfifo` internally.
24
+
25
+ ## Usage
26
+
27
+ Typical scenario: run once, use everywhere.
28
+
29
+ $ random_jpg --daemon
30
+
31
+ This creates the pipe at the default location `/tmp/random.jpg`. Now you can download 3 random images to `~/Desktop` using:
32
+
33
+ $ cp /tmp/random.jpg ~/Desktop/a.jpg
34
+ $ cp /tmp/random.jpg ~/Desktop/b.jpg
35
+ $ cp /tmp/random.jpg ~/Desktop/c.jpg
36
+
37
+ Or you can use it for seed data in loops:
38
+
39
+ ```ruby
40
+ 10.times do
41
+ post = Post.new
42
+ post.title = Faker::Lorem.words
43
+ post.image = File.open('/tmp/random.jpg')
44
+ post.save!
45
+ end
46
+ ```
47
+
48
+ On each run, a new image will be downloaded and served.
49
+
50
+ For full usage info, run:
51
+
52
+ $ random_jpg -h
53
+
54
+ ## Info
55
+
56
+ random_jpg © 2012 [Łukasz Adamczak](http://czak.pl), read LICENSE file for details.
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+ require 'rspec/core/rake_task'
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task :default => :spec
data/bin/random_jpg ADDED
@@ -0,0 +1,13 @@
1
+ #!/usr/bin/env ruby
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__) + '/../lib') unless $LOAD_PATH.include?(File.dirname(__FILE__) + '/../lib')
3
+
4
+ require "rubygems"
5
+ require "random_jpg"
6
+
7
+ begin
8
+ RandomJpg::Runner.new(ARGV).run
9
+ exit 0
10
+ rescue RandomJpg::Error => e
11
+ STDERR.puts e.message
12
+ exit 1
13
+ end
@@ -0,0 +1,3 @@
1
+ module RandomJpg
2
+ class Error < StandardError; end
3
+ end
@@ -0,0 +1,18 @@
1
+ require "net/http"
2
+ require "json"
3
+
4
+ module RandomJpg
5
+ class Loader
6
+ API_URL = "http://api.flickr.com/services/rest/?method=flickr.photos.getRecent&api_key=66c61b93c4723c7c3a3c519728eac252&per_page=1&extras=url_m&format=json"
7
+
8
+ def feed(path)
9
+ response = Net::HTTP.get URI(API_URL)
10
+ hash = JSON.parse response[14..-2]
11
+ image_data = Net::HTTP.get URI(hash["photos"]["photo"].first["url_m"])
12
+
13
+ File.open(path, "w") do |f|
14
+ f.write image_data
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,51 @@
1
+ require "optparse"
2
+
3
+ module RandomJpg
4
+ class Runner
5
+ def initialize(args = [])
6
+ @path = "/tmp/random.jpg"
7
+ @daemon = false
8
+ @force = false
9
+ @loader = Loader.new
10
+
11
+ OptionParser.new do |opts|
12
+ opts.banner = "Usage: random_jpg [options]"
13
+ opts.separator ""
14
+ opts.separator "Options:"
15
+ opts.on("-p", "--path PATH", "Full path to the random image file") do |path|
16
+ @path = path
17
+ end
18
+ opts.on("-d", "--daemon", "Run in the background") do
19
+ @daemon = true
20
+ end
21
+ opts.on("-f", "--force", "Force overwrite if a file exists at given path") do
22
+ @force = true
23
+ end
24
+ opts.on_tail("-h", "--help", "Show this message") do
25
+ puts opts
26
+ exit
27
+ end
28
+ end.parse!(args)
29
+ end
30
+
31
+ def run
32
+ create_pipe(@path, @force)
33
+ Process.daemon if @daemon
34
+ trap("SIGINT") { File.unlink(@path); exit }
35
+ loop { @loader.feed(@path) }
36
+ end
37
+
38
+ def create_pipe(path, force = false)
39
+ if File.exists?(path)
40
+ if force
41
+ raise Error.new("Permission denied: #{path}") unless File.writable?(path)
42
+ File.unlink(path)
43
+ else
44
+ raise Error.new("Unable to create pipe: #{path}, file exists. Use --force to overwrite.")
45
+ end
46
+ end
47
+
48
+ system "mkfifo #{path}"
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,3 @@
1
+ module RandomJpg
2
+ VERSION = "0.1.0"
3
+ end
data/lib/random_jpg.rb ADDED
@@ -0,0 +1,4 @@
1
+ require "random_jpg/version"
2
+ require "random_jpg/error"
3
+ require "random_jpg/runner"
4
+ require "random_jpg/loader"
@@ -0,0 +1,17 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/random_jpg/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Łukasz Adamczak"]
6
+ gem.email = ["lukasz@czak.pl"]
7
+ gem.description = %q{RandomJpg is a tool for easy downloading random images for use in scripts, application test data etc. It runs silently in the background feeding random Flickr images to a named pipe at a specified location, by default /tmp/random.jpg.}
8
+ gem.summary = %q{A command-line tool for easy downloading & reading random images from the web.}
9
+ gem.homepage = "https://github.com/czak/random_jpg"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "random_jpg"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = RandomJpg::VERSION
17
+ end
@@ -0,0 +1,52 @@
1
+ require "spec_helper"
2
+
3
+ module RandomJpg
4
+ describe Runner do
5
+ describe "#create_pipe(path, force = false)" do
6
+ let(:path) { "/tmp/fullpath.jpg" }
7
+
8
+ context "when no file exists at path" do
9
+ before { File.stub :exists? => false }
10
+
11
+ it "creates a named pipe and returns true" do
12
+ subject.should_receive(:system).with("mkfifo #{path}").and_return(true)
13
+ subject.create_pipe(path).should be_true
14
+ end
15
+ end
16
+
17
+ context "when a file exists at path" do
18
+ before { File.stub :exists? => true }
19
+
20
+ context "and force = false" do
21
+ it "raises a RandomJpg::Error" do
22
+ expect {
23
+ subject.create_pipe(path)
24
+ }.to raise_error(RandomJpg::Error)
25
+ end
26
+ end
27
+
28
+ context "and force = true" do
29
+ context "when user has write access to file" do
30
+ before { File.stub :writable? => true }
31
+
32
+ it "unlinks the original file and creates the pipe" do
33
+ File.should_receive(:unlink).with(path)
34
+ subject.should_receive(:system).with("mkfifo #{path}").and_return(true)
35
+ subject.create_pipe(path, true).should be_true
36
+ end
37
+ end
38
+
39
+ context "but user has no permission to write to the file" do
40
+ before { File.stub :writable? => false }
41
+
42
+ it "raises RandomJpg::Error" do
43
+ expect {
44
+ subject.create_pipe(path, true)
45
+ }.to raise_error(RandomJpg::Error)
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,13 @@
1
+ require "random_jpg"
2
+
3
+ RSpec.configure do |config|
4
+ config.treat_symbols_as_metadata_keys_with_true_values = true
5
+ config.run_all_when_everything_filtered = true
6
+ config.filter_run :focus
7
+
8
+ # Run specs in random order to surface order dependencies. If you find an
9
+ # order dependency and want to debug it, you can fix the order by providing
10
+ # the seed, which is printed after each run.
11
+ # --seed 1234
12
+ config.order = 'random'
13
+ end
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: random_jpg
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Łukasz Adamczak
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-08-23 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: RandomJpg is a tool for easy downloading random images for use in scripts,
15
+ application test data etc. It runs silently in the background feeding random Flickr
16
+ images to a named pipe at a specified location, by default /tmp/random.jpg.
17
+ email:
18
+ - lukasz@czak.pl
19
+ executables:
20
+ - random_jpg
21
+ extensions: []
22
+ extra_rdoc_files: []
23
+ files:
24
+ - .gitignore
25
+ - .travis.yml
26
+ - Gemfile
27
+ - LICENSE
28
+ - README.md
29
+ - Rakefile
30
+ - bin/random_jpg
31
+ - lib/random_jpg.rb
32
+ - lib/random_jpg/error.rb
33
+ - lib/random_jpg/loader.rb
34
+ - lib/random_jpg/runner.rb
35
+ - lib/random_jpg/version.rb
36
+ - random_jpg.gemspec
37
+ - spec/random_jpg/runner_spec.rb
38
+ - spec/spec_helper.rb
39
+ homepage: https://github.com/czak/random_jpg
40
+ licenses: []
41
+ post_install_message:
42
+ rdoc_options: []
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ none: false
47
+ requirements:
48
+ - - ! '>='
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ! '>='
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ requirements: []
58
+ rubyforge_project:
59
+ rubygems_version: 1.8.21
60
+ signing_key:
61
+ specification_version: 3
62
+ summary: A command-line tool for easy downloading & reading random images from the
63
+ web.
64
+ test_files:
65
+ - spec/random_jpg/runner_spec.rb
66
+ - spec/spec_helper.rb