Class: Freesound::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/freesound/response.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw, form = :json) ⇒ Response

Returns a new instance of Response.



5
6
7
8
9
# File 'lib/freesound/response.rb', line 5

def initialize(raw, form=:json)
  @content = raw
  @format  = form
  @parser  = ResponseParser.new(form)
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



3
4
5
# File 'lib/freesound/response.rb', line 3

def content
  @content
end

#parserObject (readonly)

Returns the value of attribute parser.



3
4
5
# File 'lib/freesound/response.rb', line 3

def parser
  @parser
end

Instance Method Details

#dataObject



11
12
13
# File 'lib/freesound/response.rb', line 11

def data
  @data ||= @parser.parse(@content)
end

#errorsObject



15
16
17
# File 'lib/freesound/response.rb', line 15

def errors
  @errors ||= (self.data[:error] rescue false) ? self.data : {}
end

#num_resultsObject



19
20
21
# File 'lib/freesound/response.rb', line 19

def num_results
  self.data.is_a?(Array) ? self.data.size : 1
end

#soundsObject



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/freesound/response.rb', line 23

def sounds
  return [] if self.errors[:error]

  @sounds ||= if num_results == 1
                [ Sound.new(*self.data.values) ]
              else
                self.data.map do |sound|
                  result = Sound.new
                  sound.each { |k, v| result.send("#{k}=", v) }
                  result
                end
              end
end