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



80
81
82
83
# File 'lib/nehm/client.rb', line 80

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

.posts(limit, offset, uid) ⇒ Object



85
86
87
88
89
# File 'lib/nehm/client.rb', line 85

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

.search(query, limit, offset) ⇒ Object



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

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

.track(uri) ⇒ Object

Returns track hash from SoundCloud by specified uri



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

def self.track(uri)
  HTTP_CLIENT.resolve(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
# File 'lib/nehm/client.rb', line 30

def self.tracks(count, offset, type, uid)
  UI.term "Invalid number of #{type}" if count == 0

  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



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

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