Class: MetallicaLogo::Client

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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_urlObject (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_klassObject (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 (text, destination_file)
  result = (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 (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