Class: NextBigSoundLite::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/next_big_sound_lite/resource.rb

Direct Known Subclasses

Artist, Genre, Metric, Profile, Service

Class Method Summary collapse

Class Method Details

.get(resource) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/next_big_sound_lite/resource.rb', line 8

def self.get(resource)
  begin
    res = parse resource.get
    if block_given?
      yield res
    else
      res
    end
  # The API uses 400 Bad Request to indicate negative results
  # such as 'no results found'. 
  rescue RestClient::BadRequest => e
    false
  end      
end

.idfy(hash) ⇒ Object

Next Big Sound returns some results as hashes with keys equal to id of the object and values as the relevant data. This creates an array that includes the id within the object.



51
52
53
54
55
# File 'lib/next_big_sound_lite/resource.rb', line 51

def self.idfy(hash)
  hash.to_a.map do |id, data|
    data.dup.tap { |d| d.id = id }
  end
end

.parse(response) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/next_big_sound_lite/resource.rb', line 31

def self.parse(response)
  res = JSON.parse(response)
  case res
  when Array
    if res.first.is_a? Hash
      res.map { |hash| Hashie::Mash.new hash }
    else
      res
    end
  when Hash
    Hashie::Mash.new res
  else
    res
  end
end

.post(resource, params) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/next_big_sound_lite/resource.rb', line 23

def self.post(resource, params)
  begin
    parse resource.post(params)
  rescue RestClient::BadRequest => e
    false
  end          
end

.resource(name) ⇒ Object



4
5
6
# File 'lib/next_big_sound_lite/resource.rb', line 4

def self.resource(name)
  NextBigSoundLite.base[name]
end