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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/robohash_client.rb +69 -27
  3. metadata +3 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b672467cd0e2035a9a8cf6e26210c03be0d2433f
4
- data.tar.gz: a0f3583e7eddeb2d48b8de4ab064d751e29e23ac
3
+ metadata.gz: b682fec95cae8ea1d5e02b5cb5c5123987991939
4
+ data.tar.gz: 384b0c778ee03fa48a7978996339007e5c8419ae
5
5
  SHA512:
6
- metadata.gz: c11c421a3fccdff22b8771aa412421ffaac447bd86028ebb6b03601dd6f780a9f64c72ec33761699b56741b8a22fc770a54644295f48df098fcb08fdbdabb12d
7
- data.tar.gz: cc8b0f7e27cea4bfebdd2273fec41dc8e1cb6c12f09e968aeb80c5891c88a6950bdbba1b041d6b251b87c5d84b1f4ddea4db2c1721d872920c574186f4e0adff
6
+ metadata.gz: 39077a6b2d276813133c23e6e80dfb4a2d8d0c9a4edd05fe56992dbd1fa50bd79f3db54c8fecaedcca8fa37432390bcd2d18196161677069dca056f693f3af33
7
+ data.tar.gz: 057e7c00cf58fb452d348c9755b2cf0c07f3a59e8bd25255ad33720b51b4c9d1e0146682a277a3a2557cc63ba442fe229c213cf5b85e22e07ac9caba01d778a7
@@ -2,35 +2,77 @@ require 'net/http'
2
2
  require 'json'
3
3
 
4
4
  class Robohash
5
- BASE_URL = 'https://robohash.org'
6
- DEFAULT_DIRECTORY = 'robohash_images'
7
-
8
- def self.get(name, options = '')
9
- uri = URI("#{BASE_URL}/#{name}.png?#{options}")
10
- response = Net::HTTP.get_response(uri)
11
-
12
- if response.is_a?(Net::HTTPSuccess)
13
- save(response, name)
14
- puts "Image #{name}.png saved with success!"
15
- else
16
- puts "Error obtaining image from #{uri}."
17
- end
18
- end
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
- def self.get_many(names, options = '')
21
- names.reject! do |name|
22
- !name.is_a?(String) || name == ''
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
- private
28
-
29
- def self.save(image, name)
30
- Dir.mkdir(DEFAULT_DIRECTORY) unless Dir.exists? DEFAULT_DIRECTORY
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
- open("robohash_images/#{name}.png", 'wb') do |file|
33
- file.write(image.body)
34
- end
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.2
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 for fetch robohash avatars from Robohash.org
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: CLient for Robohash.org
43
+ summary: Client for Robohash.org
44
44
  test_files: []