Class: Soundcloud::Models::Base

Inherits:
OAuthActiveResource::Resource
  • Object
show all
Defined in:
lib/soundcloud/models/base.rb

Overview

:nodoc:

Direct Known Subclasses

Comment, Group, Playlist, Track, User

Class Method Summary (collapse)

Instance Method Summary (collapse)

Class Method Details

+ (Object) can_be_a_single_changeable(*args)

has_many_single_changeable and can_be_a_single_changeable is mostly used in combination with has_many.

can_be_a_single_changeable expects to have a resource /me which is the logged-in user

like in self.has_many you have a resource user with a sub resource friends, but its not allowed to send a PUT /me/friends to update the list of friends instead to add or remove a friend you have to send a GET/PUT/DELETE to /me/friends/user_id

Example:

 class User < Resource
   has_many :friends
   can_be_a_single_changeable :friend
   has_many_single_changeable :friends
 end

 me = User.find(:one, :from => '/me')
 friend = me.friends.first
 stranger = User.find(235)

 friend.is_friend?
=> true
 stranger.is_friend?
=> false

 strange.add_friend!
 stranger.is_friend?
=> true

 stranger.remove_friend!
 stranger.is_friend?
=> false    

 friend.has_friend?(stranger.id)
=> checks if stranger and friend are friend, returns true or false


79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/soundcloud/models/base.rb', line 79

def self.can_be_a_single_changeable(*args)
  args.each do |k| 
    singular = k.to_s
    define_method("is_#{singular}?") do
      begin
        self.connection.get_without_decoding "/me/#{singular.pluralize}/#{self.id}"
        return true
      rescue ActiveResource::ResourceNotFound
        return false
      end
    end
    
    define_method("add_#{singular}!") do
      self.connection.put "/me/#{singular.pluralize}/#{self.id}"
    end                    

    define_method("remove_#{singular}!") do
      self.connection.delete "/me/#{singular.pluralize}/#{self.id}"
    end                
  end    
end

+ (Object) connection(refresh = false)

Overriden to pass our SCXmlFormat for start index partitioning



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/soundcloud/models/base.rb', line 6

def self.connection(refresh = false)
  if defined?(@connection) || superclass == Object
    @connection =  Soundcloud::SCConnection.new(@oauth_connection, site, ActiveResource::Formats::SCXmlFormat) if refresh || @connection.nil?
    @connection.proxy = proxy if proxy
    @connection.user = user if user
    @connection.password = password if password
    @connection.timeout = timeout if timeout
    @connection.ssl_options = ssl_options if ssl_options
    @connection
  else
    superclass.connection
  end
end

+ (Object) has_many_single_changeable(*args)

see can_be_a_single_changeable



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/soundcloud/models/base.rb', line 102

def self.has_many_single_changeable(*args)
  args.each do |k| 
    singular = k.to_s.singularize
    define_method("has_#{singular}?") do |object_or_id|
      if object_or_id.is_a? String or object_or_id.is_a? Integer          
        look_for_id = object_or_id
      else
        look_for_id = object_or_id.id
      end
        
      begin
        self.connection.get_without_decoding "/#{self.element_name.pluralize}/#{self.id}/#{singular.pluralize}/#{look_for_id}"
        return true
      rescue ActiveResource::ResourceNotFound
        return false
      end          
    end
  end    
end

Instance Method Details

- (Object) send_files(method, path, resource)



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/soundcloud/models/base.rb', line 20

def send_files(method, path, resource)
  self.connection.site.path[-1] == '/' ? version = self.connection.site.path.chop : version = self.connection.site.path
  unless path[version]
    path = "#{version}#{path}"
  end
  
  params = {}
  self.attributes.reject { |k,v| data_attributes.include?(k)}.each { |k,v| 
    params["#{resource}[#{k}]"] = v
  }
  
  files = {}
  data_attributes.each do |attr|
    files["#{resource}[#{attr}]".to_sym] = self.attributes[attr] if self.attributes.has_key?(attr)
    self.attributes[attr] = nil
  end
  
  response = connection.handle_response(self.class.send_multipart_request(method,path,files,params))
 
  self.id = id_from_response(response)
  load_attributes_from_response(response)
end