fetch_if_url 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +2 -1
- data/lib/fetch_if_url.rb +12 -6
- data/lib/fetch_if_url/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: efcdaaaeeecbf080b39ce8e3c68c286125d5a36b1f1deeee8d312a3306936d80
|
4
|
+
data.tar.gz: ac77b081c564a522ccbf7f9bfb554d8768413c8f334d6f1baf3c5846c3894957
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 66d7b418e4bcdad53d0257ce6ac63753814f23f6d847961e22d6be53372066141b3ed77f0c2b142f87f448c8f7893391888321b91aee314b66c233626d373b60
|
7
|
+
data.tar.gz: bb0f0854e4dff88b08ad5b4de1a708233a7ac3397ebe754205dd85bdc30b2550831bdd42cc07ff7fbf4dd1fd04974343aa16570d7093e1a4892b89d9c0986e48
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
[![Gem Version](https://badge.fury.io/rb/fetch_if_url.svg)](https://badge.fury.io/rb/fetch_if_url)
|
1
2
|
[![Build Status](https://travis-ci.com/oieioi/fetch_if_url.svg?branch=master)](https://travis-ci.com/oieioi/fetch_if_url)
|
2
3
|
|
3
4
|
# FetchIfUrl
|
@@ -24,7 +25,7 @@ Or install it yourself as:
|
|
24
25
|
## Usage
|
25
26
|
|
26
27
|
```ruby
|
27
|
-
CSV.foreach(fetch_if_url('https://example.org/some_heavy_csv.csv')) { |row| ... }
|
28
|
+
CSV.foreach(FetchIfUrl.fetch_if_url('https://example.org/some_heavy_csv.csv')) { |row| ... }
|
28
29
|
```
|
29
30
|
|
30
31
|
## Contributing
|
data/lib/fetch_if_url.rb
CHANGED
@@ -7,26 +7,32 @@ require 'securerandom'
|
|
7
7
|
module FetchIfUrl
|
8
8
|
class Error < StandardError; end
|
9
9
|
|
10
|
+
# Fetch a file and return its temporary local path if <code>path</code> looks like URL.
|
11
|
+
# If it doesn't look like URL return as it be.
|
12
|
+
# @param [String] path file path or URL
|
13
|
+
# @param [Hash] opts
|
14
|
+
# @option opts [String] :dest Destination path when fetching
|
15
|
+
# @return [String] local path
|
10
16
|
def fetch_if_url(path, dest: '/tmp')
|
11
|
-
if
|
12
|
-
|
17
|
+
if url?(path)
|
18
|
+
fetch(path, dest)
|
13
19
|
else
|
14
20
|
# do nothing if local file
|
15
21
|
path
|
16
22
|
end
|
17
23
|
end
|
18
24
|
|
19
|
-
def
|
25
|
+
def url?(path)
|
20
26
|
path.start_with?(%r{https?://})
|
21
27
|
end
|
22
28
|
|
23
|
-
def
|
29
|
+
def fetch(url, dest)
|
24
30
|
body = Net::HTTP.get(URI.parse(url))
|
25
31
|
file_name = "#{dest}/#{Time.now.to_i}-#{SecureRandom.uuid}"
|
26
32
|
File.open(file_name, 'wb') { |file| file.write(body) }
|
27
33
|
file_name
|
28
34
|
end
|
29
35
|
|
30
|
-
module_function :fetch_if_url, :
|
31
|
-
private_class_method :
|
36
|
+
module_function :fetch_if_url, :url?, :fetch
|
37
|
+
private_class_method :url?, :fetch
|
32
38
|
end
|
data/lib/fetch_if_url/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fetch_if_url
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- oieioi
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-06-
|
11
|
+
date: 2019-06-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|