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



81
82
83
84
# 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) ⇒ Object



86
87
88
89
90
# 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

.resolve(uri) ⇒ Object

Returns hash from SoundCloud by specified uri



61
62
63
# File 'lib/nehm/client.rb', line 61

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

.search(query, limit, offset) ⇒ Object



53
54
55
56
# File 'lib/nehm/client.rb', line 53

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
49
50
51
# 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) ⇒ Object

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



68
69
70
71
72
73
74
75
76
77
# 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