1 require 'activeresource'
4 class Connection < ActiveResource::Connection
7 @default_header = { 'Content-Type' => format.mime_type, 'Accept' => format.mime_type, 'X-Blip-API' => '0.02' }
11 class Resource < ActiveResource::Base
12 self.site = 'http://api.blip.pl'
16 def connection(refresh = false)
17 @connection = Connection.new(site, format) if refresh || @connection.nil?
18 @connection.user = user if user
19 @connection.password = password if password
20 @connection.timeout = timeout if timeout
23 def const_missing name
24 const_set name, Class.new(Resource) unless const_defined? name
30 def method_missing sym, *args
35 if @attributes.include?(name + '_path')
36 klass = Blip.const_get name.camelcase
37 klass.find(:one, :from => @attributes[name + '_path'])
43 returning connection.put(stripped_path(prefix_options), attributes.to_json, self.class.headers) do |response|
44 load_attributes_from_response(response)
48 # Create (i.e., save to the remote service) the new resource.
50 returning connection.post(stripped_path, attributes.to_json, self.class.headers) do |response|
51 self.id = id_from_response(response)
52 load_attributes_from_response(response)
56 def stripped_path(*a) # strips back out the extension
57 element_path(*a).sub(/\/?\.#{self.class.format.extension}/, "")
63 def const_missing name
64 const_set name, Class.new(Resource) unless const_defined? name
68 def method_missing name, *args
69 Resource.send name, args