file_to_download 0.0.0

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: 909b5b4243071fd43026e9bdc433783a99803245
4
+ data.tar.gz: adde27bec047e507f5e090d3909e7d821de1bb1f
5
+ SHA512:
6
+ metadata.gz: 5b2e5d91d33eca1628549287b0c4bd21870b2f0508d62a0e42061760015fa6e3f5c9a0abdc19bae7ccd18c7f53ef8fbc9606b858f10bb00eee5ebfdd8073b3d0
7
+ data.tar.gz: de3e4ece4076556d1984de2ebb1c3e91d6e3745834d47620747d1cea7bf50b9f6e9be6fd9836341f017d5a34acae78d86b802e63d882e19a5b17498817ececb0
data/.gitignore ADDED
@@ -0,0 +1,25 @@
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
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
23
+
24
+ spec/write/brkb.html
25
+ .DS_Store
data/.rvmrc ADDED
@@ -0,0 +1,62 @@
1
+ #!/usr/bin/env bash
2
+
3
+ # This is an RVM Project .rvmrc file, used to automatically load the ruby
4
+ # development environment upon cd'ing into the directory
5
+
6
+ # First we specify our desired <ruby>[@<gemset>], the @gemset name is optional,
7
+ # Only full ruby name is supported here, for short names use:
8
+ # echo "rvm use 2.0.0" > .rvmrc
9
+ environment_id="ruby-2.0.0-p451@file_to_download"
10
+
11
+ # Uncomment the following lines if you want to verify rvm version per project
12
+ # rvmrc_rvm_version="1.25.20 (master)" # 1.10.1 seems like a safe start
13
+ # eval "$(echo ${rvm_version}.${rvmrc_rvm_version} | __rvm_awk -F. '{print "[[ "$1*65536+$2*256+$3" -ge "$4*65536+$5*256+$6" ]]"}' )" || {
14
+ # echo "This .rvmrc file requires at least RVM ${rvmrc_rvm_version}, aborting loading."
15
+ # return 1
16
+ # }
17
+
18
+ # First we attempt to load the desired environment directly from the environment
19
+ # file. This is very fast and efficient compared to running through the entire
20
+ # CLI and selector. If you want feedback on which environment was used then
21
+ # insert the word 'use' after --create as this triggers verbose mode.
22
+ if [[ -d "${rvm_path:-$HOME/.rvm}/environments"
23
+ && -s "${rvm_path:-$HOME/.rvm}/environments/$environment_id" ]]
24
+ then
25
+ \. "${rvm_path:-$HOME/.rvm}/environments/$environment_id"
26
+ for __hook in "${rvm_path:-$HOME/.rvm}/hooks/after_use"*
27
+ do
28
+ if [[ -f "${__hook}" && -x "${__hook}" && -s "${__hook}" ]]
29
+ then \. "${__hook}" || true
30
+ fi
31
+ done
32
+ unset __hook
33
+ if (( ${rvm_use_flag:=1} >= 2 )) # display only when forced
34
+ then
35
+ if [[ $- == *i* ]] # check for interactive shells
36
+ then printf "%b" "Using: $(tput setaf 2 2>/dev/null)$GEM_HOME$(tput sgr0 2>/dev/null)
37
+ " # show the user the ruby and gemset they are using in green
38
+ else printf "%b" "Using: $GEM_HOME
39
+ " # don't use colors in non-interactive shells
40
+ fi
41
+ fi
42
+ else
43
+ # If the environment file has not yet been created, use the RVM CLI to select.
44
+ rvm --create "$environment_id" || {
45
+ echo "Failed to create RVM environment '${environment_id}'."
46
+ return 1
47
+ }
48
+ fi
49
+
50
+ # If you use bundler, this might be useful to you:
51
+ # if [[ -s Gemfile ]] && {
52
+ # ! builtin command -v bundle >/dev/null ||
53
+ # builtin command -v bundle | GREP_OPTIONS="" \grep $rvm_path/bin/bundle >/dev/null
54
+ # }
55
+ # then
56
+ # printf "%b" "The rubygem 'bundler' is not installed. Installing it now.\n"
57
+ # gem install bundler
58
+ # fi
59
+ # if [[ -s Gemfile ]] && builtin command -v bundle >/dev/null
60
+ # then
61
+ # bundle install | GREP_OPTIONS="" \grep -vE '^Using|Your bundle is complete'
62
+ # fi
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in file_to_download.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 TODO: Write your name
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,55 @@
1
+ # FileToDownload
2
+
3
+ ## Purpose
4
+ This Ruby Gem is used for the process of downloading a file and saving it on the local drive.
5
+
6
+ This gem can obtain the age and size of a file.
7
+
8
+ You can conditionally download a remote file and save a local copy.
9
+
10
+ ## Commands for development
11
+ 1. git clone https://github.com/jhsu802701/file_to_download.git
12
+ 2. cd file_to_download
13
+ 3. gem install rspec
14
+ 4. rake # runs tests
15
+ 5. rake install # installs this gem
16
+
17
+ ## Installation
18
+
19
+ Add this line to your application's Gemfile:
20
+
21
+ gem 'file_to_download'
22
+
23
+ And then execute:
24
+
25
+ $ bundle
26
+
27
+ Or install it yourself as:
28
+
29
+ $ gem install file_to_download
30
+
31
+ ## Usage
32
+
33
+ ftd = Filetodownload.new("file_path") &#35; Initialize path name of file
34
+
35
+ ftd.size &#35; Gets size of file in bytes
36
+
37
+ ftd.age &#35; Gets age of file in hours
38
+
39
+ &#35; Download web page and save it to the above file path
40
+
41
+ &#35; Give up after 4 failed attempts
42
+
43
+ &#35; Keep the existing file if it's no more than 1.0 hours old
44
+
45
+ &#35; Pause for up to 0.5 seconds after the download
46
+
47
+ ftd.get("http://finance.yahoo.com/q/pr?s=BRK-B+Profile", 4, 1.0, 0.5)
48
+
49
+ ## Contributing
50
+
51
+ 1. Fork it ( https://github.com/[my-github-username]/file_to_download/fork )
52
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
53
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
54
+ 4. Push to the branch (`git push origin my-new-feature`)
55
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new
5
+
6
+ task :default => :spec
7
+ task :test => :spec
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'file_to_download/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "file_to_download"
8
+ spec.version = FileToDownload::VERSION
9
+ spec.authors = ["Jason Hsu"]
10
+ spec.email = ["rubyist@jasonhsu.com"]
11
+ spec.summary = %q{Gem for managing the process of downloading files}
12
+ spec.description = %q{Gets the size and age of a file and whether to replace an existing file}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.rubyforge_project = "periodic_table"
17
+
18
+ spec.files = `git ls-files -z`.split("\x0")
19
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
20
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.6"
24
+ spec.add_development_dependency "rake"
25
+ spec.add_development_dependency 'rspec'
26
+ spec.add_development_dependency 'open-uri'
27
+
28
+ end
@@ -0,0 +1,79 @@
1
+ #!/usr/bin/env ruby
2
+ require "file_to_download/version"
3
+
4
+ class Filetodownload
5
+
6
+ attr :path
7
+
8
+ def initialize (path)
9
+ @path = path
10
+ end
11
+
12
+ # Get size of file in bytes (0 if it does not exist)
13
+ def size # in bytes
14
+ begin
15
+ output = File.stat(path).size
16
+ return output
17
+ rescue
18
+ return 0
19
+ end
20
+ end
21
+
22
+ # Get the age of a given file (in hours)
23
+ # If the file is not found, return 4 billion (over 100,000 years).
24
+ def age # In hours
25
+ begin
26
+ age_seconds = Time.now - File.mtime(path)
27
+ age_hours = age_seconds/3600
28
+ return age_hours
29
+ rescue
30
+ return 4E9
31
+ end
32
+ end
33
+
34
+ # This condition determines whether to keep downloading
35
+ # n_fail: current number of failed download attempts
36
+ # n_fail_max: maximum number of failed download attempts to make before giving up
37
+ # age_max_hrs: skip the downloaded unless the age of the file in hours exceeds this
38
+ def conditionDownload (n_fail, n_fail_max, age_max_hrs)
39
+ return false if n_fail >= n_fail_max
40
+ return true if size == 0
41
+ return true if age > age_max_hrs
42
+ return false
43
+ end
44
+
45
+ # PURPOSE: downloads file from url, saves locally as filename
46
+ # age_max: skip download unless existing file named filename has
47
+ # reached the maximum age (hours)
48
+ # t_delay: maximum delay in seconds following download, needed to
49
+ # minimize the impact on the upstream server
50
+
51
+ # url: web address of the page to be downloaded
52
+ # delay_max_sec: time delay before making the download (in seconds)
53
+ def get (url, n_fail_max, age_max_hrs, delay_max_sec)
54
+ require "open-uri"
55
+ # Number of failures to download file
56
+ n_fail = 0
57
+ # Download new file ONLY if the file size is 0 or is too old
58
+ # and ONLY if there have been fewer than 4 failures
59
+ while conditionDownload(n_fail, n_fail_max, age_max_hrs) == true
60
+ begin
61
+ open(path, 'w') do |fo|
62
+ fo.write open(url).read
63
+ end
64
+ # Delay of .5*delay_max_sec to 1*delay_max_sex
65
+ t = rand (delay_max_sec/2 .. delay_max_sec)
66
+ sleep (t)
67
+ rescue
68
+ n_fail += 1
69
+ puts ("Failure #{n_fail}/#{n_fail_max} #{url}")
70
+ puts ("Download failed, giving up") if n_fail > n_fail_max
71
+ end
72
+ end
73
+ end
74
+
75
+ def delete
76
+ system ("rm #{path}")
77
+ end
78
+
79
+ end
@@ -0,0 +1,3 @@
1
+ module FileToDownload
2
+ VERSION = "0.0.0"
3
+ end
@@ -0,0 +1,37 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # GENERAL DIRECTORIES
4
+ ENV["DIR_MAIN"] = File.expand_path("../../../", __FILE__)
5
+ ENV["DIR_MAIN_SCRIPTS"] = "#{ENV["DIR_MAIN"]}/lib"
6
+
7
+ # TEST MODE
8
+ ENV["DIR_SPEC"] = "#{ENV["DIR_MAIN"]}/spec"
9
+ ENV["DIR_SPEC_WRITE"] = "#{ENV["DIR_SPEC"]}/write"
10
+ ENV["DIR_SPEC_READ"] = "#{ENV["DIR_SPEC"]}/read"
11
+
12
+ puts "Main directory: #{ENV["DIR_MAIN"]}"
13
+
14
+ puts "Test directory: #{ENV["DIR_SPEC"]}"
15
+ puts "Test write directory: #{ENV["DIR_SPEC_WRITE"]}"
16
+ puts "Test read directory: #{ENV["DIR_SPEC_READ"]}"
17
+
18
+ require "#{ENV['DIR_MAIN_SCRIPTS']}/file_to_download"
19
+
20
+ describe Filetodownload do
21
+ file_to_read = Filetodownload.new ("#{ENV["DIR_SPEC_READ"]}/test.txt")
22
+ file_to_read.size.should == 50
23
+
24
+ file_to_read = Filetodownload.new ("#{ENV["DIR_SPEC_READ"]}/doesnotexist.txt")
25
+ file_to_read.size.should == 0
26
+ file_to_read.age.should == 4E9
27
+
28
+
29
+ file_to_write = Filetodownload.new ("#{ENV["DIR_SPEC_WRITE"]}/brkb.html")
30
+ file_to_write.delete
31
+ url_brkb = "http://finance.yahoo.com/q/pr?s=BRK-B+Profile"
32
+ file_to_write.get(url_brkb, 4, 1.0, 0.5)
33
+ file_to_write.age.should > 0.2/3600.0
34
+ file_to_write.age.should < 10.0/3600.0
35
+ file_to_write.size.should > 1E4
36
+
37
+ end
@@ -0,0 +1 @@
1
+ TESTING, TESTING, 1 billion, 2 billion, 3 billion!
@@ -0,0 +1 @@
1
+ require 'file_to_download'
@@ -0,0 +1,7 @@
1
+ {\rtf1\ansi\ansicpg1252\cocoartf1265\cocoasubrtf190
2
+ {\fonttbl\f0\fswiss\fcharset0 Helvetica;}
3
+ {\colortbl;\red255\green255\blue255;}
4
+ \margl1440\margr1440\vieww10800\viewh8400\viewkind0
5
+ \pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\pardirnatural
6
+
7
+ \f0\fs24 \cf0 This directory is used for files downloaded during the test script.}
metadata ADDED
@@ -0,0 +1,120 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: file_to_download
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Jason Hsu
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-04-24 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.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
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: open-uri
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
+ description: Gets the size and age of a file and whether to replace an existing file
70
+ email:
71
+ - rubyist@jasonhsu.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - .rvmrc
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - file_to_download.gemspec
83
+ - lib/file_to_download.rb
84
+ - lib/file_to_download/.DS_Store
85
+ - lib/file_to_download/version.rb
86
+ - spec/features/.DS_Store
87
+ - spec/features/file_spec.rb
88
+ - spec/read/test.txt
89
+ - spec/spec_helper.rb
90
+ - spec/write/README.txt
91
+ homepage: ''
92
+ licenses:
93
+ - MIT
94
+ metadata: {}
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - '>='
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project: periodic_table
111
+ rubygems_version: 2.2.2
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: Gem for managing the process of downloading files
115
+ test_files:
116
+ - spec/features/.DS_Store
117
+ - spec/features/file_spec.rb
118
+ - spec/read/test.txt
119
+ - spec/spec_helper.rb
120
+ - spec/write/README.txt