module Nehm::Client

Client module contains all SC API interaction methods Also it forms urls und send them to HTTPClient

Constants

HTTP_CLIENT

HTTP client object

TRACKS_LIMIT

Max limit of tracks for correct SoundCloud requests

Public Class Methods

resolve(uri) click to toggle source

Returns hash from SoundCloud by specified uri

# File lib/nehm/client.rb, line 61
def self.resolve(uri)
  HTTP_CLIENT.resolve(uri)
end
tracks(count, offset, type, uid) click to toggle source

Returns raw array of likes or posts (depends on argument ‘type’)

# File lib/nehm/client.rb, line 30
def self.tracks(count, offset, type, uid)
  iterations = count.to_f / TRACKS_LIMIT
  iterations = iterations.ceil

  tracks = []
  iterations.times do |i|
    limit = count < TRACKS_LIMIT ? count : TRACKS_LIMIT
    count -= TRACKS_LIMIT

    received =
      case type
      when :likes
        likes(limit, i * TRACKS_LIMIT + offset, uid)
      when :posts
        posts(limit, i * TRACKS_LIMIT + offset, uid)
      end

    tracks += received if received.is_a? Array # If received is a hash, then
                                               # there was error
  end
  tracks
end
user(permalink) click to toggle source

Returns user hash from SoundCloud or nil if user doesn’t exist

# File lib/nehm/client.rb, line 68
def self.user(permalink)
  url = "http://soundcloud.com/#{permalink}"
  begin
    HTTP_CLIENT.resolve(url)
  rescue HTTPClient::Status404
    return nil
  rescue HTTPClient::ConnectionError
    UI.term "Connection error. Check your internet connection\nSoundCloud can also be down"
  end
end

Public Instance Methods

likes(limit, offset, uid) click to toggle source
# File lib/nehm/client.rb, line 81
def likes(limit, offset, uid)
  uri = "/users/#{uid}/favorites?limit=#{limit}&offset=#{offset}"
  HTTP_CLIENT.get(1, uri)
end
posts(limit, offset, uid) click to toggle source
# File lib/nehm/client.rb, line 86
def posts(limit, offset, uid)
  uri = "/profile/soundcloud:users:#{uid}?limit=#{limit}&offset=#{offset}"
  response = HTTP_CLIENT.get(2, uri)
  response['collection']
end