Class: Discog

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

Constant Summary collapse

API_KEY =
'd63ff5c461'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rawartist) ⇒ Discog

Returns a new instance of Discog.



10
11
12
13
14
15
16
17
# File 'lib/discog.rb', line 10

def initialize(rawartist)
  artist = rawartist.downcase.gsub(" ", "+")
  @xml = get_xml(artist)
  @name = get_name(artist)
  @realname = get_realname(artist)
  @members = get_members(artist)
  @profile = get_profile(artist)
end

Instance Attribute Details

#membersObject (readonly)

Returns the value of attribute members.



8
9
10
# File 'lib/discog.rb', line 8

def members
  @members
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/discog.rb', line 8

def name
  @name
end

#profileObject (readonly)

Returns the value of attribute profile.



8
9
10
# File 'lib/discog.rb', line 8

def profile
  @profile
end

#realnameObject (readonly)

Returns the value of attribute realname.



8
9
10
# File 'lib/discog.rb', line 8

def realname
  @realname
end

Instance Method Details

#get_members(artist) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/discog.rb', line 40

def get_members(artist)
  members = []
  @xml.xpath("/resp/artist/members/name").each do |n|
    members << n.inner_text
  end
  return members
end

#get_name(artist) ⇒ Object



32
33
34
# File 'lib/discog.rb', line 32

def get_name(artist)
  @xml.xpath("/resp/artist/name").inner_text
end

#get_profile(artist) ⇒ Object



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

def get_profile(artist)
  @xml.xpath("/resp/artist/profile").inner_text
end

#get_realname(artist) ⇒ Object



36
37
38
# File 'lib/discog.rb', line 36

def get_realname(artist)
  @xml.xpath("/resp/artist/realname").inner_text
end

#get_xml(artist) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/discog.rb', line 19

def get_xml(artist)
  begin
    rawfile = open("http://www.discogs.com/artist/#{artist}?f=xml&api_key=#{API_KEY}", 'Accept-encoding' => 'gzip, deflate')
  rescue OpenURI::HTTPError
    return Nokogiri::XML("")
  end
  begin
    return Nokogiri::XML(Zlib::GzipReader.new(rawfile).read)
  rescue Zlib::GzipFile::Error
    return Nokogiri::XML(open("http://www.discogs.com/artist/#{artist}?f=xml&api_key=#{API_KEY}"))
  end
end