Class: Soundcloud::Models::Base

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

Overview

:nodoc:

Direct Known Subclasses

Comment, Event, Playlist, Track, User

Class Method Summary collapse

Class Method Details

.can_be_a_single_changeable(*args) ⇒ Object

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


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

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

.has_many_single_changeable(*args) ⇒ Object

see can_be_a_single_changeable



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/soundcloud/models/base.rb', line 66

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