Sha256: d9271d8b9d2da77fd2c7e5661ab4245a21144ef1624beb9ae24b902299c25c2e
Contents?: true
Size: 1.68 KB
Versions: 1
Compression:
Stored size: 1.68 KB
Contents
require 'httparty' module RapGenius module Client class << self attr_accessor :access_token end class HTTPClient include HTTParty format :json base_uri 'https://api.rapgenius.com' end BASE_URL = HTTPClient.base_uri + "/".freeze PLAIN_TEXT_FORMAT = "plain".freeze DOM_TEXT_FORMAT = "dom".freeze attr_reader :text_format def url=(url) unless url =~ /^https?:\/\// @url = build_api_url(url) else @url = url end end def document @document ||= fetch(@url) end 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 private def build_api_url(path) BASE_URL + path.gsub(/^\//, '') end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
rapgenius-1.1.2 | lib/rapgenius/client.rb |