Class: ItunesIcon

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

Instance Method Summary collapse

Instance Method Details

#download(url, path) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/TokiServer/itunesicon.rb', line 47

def download url, path
  begin
    open(url) do |f|
      File.open(path,'w+') do |file|
        file.puts f.read
      end
      puts "#{path}"
    end
  rescue
    puts "Error: failed to save icon."
  end
end

#find_icon(terms) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/TokiServer/itunesicon.rb', line 14

def find_icon terms
  url = URI.parse("http://itunes.apple.com/search?term=#{CGI.escape(terms)}&entity=#{@entity}")
  res = Net::HTTP.get_response(url).body
  match = res.match(/"#{@icon_size}":"(.*?)",/)
  unless match.nil?
    return match[1]
  else
    return false
  end
end

#grab(*args) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/TokiServer/itunesicon.rb', line 37

def grab *args
  terms = args.join(" ")
  icon_url = find_icon terms
  if icon_url
    URI.parse(icon_url)
  else
    ''
  end
end

#grab_big(*args) ⇒ Object



31
32
33
34
35
# File 'lib/TokiServer/itunesicon.rb', line 31

def grab_big *args
  @entity = "macSoftware"
  @icon_size = "artworkUrl100"
  grab args
end

#grab_small(*args) ⇒ Object



25
26
27
28
29
# File 'lib/TokiServer/itunesicon.rb', line 25

def grab_small *args
  @entity = "macSoftware"
  @icon_size = "artworkUrl60"
  grab args
end