Class: BillboardAPI

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

Constant Summary collapse

@@cache_frequency =
86400
@@artist_100_cache =
{}
@@artist_100_cache_time =
0
@@hot_100_cache =
{}
@@hot_100_cache_time =
0
@@billboard_200_cache =
{}
@@billboard_200_cache_time =
0

Class Method Summary collapse

Class Method Details

.artist_100(cache = false) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/billboard_api.rb', line 13

def self.artist_100(cache=false)
    if cache && !(Time.now.to_i > @@artist_100_cache_time + @@cache_frequency)
        return @@artist_100_cache
    end
    @@artist_100_cache_time = Time.now.to_i
    document = Nokogiri::HTML.parse(open(ARTIST_100_URL))
    artists = document.css(".chart-list-item")
    @@artist_100_cache = artists.map do |artist|
        new_artist = create_artist_object(
            artist.attribute("data-title").value, 
            if artist.css(".chart-list-item__image-wrapper").css("img").length > 1
                if artist.css(".chart-list-item__image-wrapper").css("img")[1].attributes.has_key?("data-src")
                    artist.css(".chart-list-item__image-wrapper").css("img")[1].attribute("data-src").value
                else
                    artist.css(".chart-list-item__image-wrapper").css("img")[1].attribute("src").value
                end
            else
                artist.css(".chart-list-item__image-wrapper").css("img").attribute("data-src").value
            end
        )

    end
end

.billboard_200(cache = false) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/billboard_api.rb', line 61

def self.billboard_200(cache=false)
    if cache && !(Time.now.to_i > @@billboard_200_cache_time + @@cache_frequency)
        return @@billboard_200_cache
    end
    @@billboard_200_cache_time = Time.now.to_i
    document = Nokogiri::HTML.parse(open(BILLBOARD_200_URL))
    songs = document.css(".chart-list__element")
    @@billboard_200_cache = songs.map do |song|
        new_song = create_song_object(
            song.css(".chart-element__information__song").text, 
            song.css(".chart-element__information__artist").text, 
            if song.css(".chart-element__image").attribute("style").value == "" #Empty style attribute catch
                ""
            else
                song.css(".chart-element__image").attribute("style").value.split("('")[1].split("')")[0]
            end
        )
    end
end

.cache_frequencyObject



96
97
98
# File 'lib/billboard_api.rb', line 96

def self.cache_frequency
    @@cache_frequency
end

.cache_frequency=(cache_frequency) ⇒ Object



100
101
102
# File 'lib/billboard_api.rb', line 100

def self.cache_frequency=(cache_frequency)
    @@cache_frequency = cache_frequency
end

.create_artist_object(name, img_url) ⇒ Object



81
82
83
84
85
86
# File 'lib/billboard_api.rb', line 81

def self.create_artist_object(name, img_url)
    {
        name: name,
        img_url: img_url
    }
end

.create_song_object(name, artist, img_url) ⇒ Object



88
89
90
91
92
93
94
# File 'lib/billboard_api.rb', line 88

def self.create_song_object(name, artist, img_url)
    {
        name: name,
        artist: artist,
        img_url: img_url
    }
end

.hot_100(cache = false) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/billboard_api.rb', line 39

def self.hot_100(cache=false)
    if cache && !(Time.now.to_i > @@hot_100_cache_time + @@cache_frequency)
        return @@hot_100_cache
    end
    @@hot_100_cache_time = Time.now.to_i
    document = Nokogiri::HTML.parse(open(HOT_100_URL))
    songs = document.css(".chart-list__element")
    @@hot_100_cache = songs.map do |song|
        new_song = create_song_object(
            song.css(".chart-element__information__song").text, 
            song.css(".chart-element__information__artist").text, 
            if song.css(".chart-element__image").attribute("style").value == "" #Empty style attribute catch
                ""
            else
                song.css(".chart-element__image").attribute("style").value.split("('")[1].split("')")[0]
            end
        )
    end
end