robohash_client 0.0.2 → 0.0.3
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 +4 -4
- data/lib/robohash_client.rb +69 -27
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b682fec95cae8ea1d5e02b5cb5c5123987991939
|
4
|
+
data.tar.gz: 384b0c778ee03fa48a7978996339007e5c8419ae
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 39077a6b2d276813133c23e6e80dfb4a2d8d0c9a4edd05fe56992dbd1fa50bd79f3db54c8fecaedcca8fa37432390bcd2d18196161677069dca056f693f3af33
|
7
|
+
data.tar.gz: 057e7c00cf58fb452d348c9755b2cf0c07f3a59e8bd25255ad33720b51b4c9d1e0146682a277a3a2557cc63ba442fe229c213cf5b85e22e07ac9caba01d778a7
|
data/lib/robohash_client.rb
CHANGED
@@ -2,35 +2,77 @@ require 'net/http'
|
|
2
2
|
require 'json'
|
3
3
|
|
4
4
|
class Robohash
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
5
|
+
class << self
|
6
|
+
BASE_URL = 'https://robohash.org'
|
7
|
+
DEFAULT_DIRECTORY = 'robohash_images'
|
8
|
+
BGSET = 'bgset='
|
9
|
+
SIZE = 'size='
|
10
|
+
SET = 'set='
|
11
|
+
OPTIONS = {
|
12
|
+
set: {
|
13
|
+
classic: "#{SET}set1", human: "#{SET}set2", heads: "#{SET}set3", cats: "#{SET}set4", any: "#{SET}any"
|
14
|
+
},
|
15
|
+
size: {
|
16
|
+
small: "#{SIZE}100x100", medium: "#{SIZE}250x250", large: "#{SIZE}500x500", extra: "#{SIZE}900x900"
|
17
|
+
},
|
18
|
+
bgset: {
|
19
|
+
one: "#{BGSET}bg1", two: "#{BGSET}bg2"
|
20
|
+
}
|
21
|
+
}
|
19
22
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
+
def get(name, options = {})
|
24
|
+
valid_options = extract_valid_options(options)
|
25
|
+
query_string = build_query_string(valid_options)
|
26
|
+
make_request(name, query_string)
|
27
|
+
end
|
28
|
+
|
29
|
+
def get_many(names, options = {})
|
30
|
+
names.reject! { |name| !name.is_a?(String) || name == '' }
|
31
|
+
valid_options = extract_valid_options(options)
|
32
|
+
query_string = build_query_string(valid_options)
|
33
|
+
names.each { |name| make_request(name, query_string) }
|
23
34
|
end
|
24
|
-
names.each { |name| self.get(name, options) }
|
25
|
-
end
|
26
35
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
36
|
+
private
|
37
|
+
|
38
|
+
def extract_valid_options(options)
|
39
|
+
valid_options = []
|
40
|
+
|
41
|
+
if options.is_a?(Hash)
|
42
|
+
options.each_pair do |key, value|
|
43
|
+
inner_hash = OPTIONS.fetch(key, nil)
|
44
|
+
option = inner_hash.fetch(value, nil) if inner_hash.is_a?(Hash)
|
45
|
+
valid_options.push(option) if option
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
valid_options
|
50
|
+
end
|
31
51
|
|
32
|
-
|
33
|
-
|
34
|
-
|
52
|
+
def make_request(name, query_string)
|
53
|
+
uri = URI("#{BASE_URL}/#{name}#{query_string}")
|
54
|
+
response = Net::HTTP.get_response(uri)
|
55
|
+
|
56
|
+
if response.is_a?(Net::HTTPSuccess)
|
57
|
+
save(response, name)
|
58
|
+
puts "Image #{name}.png saved successfully!"
|
59
|
+
else
|
60
|
+
puts "Error obtaining image from #{uri}."
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def build_query_string(valid_options)
|
65
|
+
query_string = '?'
|
66
|
+
valid_options.each { |option| query_string += "#{option}&" }
|
67
|
+
query_string
|
68
|
+
end
|
69
|
+
|
70
|
+
def save(image, name)
|
71
|
+
Dir.mkdir(DEFAULT_DIRECTORY) unless Dir.exists? DEFAULT_DIRECTORY
|
72
|
+
|
73
|
+
open("robohash_images/#{name}.png", 'wb') do |file|
|
74
|
+
file.write(image.body)
|
75
|
+
end
|
76
|
+
end
|
35
77
|
end
|
36
|
-
end
|
78
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: robohash_client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Danilo Barion Nogueira
|
@@ -10,7 +10,7 @@ bindir: bin
|
|
10
10
|
cert_chain: []
|
11
11
|
date: 2017-11-04 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
|
-
description: A simple client
|
13
|
+
description: A simple client to get robohash avatar's from Robohash.org.
|
14
14
|
email: danilo.barion1986@live.com
|
15
15
|
executables: []
|
16
16
|
extensions: []
|
@@ -40,5 +40,5 @@ rubyforge_project:
|
|
40
40
|
rubygems_version: 2.6.13
|
41
41
|
signing_key:
|
42
42
|
specification_version: 4
|
43
|
-
summary:
|
43
|
+
summary: Client for Robohash.org
|
44
44
|
test_files: []
|