Class: MusicBrainz::Client

Inherits:
Object
  • Object
show all
Includes:
MusicBrainz::ClientModules::CachingProxy, MusicBrainz::ClientModules::FailsafeProxy, MusicBrainz::ClientModules::TransparentProxy
Defined in:
lib/musicbrainz/client.rb

Instance Method Summary collapse

Methods included from MusicBrainz::ClientModules::CachingProxy

#cache_path, #caching?, #clear_cache, #get_contents

Methods included from MusicBrainz::ClientModules::FailsafeProxy

#failsafe?, #get_contents, #ready?, #time_passed, #time_to_wait, #wait_util_ready!

Methods included from MusicBrainz::ClientModules::TransparentProxy

#get_contents

Instance Method Details

#httpObject


8
9
10
11
12
13
14
# File 'lib/musicbrainz/client.rb', line 8

def http
  @faraday ||= Faraday.new do |f|
    f.request :url_encoded            # form-encode POST params
    f.adapter Faraday.default_adapter # make requests with Net::HTTP
    f.use     MusicBrainz::Middleware # run requests with correct headers
  end
end

#load(resource, query, params) ⇒ Object

Raises:

  • (Exception)

16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/musicbrainz/client.rb', line 16

def load(resource, query, params)
  raise Exception.new("You need to run MusicBrainz.configure before querying") if MusicBrainz.config.nil?

  url = build_url(resource, query)
  response = get_contents(url)

  return nil if response[:status] != 200

  xml = Nokogiri::XML.parse(response[:body]).remove_namespaces!.xpath('/metadata')
  data = binding_class_for(params[:binding]).parse(xml)

  if params[:create_model]
    model_class_for(params[:create_model]).new(data)
  elsif params[:create_models]
    models = data.map{ |item| model_class_for(params[:create_models]).new(item) }
    models.sort!{ |a, b| a.send(params[:sort]) <=> b.send(params[:sort]) } if params[:sort]
    models
  else
    data
  end
end