Class: SoundCouch

Inherits:
Object
  • Object
show all
Defined in:
lib/soundcouch.rb,
lib/soundcouch/client.rb,
lib/soundcouch/form_request.rb

Defined Under Namespace

Classes: Client, FormRequest

Constant Summary collapse

ReadOnlyError =
Class.new(RuntimeError)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(permalink_url, access_token) ⇒ SoundCouch

Returns a new instance of SoundCouch


17
18
19
20
21
22
23
24
# File 'lib/soundcouch.rb', line 17

def initialize(permalink_url, access_token)
  @client = Client.new('https://api.soundcloud.com', access_token)

  @user = @client.request(:get, resolve(permalink_url)).json
  @me = @client.request(:get, '/me').json

  @index = {}
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client


15
16
17
# File 'lib/soundcouch.rb', line 15

def client
  @client
end

Instance Method Details

#all_docs(options = {}) ⇒ Object


34
35
36
37
# File 'lib/soundcouch.rb', line 34

def all_docs(options = {})
  response = @client.request(:get, @user['uri'] + '/tracks')
  response.json.map { |track| document(track, options) }
end

#bulk_docs(docs, _options = {}) ⇒ Object


39
40
41
42
43
# File 'lib/soundcouch.rb', line 39

def bulk_docs(docs, _options = {})
  docs.map do |doc|
    doc['uri'].nil? ? post(doc) : put(doc)
  end
end

#changesObject


26
27
28
29
30
31
32
# File 'lib/soundcouch.rb', line 26

def changes
  all_docs.map do |doc|
    { 'seq'     => 0,
      'id'      => doc['_id'],
      'changes' => ['rev' => doc['_rev']] }
  end
end

#delete(doc) ⇒ Object


63
64
65
66
67
# File 'lib/soundcouch.rb', line 63

def delete(doc)
  @index.clear
  response = @client.request(:delete, doc_uri(doc))
  response.successful?
end

#get(doc, options = {}) ⇒ Object


53
54
55
56
57
58
59
60
61
# File 'lib/soundcouch.rb', line 53

def get(doc, options = {})
  response = @client.request(:get, doc_uri(doc))
  if response.successful?
    document(response.json, options)
  else
    ap [:get, doc_uri(doc), response]
    false
  end
end

#post(doc) ⇒ Object

FIXME: filename might be optional

Raises:


80
81
82
83
84
85
86
87
88
89
# File 'lib/soundcouch.rb', line 80

def post(doc)
  raise ReadOnlyError if readonly?

  track    = relevant_fields(doc).merge((doc))
  filename = doc['_attachments'].keys.first
  data     = filename && doc['_attachments'][filename]['data']

  response = @client.upload(track, filename, data)
  document(response.json)
end

#put(doc) ⇒ Object

Raises:


69
70
71
72
73
74
75
76
77
# File 'lib/soundcouch.rb', line 69

def put(doc)
  raise ReadOnlyError if readonly?

  track = relevant_fields(doc)
    .merge((doc))

  response = @client.request(:put, doc_uri(doc), track)
  document(response.json)
end

#readonly?Boolean

Returns:

  • (Boolean)

100
101
102
# File 'lib/soundcouch.rb', line 100

def readonly?
  @me['uri'] != @user['uri']
end

#resolve(permalink_url) ⇒ Object


91
92
93
94
95
96
97
98
# File 'lib/soundcouch.rb', line 91

def resolve(permalink_url)
  response = @client.request(:get, '/resolve?url=' + permalink_url)
  if response.redirection?
    response.headers['Location']
  else
    false
  end
end

#revs_diff(revs, options = {}) ⇒ Object


45
46
47
48
49
50
51
# File 'lib/soundcouch.rb', line 45

def revs_diff(revs, options = {})
  docs = all_docs(options)
  revs.reject do |id, rev_ids|
    doc = docs.detect { |d| d['_id'] == id }
    doc && rev_ids.include?(doc['_rev'])
  end
end

#wait(doc, interval) ⇒ Object


104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/soundcouch.rb', line 104

def wait(doc, interval)
  return unless doc['uri']
  loop do
    response = @client.request(:get, doc['uri'])
    # 404 probably caused by replication lag on soundcloud's side
    if (response.client_error? && response.status != 404) ||
        response.json['state'] =~ /finished|failed/
      ap [:wait, doc['uri'], response]
      break
    else
      HTTPkit.sleep(interval)
    end
  end
end