anyfetch 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7f6440473107e9be88c9355b4b3ba91bb518d292
4
+ data.tar.gz: 1140887e72f2336f59cc7b9738496bf6b642e8a5
5
+ SHA512:
6
+ metadata.gz: 15e01dc7b1ac52f129a086a73618b0262245ff939b3639aed43d908064b7a155e6360da4dd8e609d94ed55aed4b3c6c2d6ff25807958d77e3e86ae253625dd12
7
+ data.tar.gz: 5200670c1d3b55948c4eeaf589d9efeac0c801412c8b68cbaa8be85af8e0bd92f9f4f1713a1b023eb21281f380dcd57ed9fa70dd68f3bd3a64a9dc102e88d484
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.15.3
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in anyfetch.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Michał Szajbe
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,76 @@
1
+ # Anyfetch
2
+
3
+ Anyfetch provides abstraction layer around multiple protocols to allow accessing files in uniform way.
4
+
5
+ Current support:
6
+
7
+ * local files (via path or `file:///` protocol)
8
+ * HTTP(s)
9
+ * FTP
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'anyfetch'
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ $ bundle
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install anyfetch
26
+
27
+ ## Usage
28
+
29
+ Simply provide file's URI. Optionally an `options` hash can be provided as second argument, it will be passed "as is" to specific handler (see below).
30
+
31
+ `Anyfetch.open(uri, options)`
32
+
33
+ ### Local files
34
+
35
+ Both path and `file://` protocol are accepted.
36
+
37
+ ```
38
+ Anyfetch.open("/path/to/file")
39
+ Anyfetch.open("file:///path/to/file")
40
+ ```
41
+
42
+ ### HTTP/HTTPS and FTP
43
+
44
+ Internally `open-uri` (with extensions provided by `open_uri_redirections` gem) is used to fetch the file from HTTP and FTP servers. Feel free to pass any options recognized by `open-uri` or `open_uri_redirections`.
45
+
46
+ ```
47
+ Anyfetch.open("http://example.org/file.html")
48
+ Anyfetch.open("https://user:password@example.org/file.html", { 'User-Agent' => '...' })
49
+ Anyfetch.open("ftp://user:password@example.org/file.html")
50
+ ```
51
+
52
+ ### Original filenames
53
+
54
+ It is not always possible to know the original filename and/or content type of the accessed file upfront, e.g. when fetching files from the URLs like `http://example.org/file?id=123` or when the file is streamed from the server. Anyfetch provides `original_filename` method to the file instance to handle this. Internally it checks `content-disposition` meta information returned by the server, file's content type or simply file's basename for local files. This method can also be handy when trying to assign the file to an uploader like Paperclip or Carrierwave.
55
+
56
+ ```
57
+ file = Anyfetch.open("http://example.org/file?id=123")
58
+ file.original_filename
59
+ # => filename.html
60
+ ```
61
+
62
+ ## Development
63
+
64
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
65
+
66
+ To install this gem onto your local machine, run `bundle exec rake install`.
67
+
68
+ Found a bug? File an issue on Github. Got a patch? Submit a pull request.
69
+
70
+ ## Contributing
71
+
72
+ Bug reports and pull requests are welcome on GitHub at https://github.com/szajbus/anyfetch.
73
+
74
+ ## License
75
+
76
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/anyfetch.gemspec ADDED
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "anyfetch/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "anyfetch"
8
+ spec.version = Anyfetch::VERSION
9
+ spec.authors = ["Michał Szajbe"]
10
+ spec.email = ["michal.szajbe@gmail.com"]
11
+
12
+ spec.summary = %q{Fetch files via any protocol.}
13
+ spec.description = %q{Anyfetch provides abstraction layer around multiple protocols to allow accessing files in uniform way.}
14
+ spec.homepage = "https://github.com/szajbus/anyfetch"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_dependency 'cocaine'
25
+ spec.add_dependency 'mime-types'
26
+ spec.add_dependency 'open_uri_redirections'
27
+
28
+ spec.add_development_dependency "bundler", "~> 1.15"
29
+ spec.add_development_dependency "rake", "~> 10.0"
30
+ spec.add_development_dependency "rspec", "~> 3.0"
31
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "anyfetch"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,15 @@
1
+ require "anyfetch/original_filename/path"
2
+
3
+ module Anyfetch
4
+ class File
5
+ def initialize(uri, options = {})
6
+ @uri = uri
7
+ @options = options
8
+ end
9
+
10
+ def open
11
+ file = ::File.open(@uri.path)
12
+ file.extend(OriginalFilename::Path)
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,26 @@
1
+ require "anyfetch/original_filename/content_type"
2
+
3
+ module Anyfetch
4
+ class FTP
5
+ def initialize(uri, options = {})
6
+ @uri = uri
7
+ @options = options
8
+ setup_auth
9
+ end
10
+
11
+ def open
12
+ file = super(@uri)
13
+ file.extend(OriginalFilename::ContentType)
14
+ file
15
+ end
16
+
17
+ private
18
+
19
+ def setup_auth
20
+ if @uri.password
21
+ # Rollback URI-encoding of password by open-uri lib
22
+ @uri.instance_variable_set "@password", URI.decode(@uri.password)
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,32 @@
1
+ require "anyfetch/original_filename/content_type"
2
+
3
+ module Anyfetch
4
+ class HTTP
5
+ OPTIONS = {
6
+ "User-Agent" => "Mozilla/5.0 (X11; Linux x86_64; rv:39.0) Gecko/20100101 Firefox/39.0",
7
+ allow_redirections: :safe,
8
+ }
9
+
10
+ def initialize(uri, options)
11
+ @uri = uri
12
+ @options = OPTIONS.merge(options)
13
+ setup_basic_auth
14
+ end
15
+
16
+ def open
17
+ file = super(@uri, @options)
18
+ file.extend(OriginalFilename::ContentType)
19
+ file
20
+ end
21
+
22
+ private
23
+
24
+ def setup_basic_auth
25
+ if @uri.user || @uri.password
26
+ @options[:http_basic_authentication] = [@uri.user, @uri.password]
27
+ @uri.user = nil
28
+ @uri.password = nil
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,32 @@
1
+ require "cocaine"
2
+ require "mime-types"
3
+
4
+ module Anyfetch
5
+ module OriginalFilename
6
+ module ContentType
7
+ def original_filename
8
+ if meta.include? 'content-disposition'
9
+ match = meta['content-disposition'].match(/filename=(\"?)(.+)\1/)
10
+ return match[2] unless match.nil?
11
+ end
12
+
13
+ filename = ::File.basename(base_uri.path)
14
+ ext = ::File.extname(filename)
15
+
16
+ cmd = Cocaine::CommandLine.new('/usr/bin/file', '--mime-type -b :file')
17
+ begin
18
+ mime_type = cmd.run(file: path)
19
+ rescue
20
+ mime_type = meta['content-type']
21
+ end
22
+
23
+ if mime = MIME::Types[mime_type.to_s.strip].first
24
+ mime_ext = mime.extensions.first
25
+ ext != mime_ext ? [::File.basename(filename, ext), '.', mime_ext].join : filename
26
+ else
27
+ filename
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,9 @@
1
+ module Anyfetch
2
+ module OriginalFilename
3
+ module Path
4
+ def original_filename
5
+ ::File.basename(path)
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module Anyfetch
2
+ VERSION = "0.1.0"
3
+ end
data/lib/anyfetch.rb ADDED
@@ -0,0 +1,37 @@
1
+ require "open-uri"
2
+ require "open_uri_redirections"
3
+
4
+ require "anyfetch/file"
5
+ require "anyfetch/ftp"
6
+ require "anyfetch/http"
7
+ require "anyfetch/version"
8
+
9
+ module Anyfetch extend self
10
+ def open(uri, options = {})
11
+ uri = URI.parse(uri.to_s)
12
+ handler(uri, options).open
13
+ end
14
+
15
+ private
16
+
17
+ def to_uri(uri)
18
+ uri = uri.gsub('[', '%5B').gsub(']', '%5D').strip
19
+ uri = ::URI.parse(::URI.encode(::URI.decode(uri)))
20
+ URI.parse(uri)
21
+ end
22
+
23
+ def handler(uri, options = {})
24
+ scheme = uri.scheme || "file"
25
+
26
+ klass =
27
+ case scheme
28
+ when "file" then File
29
+ when "ftp" then FTP
30
+ when /^https?/ then HTTP
31
+ else
32
+ raise "No handler for '#{scheme}' protocol."
33
+ end
34
+
35
+ klass.new(uri, options)
36
+ end
37
+ end
metadata ADDED
@@ -0,0 +1,146 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: anyfetch
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Michał Szajbe
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-08-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: cocaine
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: mime-types
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
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: open_uri_redirections
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
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: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.15'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.15'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.0'
97
+ description: Anyfetch provides abstraction layer around multiple protocols to allow
98
+ accessing files in uniform way.
99
+ email:
100
+ - michal.szajbe@gmail.com
101
+ executables: []
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - ".gitignore"
106
+ - ".rspec"
107
+ - ".travis.yml"
108
+ - Gemfile
109
+ - LICENSE.txt
110
+ - README.md
111
+ - Rakefile
112
+ - anyfetch.gemspec
113
+ - bin/console
114
+ - bin/setup
115
+ - lib/anyfetch.rb
116
+ - lib/anyfetch/file.rb
117
+ - lib/anyfetch/ftp.rb
118
+ - lib/anyfetch/http.rb
119
+ - lib/anyfetch/original_filename/content_type.rb
120
+ - lib/anyfetch/original_filename/path.rb
121
+ - lib/anyfetch/version.rb
122
+ homepage: https://github.com/szajbus/anyfetch
123
+ licenses:
124
+ - MIT
125
+ metadata: {}
126
+ post_install_message:
127
+ rdoc_options: []
128
+ require_paths:
129
+ - lib
130
+ required_ruby_version: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - ">="
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ required_rubygems_version: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - ">="
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
140
+ requirements: []
141
+ rubyforge_project:
142
+ rubygems_version: 2.5.1
143
+ signing_key:
144
+ specification_version: 4
145
+ summary: Fetch files via any protocol.
146
+ test_files: []