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

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Class Attribute Details

.access_tokenObject

Returns the value of attribute access_token.



6
7
8
# File 'lib/rapgenius/client.rb', line 6

def access_token
  @access_token
end

Instance Attribute Details

#text_formatObject (readonly)

Returns the value of attribute text_format.



20
21
22
# File 'lib/rapgenius/client.rb', line 20

def text_format
  @text_format
end

Instance Method Details

#documentObject



30
31
32
# File 'lib/rapgenius/client.rb', line 30

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

#fetch(url, params = {}) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/rapgenius/client.rb', line 34

def fetch(url, params = {})
  warn "[rapgenius] The RapGenius gem is now deprecated in favour of the more " \
       "powerful and more robust Genius gem. See https://github.com/timrogers/" \
       "genius for more details."

  unless RapGenius::Client.access_token
    raise MissingAccessTokenError, "You must specify an access token by setting " \
                                   "RapGenius::Client.access_token"
  end

  response = HTTPClient.get(url, query: {
    text_format: "#{DOM_TEXT_FORMAT},#{PLAIN_TEXT_FORMAT}"
  }.merge(params), headers: {
    'Authorization' => "Bearer #{RapGenius::Client.access_token}",
    'User-Agent' => "rapgenius.rb v#{RapGenius::VERSION}"
  })

  case response.code
  when 404
    raise RapGenius::NotFoundError
  when 401
    raise RapGenius::AuthenticationError
  when 200
    return response.parsed_response
  else
    raise RapGenius::Error, "Received a #{response.code} HTTP response"
  end
end

#url=(url) ⇒ Object



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

def url=(url)
  unless url =~ /^https?:\/\//
    @url = build_api_url(url)
  else
    @url = url
  end
end