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

Instance Method Summary collapse

Instance Method Details

#documentObject



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

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

#fetch(url) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'lib/rapgenius/client.rb', line 32

def fetch(url)
  response = HTTPClient.get(url)

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

  response.parsed_response
end

#parse_description(node) ⇒ Object

Descriptions are formatted in an irritating way, encapsulating the various kinds of HTML tag that can be included. This parses that into text, but some content may be lost.



45
46
47
48
49
50
51
52
53
54
# File 'lib/rapgenius/client.rb', line 45

def parse_description(node)
  if node.is_a? String
    node
  elsif node.is_a? Array
    node.map { |subnode| parse_description(subnode) }
  elsif node.is_a? Hash
    return unless node.key? "children"
    parse_description(node["children"])
  end
end

#url=(url) ⇒ Object



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

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