rhcf-utils 0.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6d1a0ea6f99f15f7e81b21291239462daf312138
4
+ data.tar.gz: 965d1184ecf122ebd6f1202bdb10de4aa4c08ecf
5
+ SHA512:
6
+ metadata.gz: 18cb8e28818558e63942cdd47faa4faec1b5d45641535235ce63adad59a31148ad497ad349a1aa92930956e65fb43a5d9738c8f61e5f9aab84ca50b46db39d60
7
+ data.tar.gz: d32f42f65b794238cbd6234e72a66896c80b521902cd5e21d81008975af5e2a885c09a31fac151c4c8146fe221ac655fed3ed5839cf6940f8ef467d0133d28bd
data/.gitignore ADDED
@@ -0,0 +1,18 @@
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
+ .idea
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rhcf-utils.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,10 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard :rspec do
5
+ watch(%r{^spec/.+_spec\.rb$})
6
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
7
+ watch('spec/spec_helper.rb') { "spec" }
8
+
9
+ end
10
+
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Romeu Fonseca
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,35 @@
1
+ # Rhcf::Utils
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'rhcf-utils'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install rhcf-utils (not yet)
18
+
19
+ ## Usage
20
+ ### Rhcf::Utils::DownloadCache
21
+
22
+ ```ruby
23
+ require 'rhcf/utils/download_cache'
24
+ cache = Rhcf::Utils::DownloadCache.new('a_cache_id', ttl=30)
25
+ cache.get("http://example.com/a_image.png") # -> "/tmp/.../a_image.png"
26
+ ```
27
+ If you try to download in less then 30 seconds, you will hit the cache
28
+
29
+ ## Contributing
30
+
31
+ 1. Fork it ( http://github.com/romeuhcf/rhcf-utils/fork )
32
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
33
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
34
+ 4. Push to the branch (`git push origin my-new-feature`)
35
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/lib/rhcf/utils.rb ADDED
@@ -0,0 +1,7 @@
1
+ require "rhcf/utils/version"
2
+
3
+ module Rhcf
4
+ module Utils
5
+ # Your code goes here...
6
+ end
7
+ end
@@ -0,0 +1,66 @@
1
+ require 'uri'
2
+ require 'open-uri'
3
+ require 'fileutils'
4
+ require 'digest/md5'
5
+
6
+ module Rhcf
7
+ module Utils
8
+ class DownloadCache
9
+ include FileUtils
10
+ def initialize(cache_id= 'default', ttl = nil, root_path = nil)
11
+ @cache_id = cache_id
12
+ @ttl = ttl
13
+ @root_path ||= "/tmp/#{self.class.name}"
14
+ end
15
+
16
+
17
+ def get(url)
18
+ outfile = filename_for(url)
19
+ return outfile if self.class.hit_fname?(outfile, @ttl) # here goes the cache
20
+ download!(url, outfile)
21
+ rescue
22
+ File.unkink(outfile) if File.exist?(outfile)
23
+ raise
24
+ end
25
+
26
+
27
+ def download!(url, outfile)
28
+ mkdir_p(File.dirname(outfile))
29
+ File.open(outfile, 'w') do |fd|
30
+ open(url, "r") do |down|
31
+ fd.write(down.read)
32
+ end
33
+ end
34
+ outfile
35
+ end
36
+
37
+ def hit?(url)
38
+ outfile = filename_for(url)
39
+ self.class.hit_fname?(outfile, @ttl) # here goes the cache
40
+ end
41
+
42
+ def self.hit_fname?(fname, ttl)
43
+
44
+ if File.exist?(fname)
45
+ if ttl
46
+ File::Stat.new(fname).ctime > Time.now - ttl
47
+ else
48
+ true
49
+ end
50
+ else
51
+ false
52
+ end
53
+ end
54
+
55
+ def filename_for(url)
56
+ hash = Digest::MD5.hexdigest(url)
57
+ uri = URI(url)
58
+ basename = File.basename(uri.path)
59
+ File.join(@root_path, @cache_id, hash, basename)
60
+ end
61
+
62
+ end
63
+ end
64
+ end
65
+
66
+
@@ -0,0 +1,5 @@
1
+ module Rhcf
2
+ module Utils
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rhcf/utils/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rhcf-utils"
8
+ spec.version = Rhcf::Utils::VERSION
9
+ spec.authors = ["Romeu Fonseca"]
10
+ spec.email = ["romeu.hcf@gmail.com"]
11
+ spec.summary = %q{Personal Ruby Utility Tools.}
12
+ spec.description = %q{Personal set of ruby recurrent utility tools.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.5"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_development_dependency "simplecov"
25
+ spec.add_development_dependency "guard"
26
+ spec.add_development_dependency "guard-rspec"
27
+ spec.add_development_dependency "timecop"
28
+ spec.add_development_dependency "pry-debugger"
29
+ end
@@ -0,0 +1,137 @@
1
+ require 'spec_helper'
2
+ require 'rhcf/utils/download_cache'
3
+ require 'fileutils'
4
+ require 'timecop'
5
+
6
+ describe Rhcf::Utils::DownloadCache do
7
+ let(:img_url){"http://www.sunnyvision.com/images/cloud_cdn_icon.jpg"}
8
+ let(:cache_of_30s){Rhcf::Utils::DownloadCache.new('cache_of_30s', 30)}
9
+ let(:foo_cache_of_30s){Rhcf::Utils::DownloadCache.new('cache_of_30s', 30, '/tmp/foo')}
10
+ it "should be a class" do
11
+ Rhcf::Utils::DownloadCache.should be_instance_of(Class)
12
+ end
13
+
14
+
15
+ describe "#hit_fname?" do
16
+ it "should return true if file exists and ttl is null" do
17
+ Rhcf::Utils::DownloadCache.hit_fname?("/etc/passwd", nil).should be_true
18
+ end
19
+ it "should return true if file exists and newer then ttl" do
20
+ Rhcf::Utils::DownloadCache.hit_fname?("/etc/passwd", 20 * 365 * 24 * 3600 ).should be_true
21
+ end
22
+ it "should return false if file exists and older then ttl" do
23
+ Rhcf::Utils::DownloadCache.hit_fname?("/etc/passwd",30 ).should be_false
24
+ end
25
+ it "should return false if doesn't exist" do
26
+ Rhcf::Utils::DownloadCache.hit_fname?("/dfasd/asd/as/das/d/sada", nil).should be_false
27
+ end
28
+ end
29
+
30
+
31
+ describe "#hit?" do
32
+ before(:each) do
33
+ FileUtils.rm_rf "/tmp/Rhcf::Utils::DownloadCache/cache_of_30s/aea455b4b37a2d4240a6b054ca0b0d5f8"
34
+ FileUtils.mkdir_p "/tmp/Rhcf::Utils::DownloadCache/cache_of_30s/aea455b4b37a2d4240a6b054a0b0d5f8"
35
+ FileUtils.touch "/tmp/Rhcf::Utils::DownloadCache/cache_of_30s/aea455b4b37a2d4240a6b054a0b0d5f8/cloud_cdn_icon.jpg"
36
+ end
37
+ it "should return true if file downloaded and ttl is null" do
38
+ cache_of_30s.filename_for(img_url).should == '/tmp/Rhcf::Utils::DownloadCache/cache_of_30s/aea455b4b37a2d4240a6b054a0b0d5f8/cloud_cdn_icon.jpg'
39
+ cache_of_30s.hit?(img_url).should be_true
40
+ end
41
+ it "should return true if file downloaded and newer then ttl" do
42
+ cache_of_30s.hit?(img_url).should be_true
43
+ end
44
+ it "should return false if file downloaded and older then ttl" do
45
+ Timecop.travel(Time.now + 3600) do
46
+ cache_of_30s.hit?(img_url).should be_false
47
+ end
48
+ end
49
+ it "should return false if doesn't exist" do
50
+ FileUtils.rm "/tmp/Rhcf::Utils::DownloadCache/cache_of_30s/aea455b4b37a2d4240a6b054a0b0d5f8/cloud_cdn_icon.jpg"
51
+ cache_of_30s.hit?(img_url).should be_false
52
+ end
53
+
54
+
55
+ end
56
+
57
+ describe "#filename_for" do
58
+ it "should return a path with cache id , file name hash and file hash component" do
59
+ subject.filename_for(img_url).should == '/tmp/Rhcf::Utils::DownloadCache/default/aea455b4b37a2d4240a6b054a0b0d5f8/cloud_cdn_icon.jpg'
60
+ end
61
+ end
62
+
63
+ describe "#download!" do
64
+ it "should return the given file path" do
65
+ expect do
66
+ out = subject.download!(img_url, "/tmp/b/c/d/a.jpg")
67
+ out.should == "/tmp/b/c/d/a.jpg"
68
+ end.to_not raise_error
69
+ end
70
+
71
+ it "should create necessary directories and download to the given path" do
72
+ File.unlink("/tmp/b/c/d/a.jpg")
73
+ File.exist?("/tmp/b/c/d/a.jpg").should be_false
74
+ expect{ subject.download!(img_url, "/tmp/b/c/d/a.jpg") }.to_not raise_error
75
+ File.exist?("/tmp/b/c/d/a.jpg").should be_true
76
+ end
77
+ end
78
+
79
+ describe "#get" do
80
+ let(:expected_path){ '/tmp/Rhcf::Utils::DownloadCache/default/aea455b4b37a2d4240a6b054a0b0d5f8/cloud_cdn_icon.jpg' }
81
+ let(:expected_30s_path){ '/tmp/Rhcf::Utils::DownloadCache/cache_of_30s/aea455b4b37a2d4240a6b054a0b0d5f8/cloud_cdn_icon.jpg' }
82
+ before(:each) do
83
+ File.unlink(expected_path) rescue nil
84
+ end
85
+
86
+ it "should download when not exist"do
87
+ subject.get(img_url).should == expected_path
88
+ File.exist?(expected_path).should be_true
89
+ end
90
+
91
+
92
+ it "should download on different directory per cache_id" do
93
+ cache_of_30s.get(img_url).should_not == expected_path
94
+ File.exist?(expected_path).should be_false
95
+ end
96
+
97
+
98
+ it "should download on given root path" do
99
+ foo_cache_of_30s.get(img_url).should == expected_30s_path
100
+ end
101
+
102
+
103
+ it "should redownload files older then ttl" do
104
+ File.open(expected_30s_path, "w") {|fd| fd.write("oldycontent")}
105
+ File.exist?(expected_30s_path).should be_true
106
+ Timecop.travel(3600) do
107
+ cache_of_30s.get(img_url).should == expected_30s_path
108
+ end
109
+ IO.read(expected_30s_path).should_not == "oldycontent"
110
+ end
111
+
112
+
113
+ it "should not redownload files newer then ttl" do
114
+ File.open(expected_30s_path, "w") {|fd| fd.write("oldycontent")}
115
+ File.exist?(expected_30s_path).should be_true
116
+ Timecop.travel(Time.now + 3) do
117
+ cache_of_30s.get(img_url).should == expected_30s_path
118
+ end
119
+ IO.read(expected_30s_path).should == "oldycontent"
120
+ end
121
+
122
+
123
+ it "should return absolute path to downloaded or cache file" do
124
+ cache_of_30s.get(img_url).should == expected_30s_path
125
+ end
126
+
127
+ it "should raise when download problem" do
128
+ expect{ cache_of_30s.get("hftp://3423c434v234xd")}.to raise_error
129
+ end
130
+
131
+ describe "when ttl is null" do
132
+ it "should cache forever if ttl is null" do
133
+ end
134
+ end
135
+ end
136
+
137
+ end
@@ -0,0 +1,19 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+ require 'simplecov'
8
+ SimpleCov.start
9
+ RSpec.configure do |config|
10
+ config.treat_symbols_as_metadata_keys_with_true_values = true
11
+ config.run_all_when_everything_filtered = true
12
+ config.filter_run :focus
13
+
14
+ # Run specs in random order to surface order dependencies. If you find an
15
+ # order dependency and want to debug it, you can fix the order by providing
16
+ # the seed, which is printed after each run.
17
+ # --seed 1234
18
+ config.order = 'random'
19
+ end
metadata ADDED
@@ -0,0 +1,173 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rhcf-utils
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Romeu Fonseca
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-04-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: simplecov
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: guard
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: guard-rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: timecop
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: pry-debugger
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - '>='
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description: Personal set of ruby recurrent utility tools.
126
+ email:
127
+ - romeu.hcf@gmail.com
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - .gitignore
133
+ - .rspec
134
+ - Gemfile
135
+ - Guardfile
136
+ - LICENSE.txt
137
+ - README.md
138
+ - Rakefile
139
+ - lib/rhcf/utils.rb
140
+ - lib/rhcf/utils/download_cache.rb
141
+ - lib/rhcf/utils/version.rb
142
+ - rhcf-utils.gemspec
143
+ - spec/lib/rhcf/utils/.download_cache_spec.rb.swp
144
+ - spec/lib/rhcf/utils/download_cache_spec.rb
145
+ - spec/spec_helper.rb
146
+ homepage: ''
147
+ licenses:
148
+ - MIT
149
+ metadata: {}
150
+ post_install_message:
151
+ rdoc_options: []
152
+ require_paths:
153
+ - lib
154
+ required_ruby_version: !ruby/object:Gem::Requirement
155
+ requirements:
156
+ - - '>='
157
+ - !ruby/object:Gem::Version
158
+ version: '0'
159
+ required_rubygems_version: !ruby/object:Gem::Requirement
160
+ requirements:
161
+ - - '>='
162
+ - !ruby/object:Gem::Version
163
+ version: '0'
164
+ requirements: []
165
+ rubyforge_project:
166
+ rubygems_version: 2.2.2
167
+ signing_key:
168
+ specification_version: 4
169
+ summary: Personal Ruby Utility Tools.
170
+ test_files:
171
+ - spec/lib/rhcf/utils/.download_cache_spec.rb.swp
172
+ - spec/lib/rhcf/utils/download_cache_spec.rb
173
+ - spec/spec_helper.rb