Class: Beats::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/beats/client.rb

Constant Summary collapse

DEFAULT_OPTIONS =
{
  :base_url => ENV['BEATS_URL'] || 'http://prdaisy.com/'
}

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Creates a client object that can be used to interact with the Beats API

Parameters:

  • options (Hash) (defaults to: {})

    Configuration options for the client

Options Hash (options):

  • :base_url (String)

    The URL the client will use to access the API. Defaults to http://prdaisy.com/ or the BEATS_URL environment variable if it is set.

  • :access_token (String)

    The access token to use for requests that require authentication.



21
22
23
24
25
# File 'lib/beats/client.rb', line 21

def initialize(options = {})
  options = DEFAULT_OPTIONS.merge(options)
  @base_url = options[:base_url]
  @access_token = options[:access_token]
end

Instance Method Details

#accountHash

Retrieves the representation of the currently authenticated user. Requires authentication.

Returns:

  • (Hash)

    A hash with info about the current user.



41
42
43
# File 'lib/beats/client.rb', line 41

def 
  decode_json(get(resolve_uri(@base_url, href(:account))))
end

#add_history(uri) ⇒ Object

Adds a resource (e.g. a track) to the currently authenticated users history. Requires authentication.

Parameters:

  • uri (String)

    The relative URI for a resource (usually a track).



71
72
73
74
# File 'lib/beats/client.rb', line 71

def add_history(uri)
  post(resolve_uri(@base_url, href(:history)),
    :uri => uri)
end

#add_release(release) ⇒ Hash

Adds a release to the catalogue.

Parameters:

  • release (Hash)

    Metadata describing the release.

Returns:

  • (Hash)

    A hash describing the added release.



55
56
57
# File 'lib/beats/client.rb', line 55

def add_release(release)
  decode_json(post(resolve_uri(@base_url, href(:catalogue)), release))
end

#indexHash

Retrieves the resource discovery index. The index is a set of links to other resources.

Returns:

  • (Hash)

    A hash with a "links" field (an array of hashes representing the links).



31
32
33
34
35
36
# File 'lib/beats/client.rb', line 31

def index
  return @index if @index
  get(@base_url)
rescue RestClient::MultipleChoices => e
  @index = decode_json(e.response.body)
end

#resource(uri) ⇒ Hash

Retrieves the representation of a resource.

Parameters:

  • uri (String)

    A relative or absolute URI that names the resource.

Returns:

  • (Hash)

    A hash with at least the fields "uri", "name" and "type".



48
49
50
# File 'lib/beats/client.rb', line 48

def resource(uri)
  decode_json(get(resolve_uri(@base_url, uri)))
end

#search(query) ⇒ Hash

Performs a search in the music catalogue and returns the results.

Parameters:

  • query (String)

    A search query, like "the beatles".

Returns:

  • (Hash)

    A hash with array fields for the different kinds of resource types found.



63
64
65
66
# File 'lib/beats/client.rb', line 63

def search(query)
  decode_json(get(resolve_uri(@base_url, href(:search)),
    :params => { :q => query }))
end

#sentenceHash

Retrieves the building blocks for "The Sentence".

Returns:

  • (Hash)

    The bulding blocks for the sentence.



78
79
80
# File 'lib/beats/client.rb', line 78

def sentence
  decode_json(get(resolve_uri(@base_url, href(:sentence))))
end

#stream(uri) ⇒ Object



82
83
84
85
86
87
88
89
90
91
# File 'lib/beats/client.rb', line 82

def stream(uri)
  url = "#{@base_url}#{uri}/stream"
  RestClient.get(url) do |response, request, result, &block|
    if response.code == 302
      return response.headers[:location]
    else
      return nil
    end 
  end
end