Module: Genius::Search

Defined in:
lib/genius/api/search.rb

Overview

Genius::Search module provides methods to work with Genius search database

Class Method Summary collapse

Class Method Details

.search(token: nil, query: nil, search_by: nil) ⇒ String, ...

Genius::Search.search -> NilClass

@todo: refactor example group in method documentation This method is a standard Genius API method and it is needed to send a request to the server and get information about artists, tracks and everything else that may be inside the response body. According to docs.genius.com/#search-h2, token is required to be in response, but you can use this method without use of it! =)

Also, you can use this method with search_by param, which is needed to search interested data through returned JSON. It uses deep_find extension under the hood.

Examples:

Genius::Search.search(query: "Ariana Grande") #=> {..., "full_title"=>" thank u, next by Ariana Grande", ...}
Genius::Search.search(query: "Bones", search_by: "title") #=> ["Dirt", "HDMI", "RestInPeace", "Sodium"]

Parameters:

  • token (String) (defaults to: nil)

    Token to access api.genius.com.

  • query (String) (defaults to: nil)

    Search query.

  • search_by (Object) (defaults to: nil)

    Optional parameter to search by key in output JSON.

Returns:

  • (String)

    if search_by is TrueClass.

  • (Hash)

    if search_by is FalseClass.

  • (NilClass)

    if TokenError exception raised.

Raises:

  • (ArgumentError)

    if query got incorrect value.

  • (TokenError)

    if token or Genius::Auth.token are invalid.

See Also:

  • #deep_find


37
38
39
40
41
42
43
44
45
# File 'lib/genius/api/search.rb', line 37

def search(token: nil, query: nil, search_by: nil)
  return if token.nil? && !Auth.authorized?.nil?

  Errors.validate_token(token) unless token.nil?

  response = HTTParty.get("#{Api::RESOURCE}/search?q=#{query}&access_token=#{token_ext(token)}").body
  search = JSON.parse(response)
  search_by ? search.deep_find(search_by) : search
end