Class: OAuthActiveResource::Resource

Inherits:
ActiveResource::Base
  • Object
show all
Defined in:
lib/oauth_active_resource/resource.rb

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.oauth_connectionObject

Returns the value of attribute oauth_connection.



9
10
11
# File 'lib/oauth_active_resource/resource.rb', line 9

def oauth_connection
  @oauth_connection
end

Class Method Details

.belongs_to(*args) ⇒ Object

belongs_to :user

=> will look for a user-id tag and load this user


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/oauth_active_resource/resource.rb', line 39

def self.belongs_to(*args)
  args.each do |k| 
    name = k.to_s
    define_method(k) do          
      if @belongs_to_cache.nil?
        @belongs_to_cache = {}
      end
      if not @belongs_to_cache[name]
        resource  = find_or_create_resource_for(name)
        @belongs_to_cache[name] = resource.find(self.send("#{name}_id"))
      end
      return @belongs_to_cache[name]          
    end
  end
end

.connection(refresh = false) ⇒ Object



12
13
14
15
16
# File 'lib/oauth_active_resource/resource.rb', line 12

def self.connection(refresh = false)
  @connection = Connection.new(oauth_connection, site,format) if @connection.nil? || refresh
  @connection.timeout = timeout if timeout      
  return @connection      
end

.has_many(*args) ⇒ Object

has_many allows resources with sub-resources which arent nested to be accessable.

Example: User 123 (example.com/users/123/) has many friends The list of friends can be accessed by example.com/users/123/friends Our class definition:

 class User < Resource
   has_many :friends
 end

 user = User.find(123)
 user.friends.each do |friend|
   p friend.name
 end

 # adding a friend 
 stranger = User.find(987)
 user.friends << stranger
 user.friends.save
=> sends a PUT with collection of friends to http://example.com/users/123/friends ## OUTDATED!!?


77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/oauth_active_resource/resource.rb', line 77

def self.has_many(*args)
  args.each do |k| 
    name = k.to_s
    singular = name.singularize
    define_method(k) do |*options|   
      
      options = options.first || {}      
      #if @has_many_cache.nil?
      #  @has_many_cache = {}
      #end
      @has_many_cache ||= {}
      cache_name = "#{name}#{options.hash}"
      if not @has_many_cache[cache_name]

        collection_path = "/#{self.element_name.pluralize}/#{self.id}/#{name}"
        resource  = find_or_create_resource_for(singular)
        @has_many_cache[cache_name] = OAuthActiveResource::UniqueResourceArray.new(self.connection,resource,collection_path,options)
      end
      return @has_many_cache[cache_name]          
    end
  end
end

.load_collection(*args) ⇒ Object



31
32
33
# File 'lib/oauth_active_resource/resource.rb', line 31

def self.load_collection(*args)
  instantiate_collection(*args)
end

.send_multipart_request(method, path, files, params = {}) ⇒ Object

allows you to POST/PUT an oauth authenticated multipart request



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/oauth_active_resource/resource.rb', line 101

def self.send_multipart_request(method,path,files,params={})
  req = Net::HTTP::Post.new(path)
  if method == :put
    params[:_method] = "PUT"
  end
  file_hash = {}
  files.each do |k,v|
    file_hash[k] = Net::HTTP::FileForPost.new(v)
  end
  req.set_multipart_data(file_hash, params)  
  
  oauth_connection.sign!(req) if not oauth_connection.nil?   
  res = Net::HTTP.new(site.host, site.port).start do |http|
    http.request(req)
  end
  res
end

Instance Method Details

#==(other) ⇒ Object



25
26
27
28
29
# File 'lib/oauth_active_resource/resource.rb', line 25

def ==(other)
    return (other.is_a? Resource) && (self.is_a? Resource) && (self.element_name == other.element_name) && (self.id == other.id)
  rescue
    return super(other)
end

#load(*args) ⇒ Object

TODO remove when soundcloud api is fixed if self has no id, try extracting from uri



20
21
22
23
# File 'lib/oauth_active_resource/resource.rb', line 20

def load(*args)
  super(*args)  
  self.id = self.uri.split('/').last if self.id.nil? and defined? self.uri
end