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.



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

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


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

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



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

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!!?


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

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



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

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

.multipart_bug_fix(params) ⇒ Object

ignore is added because the multipart gem is adding an extra new line to the last parameter which will break parsing of track



104
105
106
107
108
109
110
111
# File 'lib/oauth_active_resource/resource.rb', line 104

def self.multipart_bug_fix(params)
  ordered_params = ActiveSupport::OrderedHash.new
  params.each do |k,v|
    ordered_params[k] = v
  end
  ordered_params[:ignore] = 'multipart bug'
  ordered_params
end

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

allows you to POST/PUT an oauth authenticated multipart request



114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/oauth_active_resource/resource.rb', line 114

def self.send_multipart_request(method, path, files, params={})
  params[:_method] = "PUT" if method == :put      
  files.each do |k, v|
    params[k] = UploadIO.new(v, 'application/octet', v.path)
  end

  req = Net::HTTP::Post::Multipart.new path, 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



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

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



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

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