Module: RapGenius::Client

Included in:
RapGenius, Artist, Line, Song
Defined in:
lib/rapgenius/client.rb

Defined Under Namespace

Classes: HTTPClient

Constant Summary collapse

BASE_URL =
HTTPClient.base_uri + "/".freeze
PLAIN_TEXT_FORMAT =
"plain".freeze
DOM_TEXT_FORMAT =
"dom".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#text_formatObject (readonly)

Returns the value of attribute text_format.



17
18
19
# File 'lib/rapgenius/client.rb', line 17

def text_format
  @text_format
end

Instance Method Details

#documentObject



27
28
29
# File 'lib/rapgenius/client.rb', line 27

def document
  @document ||= fetch(@url)
end

#fetch(url) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/rapgenius/client.rb', line 31

def fetch(url)
  response = HTTPClient.get(url, query: {
    text_format: "#{DOM_TEXT_FORMAT},#{PLAIN_TEXT_FORMAT}"
  })

  if response.code != 200
    if response.code == 404
      raise RapGenius::NotFoundError
    else
      raise RapGenius::Error, "Received a #{response.code} HTTP response"
    end
  end

  response.parsed_response
end

#url=(url) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/rapgenius/client.rb', line 19

def url=(url)
  unless url =~ /^https?:\/\//
    @url = BASE_URL + url.gsub(/^\//, '')
  else
    @url = url
  end
end