Module: Nehm::Client

Defined in:
lib/nehm/client.rb

Overview

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

Constant Summary collapse

HTTP_CLIENT =

HTTP client object

HTTPClient.new
TRACKS_LIMIT =

Max limit of tracks for correct SoundCloud requests

200

Class Method Summary collapse

Class Method Details

.likes(limit, offset, uid) ⇒ Object



78
79
80
81
# File 'lib/nehm/client.rb', line 78

def likes(limit, offset, uid)
  uri = "/users/#{uid}/favorites?limit=#{limit}&offset=#{offset}"
  HTTP_CLIENT.get(1, uri)
end

.posts(limit, offset, uid) ⇒ Object



83
84
85
86
87
# File 'lib/nehm/client.rb', line 83

def posts(limit, offset, uid)
  uri = "/profile/soundcloud:users:#{uid}?limit=#{limit}&offset=#{offset}"
  response = HTTP_CLIENT.get(2, uri)
  response['collection']
end

.resolve(uri) ⇒ Object

Returns hash from SoundCloud by specified uri



58
59
60
# File 'lib/nehm/client.rb', line 58

def self.resolve(uri)
  HTTP_CLIENT.resolve(uri)
end

.search(query, limit, offset) ⇒ Object



50
51
52
53
# File 'lib/nehm/client.rb', line 50

def self.search(query, limit, offset)
  uri = "/tracks?q=#{query}&limit=#{limit}&offset=#{offset}"
  HTTP_CLIENT.get(1, uri)
end

.tracks(count, offset, type, uid) ⇒ Object

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



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# 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

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

.user(permalink) ⇒ Object

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



65
66
67
68
69
70
71
72
73
74
# File 'lib/nehm/client.rb', line 65

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