Class: TunecoreDirect::Response

Inherits:
Base
  • Object
show all
Includes:
TunecoreDirect
Defined in:
lib/tunecore_direct/response.rb

Overview

Represents a response from the TC web service. This class is designed to take a response from any method call in the API and create a SDK object out of it. It will look at the root XML node in the body of the HTTP response to determine the type of #object to return.

When a response comes back from the TuneCore web service it is processed with this class. The object being returned is extracted with the Response#object method. You shouldn’t have to use this class directly, as convenience methods are provided in the Person, Album, and Song classes.

Any API object errors will be set in the errors accessor and the object type will be set in the type accessor.

Instance Attribute Summary collapse

Attributes inherited from Base

#log

Instance Method Summary collapse

Methods inherited from Base

api_key, #api_key, api_key=, tunecore_server, #tunecore_server, tunecore_server=

Constructor Details

#initialize(body) ⇒ Response

Accepts the body of the HTTP response from the TC web service.



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/tunecore_direct/response.rb', line 16

def initialize( body )
  @body = body
  @doc = REXML::Document.new( body )
  @type = @doc.root.name
  @status = @doc.root.elements["/*/status"].text

  if @status == "error"
    #collect the error messages and put them in an array of hashes
    @errors = @doc.root.elements.collect("/*/error"){|e| {:attribute => e.elements["attribute"].text, :message => e.elements["message"].text} }
  end
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



13
14
15
# File 'lib/tunecore_direct/response.rb', line 13

def errors
  @errors
end

#statusObject (readonly)

Returns the value of attribute status.



13
14
15
# File 'lib/tunecore_direct/response.rb', line 13

def status
  @status
end

#typeObject (readonly)

Returns the value of attribute type.



13
14
15
# File 'lib/tunecore_direct/response.rb', line 13

def type
  @type
end

Instance Method Details

#objectObject

Returns the appropriate object based on the complete response XML we got from the web service. The object returned will be one of the following, based on what type of request you made:

  • Person

  • Album

  • Song

  • Array of people

  • Array of albums

  • Array of songs



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
62
63
64
65
66
67
68
69
70
71
# File 'lib/tunecore_direct/response.rb', line 36

def object
  case @type
  when "person"
    person_element = @doc.root.elements["/person"]
    person = Person.from_xml_element( person_element)
    return person
  when "people"
    people = []
    @doc.root.elements.each("/people/person") do |person_element|
      people << Person.from_xml_element( person_element )
    end
    return people
  when "album"
    album_element = @doc.root.elements["/album"]
    album = Album.from_xml_element( album_element)
    return album
  when "albums"
    albums = []
    @doc.root.elements.each("/albums/album") do |album_element|
      albums << Album.from_xml_element( album_element )
    end
    return albums
  when "song"
    song_element = @doc.root.elements["/song"]
    song = Song.from_xml_element( song_element )
    return song
  when "songs"
    albums = []
    @doc.root.elements.each("/songs/song") do |song_element|
      songs << Song.from_xml_element( song_element )
    end
    return songs
  else
    raise "Unsupported response type: #{@type}"
  end
end

#to_xmlObject



73
74
75
# File 'lib/tunecore_direct/response.rb', line 73

def to_xml
  @body
end