Class: Bremen::Soundcloud

Inherits:
Base
  • Object
show all
Defined in:
lib/bremen/soundcloud.rb

Constant Summary collapse

BASE_URL =
'http://api.soundcloud.com/'

Class Attribute Summary collapse

Attributes included from Track

#author, #created_at, #length, #thumbnail_url, #title, #uid, #updated_at, #url

Class Method Summary collapse

Methods inherited from Base

find, search

Methods included from Request

#build_query, #get

Methods included from Track

#initialize

Class Attribute Details

.client_idObject

Returns the value of attribute client_id.



16
17
18
# File 'lib/bremen/soundcloud.rb', line 16

def client_id
  @client_id
end

Class Method Details

.build_query(options = {}) ⇒ Object



18
19
20
21
22
# File 'lib/bremen/soundcloud.rb', line 18

def build_query options = {}
  self.client_id ||= ENV['SOUNDCLOUD_CLIENT_ID']
  raise %Q{"#{self.name}.client_id" must be set} unless client_id
  super(options.merge(client_id: client_id))
end

.find_url(uid_or_url) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/bremen/soundcloud.rb', line 24

def find_url uid_or_url
  if uid_or_url.to_s =~ %r{\A\d+\Z}
    "#{BASE_URL}tracks/#{uid_or_url}.json?#{build_query}"
  else
    "#{BASE_URL}resolve.json?#{build_query({url: uid_or_url})}"
  end
end

.from_api(hash = {}) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/bremen/soundcloud.rb', line 44

def from_api hash = {}
  created_at = Time.parse(hash['created_at']).utc
  new({
    uid: hash['id'].to_s,
    url: hash['permalink_url'],
    title: hash['title'],
    author: Bremen::Author.new({
      uid: hash['user']['id'].to_s,
      url: hash['user']['permalink_url'],
      name: hash['user']['username'],
      thumbnail_url: hash['user']['avatar_url'].sub(%r{\?.*}, ''),
    }),
    length: (hash['duration'].to_i / 1000).round,
    thumbnail_url: hash['artwork_url'] ? hash['artwork_url'].sub(%r{\?.*}, '') : nil,
    created_at: created_at,
    updated_at: created_at,
  })
end

.search_url(options = {}) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/bremen/soundcloud.rb', line 32

def search_url options = {}
  options = default_options.merge(options)
  query = {
    q: options[:keyword],
    order: options[:order],
    limit: options[:limit],
    offset: options[:limit] * (options[:page] - 1),
    filter: options[:filter],
  }
  "#{BASE_URL}tracks.json?#{build_query(query)}"
end