Module: BlipFm

Defined in:
lib/blipfm.rb

Defined Under Namespace

Classes: Blip, Song, User

Constant Summary collapse

CONFIG =
YAML.load_file('config.yml')

Class Method Summary collapse

Class Method Details

.add_blip_to_playlist(user_name, user_password, blip_id) ⇒ Object



124
125
126
# File 'lib/blipfm.rb', line 124

def BlipFm.add_blip_to_playlist(user_name, user_password, blip_id)
  return execute_blip_method('favorite/addToPlaylist', true, true, {'blipId' => blip_id}, true, user_name, user_password, 'POST')
end

.add_dj(user_name, user_password, dj_user_name) ⇒ Object



120
121
122
# File 'lib/blipfm.rb', line 120

def BlipFm.add_dj(user_name, user_password, dj_user_name)
  return execute_blip_method('favorite/addDJ', true, true, {'username' => dj_user_name}, true, user_name, user_password, 'POST')
end

.blip_signature(blip_method, blip_timestamp, blip_nonce) ⇒ Object



26
27
28
29
# File 'lib/blipfm.rb', line 26

def BlipFm.blip_signature(blip_method, blip_timestamp, blip_nonce)
  blip_data = blip_method + "\n" + blip_timestamp.to_s + "\n" + blip_nonce
  CGI.escape(Base64.encode64(HMAC::SHA1.digest(CONFIG['blip_secret_key'], blip_data)).strip)
end

.create_blip_array(blip_xml, object_type = 'Blip') ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/blipfm.rb', line 72

def BlipFm.create_blip_array(blip_xml, object_type = 'Blip')
  blip_array = []
  blip_collection_xml = REXML::Document.new(blip_xml).elements['BlipApiResponse/result/collection']
  blip_collection_xml.elements.each(object_type) do |current_blip_xml|
    case (object_type)
      when 'Song'
        blip_array << Song.create_by_xml(current_blip_xml)
      when 'user', 'User'
        blip_array << User.create_by_xml(current_blip_xml)
      else
        blip_array << Blip.create_by_xml(current_blip_xml)
    end
  end
  return blip_array
end

.create_user(user_name, user_email, user_password) ⇒ Object



172
173
174
# File 'lib/blipfm.rb', line 172

def BlipFm.create_user(user_name, user_email, user_password)
  return execute_blip_method('user/signup', true, true, {'username' => user_name, 'emailAddress' => user_email, 'password' => user_password}, false, nil, nil, 'POST')
end

.delete_blip(user_name, user_password, blip_id) ⇒ Object



88
89
90
# File 'lib/blipfm.rb', line 88

def BlipFm.delete_blip(user_name, user_password, blip_id)
  return execute_blip_method('blip/delete', true, true, {'id' => blip_id}, true, user_name, user_password, 'POST')
end

.execute_blip_method(blip_method, require_key, require_auth, parameter_hash = nil, require_user_auth = false, blip_user_name = '', blip_user_password = '', request_type = 'GET') ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/blipfm.rb', line 31

def BlipFm.execute_blip_method(blip_method, require_key, require_auth, parameter_hash = nil, require_user_auth = false, blip_user_name = '', blip_user_password = '', request_type = 'GET')
  # INIT URL
  blip_url = '/' + blip_method + '.xml?a=1'
  if (require_key)
    blip_url += '&apiKey=' + CONFIG['blip_api_key']
  end
  
  # SIGN REQUEST
  if (require_auth)
    blip_nonce = generate_blip_nonce()
    blip_timestamp = get_blip_timestamp()
    blip_url += '&nonce=' + blip_nonce
    blip_url += '&timestamp=' + blip_timestamp
    blip_url += '&signature=' + blip_signature(request_type, blip_timestamp, blip_nonce)
  end
  
  # ADD PARAMETERS TO URL IF GET
  if ((!parameter_hash.nil?) && (request_type == 'GET'))
    parameter_hash.each do |blip_parameter, blip_value|
      blip_url += '&' + blip_parameter + '=' + CGI.escape(blip_value.to_s)
    end
  end

  # PROCESS REQUEST
  Net::HTTP.start(CONFIG['blip_base_url']) {|http|
    request = nil
    if (request_type == 'GET')
      request = Net::HTTP::Get.new(blip_url)
    else
      request = Net::HTTP::Post.new(blip_url)
      request.set_form_data(parameter_hash)
    end
    if (require_user_auth)
      request.basic_auth blip_user_name, blip_user_password
    end
    response = http.request(request)
    response_xml = response.body
    return response.body
  }
end

.find_songs(search_term) ⇒ Object



140
141
142
# File 'lib/blipfm.rb', line 140

def BlipFm.find_songs(search_term)
  return create_blip_array(execute_blip_method('search/findSongs', true, true, {'searchTerm' => search_term}), 'Song')
end

.find_users(search_term, offset = 0, limit = 25) ⇒ Object



144
145
146
# File 'lib/blipfm.rb', line 144

def BlipFm.find_users(search_term, offset = 0, limit = 25)
  return create_blip_array(execute_blip_method('search/findUsers', true, true, {'searchTerm' => search_term, 'offset' => offset, 'limit' => limit}), 'user')
end

.generate_blip_nonceObject



22
23
24
# File 'lib/blipfm.rb', line 22

def BlipFm.generate_blip_nonce()
  return UUID.generate().gsub('-', '')
end

.get_blip_timestampObject



176
177
178
179
# File 'lib/blipfm.rb', line 176

def BlipFm.get_blip_timestamp()
  time_xml = execute_blip_method('util/time', false, false)
  return REXML::Document.new(time_xml).root.elements['result'].text
end

.get_blips(blip_id_array) ⇒ Object



92
93
94
# File 'lib/blipfm.rb', line 92

def BlipFm.get_blips(blip_id_array)
  return create_blip_array(execute_blip_method('blip/getById', false, false, {'id' => blip_id_array.join(',')}))
end

.get_public_stream(last_blip_timestamp = 0, offset_blip_id = 0, blip_limit = 25) ⇒ Object



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

def BlipFm.get_public_stream(last_blip_timestamp = 0, offset_blip_id = 0, blip_limit = 25)
  return create_blip_array(execute_blip_method('blip/getPublic', true, true, {'ts' => last_blip_timestamp, 'offset' => offset_blip_id, 'limit' => blip_limit}))
end

.get_user_djs(user_name, offset = 0, limit = 25) ⇒ Object



152
153
154
# File 'lib/blipfm.rb', line 152

def BlipFm.get_user_djs(user_name, offset = 0, limit = 25)
  return create_blip_array(execute_blip_method('user/getFavoriteDJs', true, true, {'username' => user_name, 'offset' => offset, 'limit' => limit}), 'User')
end

.get_user_home(blip_user_name, blip_password, last_blip_timestamp = 0, offset_blip_id = 0, blip_limit = 25) ⇒ Object



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

def BlipFm.get_user_home(blip_user_name, blip_password, last_blip_timestamp = 0, offset_blip_id = 0, blip_limit = 25)
  return create_blip_array(execute_blip_method('blip/getUserHome', true, true, {'ts' => last_blip_timestamp, 'offset' => offset_blip_id, 'limit' => blip_limit}, true, blip_user_name, blip_password))
end

.get_user_listeners(user_name, offset = 0, limit = 25) ⇒ Object



156
157
158
# File 'lib/blipfm.rb', line 156

def BlipFm.get_user_listeners(user_name, offset = 0, limit = 25)
  return create_blip_array(execute_blip_method('user/getListeners', true, true, {'username' => user_name, 'offset' => offset, 'limit' => limit}), 'User')
end

.get_user_playlist(blip_user_name) ⇒ Object



104
105
106
# File 'lib/blipfm.rb', line 104

def BlipFm.get_user_playlist(blip_user_name)
  return create_blip_array(execute_blip_method('blip/getUserPlaylist', true, true, {'username' => blip_user_name}))
end

.get_user_preferences(user_name, user_password) ⇒ Object



160
161
162
# File 'lib/blipfm.rb', line 160

def BlipFm.get_user_preferences(user_name, user_password)
  return execute_blip_method('user/getPreferences', true, true, {}, true, user_name, user_password)
end

.get_user_profile(blip_user_name, offset_blip_id = 0, blip_limit = 25) ⇒ Object



108
109
110
# File 'lib/blipfm.rb', line 108

def BlipFm.(blip_user_name, offset_blip_id = 0, blip_limit = 25)
  return create_blip_array(execute_blip_method('blip/getUserProfile', true, true, {'username' => blip_user_name, 'offset' => offset_blip_id, 'limit' => blip_limit}))
end

.get_user_replies(blip_user_name, offset_blip_id = 0, blip_limit = 150) ⇒ Object



112
113
114
# File 'lib/blipfm.rb', line 112

def BlipFm.get_user_replies(blip_user_name, offset_blip_id = 0, blip_limit = 150)
  return create_blip_array(execute_blip_method('blip/getUserReplies', true, true, {'username' => blip_user_name, 'offset' => offset_blip_id, 'limit' => blip_limit}))
end

.get_user_stats(user_name) ⇒ Object



164
165
166
# File 'lib/blipfm.rb', line 164

def BlipFm.get_user_stats(user_name)
  return execute_blip_method('user/getStats', true, true, {'username' => user_name})
end

.get_users(user_name_array) ⇒ Object



148
149
150
# File 'lib/blipfm.rb', line 148

def BlipFm.get_users(user_name_array)
  return create_blip_array(execute_blip_method('user/getByUsername', true, true, {'username' => user_name_array.join(',')}), 'User')
end

.give_user_props(user_name, user_password, dj_user_name, blip_id = 0) ⇒ Object



168
169
170
# File 'lib/blipfm.rb', line 168

def BlipFm.give_user_props(user_name, user_password, dj_user_name, blip_id = 0)
  return execute_blip_method('user/giveProps', true, true, {'username' => dj_user_name, 'blipId' => blip_id}, true, user_name, user_password, 'POST')
end

.post_blip(user_name, user_password, song_artist, song_title, song_url, blip_message, song_bitrate, song_genre, blip_type, reblip_id = 0, get_recommendations = 1) ⇒ Object



116
117
118
# File 'lib/blipfm.rb', line 116

def BlipFm.post_blip(user_name, user_password, song_artist, song_title, song_url, blip_message, song_bitrate, song_genre, blip_type, reblip_id = 0, get_recommendations = 1)
  return execute_blip_method('blip/post', true, true, {'artist' => song_artist, 'title' => song_title, 'url' => song_url, 'message' => blip_message, 'bitrate' => song_bitrate, 'genre' => song_genre, 'blipType' => blip_type, 'reblipId' => reblip_id, 'getUserRecs' => get_recommendations}, true, user_name, user_password, 'POST')
end

.remove_blip_from_playlist(user_name, user_password, blip_id) ⇒ Object



132
133
134
# File 'lib/blipfm.rb', line 132

def BlipFm.remove_blip_from_playlist(user_name, user_password, blip_id)
  return execute_blip_method('favorite/removeFromPlaylist', true, true, {'blipId' => blip_id}, true, user_name, user_password, 'POST')
end

.remove_dj(user_name, user_password, dj_user_name) ⇒ Object



128
129
130
# File 'lib/blipfm.rb', line 128

def BlipFm.remove_dj(user_name, user_password, dj_user_name)
  return execute_blip_method('favorite/removeDJ', true, true, {'username' => dj_user_name}, true, user_name, user_password, 'POST')
end

.update_playlist(user_name, user_password, blip_id_array) ⇒ Object



136
137
138
# File 'lib/blipfm.rb', line 136

def BlipFm.update_playlist(user_name, user_password, blip_id_array)
  return execute_blip_method('favorite/updatePlaylist', true, true, {'ids' => blip_id_array.join(',')}, true, user_name, user_password, 'POST')
end