Class: MetallicaLogo::Client
- Inherits:
-
Object
- Object
- MetallicaLogo::Client
- Defined in:
- lib/metallica_logo/client.rb
Overview
HTTP client for requesting logo generation and also if desired, downloading to a local destination
Constant Summary collapse
- MAX_CHARACTERS =
25
Instance Attribute Summary collapse
-
#base_url ⇒ Object
readonly
Returns the value of attribute base_url.
-
#rest_klass ⇒ Object
readonly
Returns the value of attribute rest_klass.
Instance Method Summary collapse
- #generate_and_download_logo(text, destination_file) ⇒ Object
- #generate_logo(text) ⇒ Object
-
#initialize(base_url: MetallicaLogo::BASE_URL, rest_klass: RestClient) ⇒ Client
constructor
A new instance of Client.
Constructor Details
#initialize(base_url: MetallicaLogo::BASE_URL, rest_klass: RestClient) ⇒ Client
Returns a new instance of Client.
11 12 13 14 |
# File 'lib/metallica_logo/client.rb', line 11 def initialize(base_url: MetallicaLogo::BASE_URL, rest_klass: RestClient) @rest_klass = rest_klass @base_url = base_url end |
Instance Attribute Details
#base_url ⇒ Object (readonly)
Returns the value of attribute base_url.
7 8 9 |
# File 'lib/metallica_logo/client.rb', line 7 def base_url @base_url end |
#rest_klass ⇒ Object (readonly)
Returns the value of attribute rest_klass.
7 8 9 |
# File 'lib/metallica_logo/client.rb', line 7 def rest_klass @rest_klass end |
Instance Method Details
#generate_and_download_logo(text, destination_file) ⇒ Object
25 26 27 28 29 30 31 32 |
# File 'lib/metallica_logo/client.rb', line 25 def generate_and_download_logo(text, destination_file) result = generate_logo(text) destination_file = Pathname(destination_file) destination_file.dirname.mkpath response = download(result.file) IO.binwrite(destination_file, response.body) destination_file end |
#generate_logo(text) ⇒ Object
16 17 18 19 20 21 22 23 |
# File 'lib/metallica_logo/client.rb', line 16 def generate_logo(text) if text.size > MAX_CHARACTERS raise RequestError, "Too many characters, limit is #{MAX_CHARACTERS}" end response = post("#{@base_url}/creation/", 'the_text': text) MetallicaLogo::Result.new(JSON.parse(response)) end |