robohash_client 0.0.3 → 0.1.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b682fec95cae8ea1d5e02b5cb5c5123987991939
4
- data.tar.gz: 384b0c778ee03fa48a7978996339007e5c8419ae
3
+ metadata.gz: 3a7971f766726e72e7878c2f9b636c40a9bc8567
4
+ data.tar.gz: bb4273e81a7c9504085eae45f943bbb052f0eaf0
5
5
  SHA512:
6
- metadata.gz: 39077a6b2d276813133c23e6e80dfb4a2d8d0c9a4edd05fe56992dbd1fa50bd79f3db54c8fecaedcca8fa37432390bcd2d18196161677069dca056f693f3af33
7
- data.tar.gz: 057e7c00cf58fb452d348c9755b2cf0c07f3a59e8bd25255ad33720b51b4c9d1e0146682a277a3a2557cc63ba442fe229c213cf5b85e22e07ac9caba01d778a7
6
+ metadata.gz: a8f792be0f76f0f315a05df454138d756945953e2cee71e6a3b43aa1d1643602b9520e7706c62cf49addc1af8d2d79e7ba9aec361b1a907c438a68a3e99eb8a5
7
+ data.tar.gz: 02ee3a2bd21afeacba561fb280dc189d515dbf9ab805384691249d2fc529114909b5bef139de75614c3b5b09dbc343414655f010b5a60c4962f17be31c0fb11b
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'robohash_client'
4
+
5
+ random_name = [*('a'..'Z')].sample(8).join
6
+ RobohashClient.get(random_name)
@@ -1,39 +1,66 @@
1
1
  require 'net/http'
2
2
  require 'json'
3
3
 
4
- class Robohash
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
- }
4
+ class RobohashClient
5
+ BASE_URL = 'https://robohash.org'
6
+ DEFAULT_DIRECTORY = 'robohash_images'
7
+ BGSET = 'bgset='
8
+ SIZE = 'size='
9
+ SET = 'set='
10
+ OPTIONS = {
11
+ set: {
12
+ classic: "#{SET}set1", human: "#{SET}set2", heads: "#{SET}set3", cats: "#{SET}set4", any: "#{SET}any"
13
+ },
14
+ size: {
15
+ small: "#{SIZE}100x100", medium: "#{SIZE}250x250", large: "#{SIZE}500x500", extra: "#{SIZE}900x900"
16
+ },
17
+ bgset: {
18
+ one: "#{BGSET}bg1", two: "#{BGSET}bg2"
21
19
  }
20
+ }
22
21
 
22
+ class << self
23
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)
24
+ return 'Name should be an non-empty String!' if invalid_name(name)
25
+ make_request(name, build_query_string(options))
26
+ end
27
+
28
+ def get_url(name, options = {})
29
+ return 'Name should be an non-empty String!' if invalid_name(name)
30
+ build_uri(name, build_query_string(options)).to_s
27
31
  end
28
32
 
29
33
  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) }
34
+ valid_names = extract_valid_names(names)
35
+ valid_options = build_query_string(options) unless valid_names.empty?
36
+ valid_names.each { |name| make_request(name, valid_options) }
34
37
  end
35
38
 
36
- private
39
+ def get_many_url(names, options = {})
40
+ urls = []
41
+ valid_names = extract_valid_names(names)
42
+ valid_options = build_query_string(options) unless valid_names.empty?
43
+ valid_names.each { |name| urls.push(build_uri(name, valid_options).to_s) }
44
+ urls
45
+ end
46
+
47
+ private
48
+
49
+ def extract_valid_names(names)
50
+ return [] unless names.is_a?(Array)
51
+ names.reject { |name| invalid_name(name) }
52
+ end
53
+
54
+ def invalid_name(name)
55
+ !name.is_a?(String) || name == ''
56
+ end
57
+
58
+ def build_query_string(options)
59
+ valid_options = extract_valid_options(options)
60
+ return if valid_options.empty?
61
+
62
+ '?' + valid_options.collect { |option| option }.join('&')
63
+ end
37
64
 
38
65
  def extract_valid_options(options)
39
66
  valid_options = []
@@ -49,22 +76,16 @@ class Robohash
49
76
  valid_options
50
77
  end
51
78
 
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
79
+ def build_uri(name, query_string)
80
+ URI("#{BASE_URL}/#{name}#{query_string}")
62
81
  end
63
82
 
64
- def build_query_string(valid_options)
65
- query_string = '?'
66
- valid_options.each { |option| query_string += "#{option}&" }
67
- query_string
83
+ def make_request(name, query_string)
84
+ uri = build_uri(name, query_string)
85
+ response = Net::HTTP.get_response(uri)
86
+ save(response, name)
87
+ rescue => e
88
+ puts "Error obtaining image from #{uri}: #{e.message}"
68
89
  end
69
90
 
70
91
  def save(image, name)
@@ -73,6 +94,10 @@ class Robohash
73
94
  open("robohash_images/#{name}.png", 'wb') do |file|
74
95
  file.write(image.body)
75
96
  end
97
+
98
+ puts "Image #{name}.png saved successfully!"
99
+ rescue => e
100
+ puts "Error saving image #{name}.png: #{e.message}"
76
101
  end
77
102
  end
78
103
  end
metadata CHANGED
@@ -1,27 +1,47 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: robohash_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Danilo Barion Nogueira
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-11-04 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2017-11-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3'
13
27
  description: A simple client to get robohash avatar's from Robohash.org.
14
28
  email: danilo.barion1986@live.com
15
- executables: []
29
+ executables:
30
+ - robohash-client
16
31
  extensions: []
17
32
  extra_rdoc_files: []
18
33
  files:
34
+ - bin/robohash-client
19
35
  - lib/robohash_client.rb
20
36
  homepage: http://rubygems.org/gems/robohash_client
21
37
  licenses:
22
38
  - MIT
23
- metadata: {}
24
- post_install_message:
39
+ metadata:
40
+ homepage_uri: https://github.com/danilobarion1986/robohash_client
41
+ documentation_uri: https://github.com/danilobarion1986/robohash_client/blob/master/README.md
42
+ source_code_uri: https://github.com/danilobarion1986/robohash_client
43
+ bug_tracker_uri: https://github.com/danilobarion1986/robohash_client/issues
44
+ post_install_message: "=========================\n\n WE ARE THE ROBOTS! \n\n========================="
25
45
  rdoc_options: []
26
46
  require_paths:
27
47
  - lib
@@ -29,7 +49,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
29
49
  requirements:
30
50
  - - ">="
31
51
  - !ruby/object:Gem::Version
32
- version: '0'
52
+ version: '1.9'
33
53
  required_rubygems_version: !ruby/object:Gem::Requirement
34
54
  requirements:
35
55
  - - ">="