Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #1 from SeanRoberts/master

Remove reliance on ActiveSupport
  • Loading branch information...
commit 66d75d880b3e5149ee86825eaa3bba16a4414faa 2 parents 9be5812 + 20c0d54
@jeffrafter authored
Showing with 23 additions and 23 deletions.
  1. +23 −23 lib/resource_party.rb
View
46 lib/resource_party.rb
@@ -8,16 +8,16 @@ class Base
include HTTParty
attr_accessor :attributes
-
+
def self.route_for(val)
class_eval("def self.route; '#{val}'; end")
end
-
+
def self.resource_for(val)
class_eval("def self.resource; '#{val}'; end")
end
-
- def initialize(params = {}, query = {}, no_defaults = false)
+
+ def initialize(params = {}, query = {}, no_defaults = false)
return self if no_defaults
response = self.class.get("/#{self.class.route}/new.xml")
handle_errors(response)
@@ -27,63 +27,63 @@ def initialize(params = {}, query = {}, no_defaults = false)
end
def self.find(param, query = {})
- response = self.get("/#{self.route}/#{param}.xml", :query => query)
+ response = self.get("/#{self.route}/#{param}.xml", :query => query)
handle_not_found(response)
handle_errors(response)
handle_response(response)
end
-
+
def self.create(params = {}, query = {})
- response = self.post("/#{self.route}.xml", :body => body_for(params), :query => query)
+ response = self.post("/#{self.route}.xml", :body => body_for(params), :query => query)
handle_errors(response)
handle_response(response)
end
-
+
def self.update(param, params = {}, query = {})
- response = self.put("/#{self.route}/#{param}.xml", :body => body_for(params), :query => query)
+ response = self.put("/#{self.route}/#{param}.xml", :body => body_for(params), :query => query)
handle_not_found(response)
handle_errors(response)
handle_response(response)
end
-
+
def update(params = {}, query = {})
self.class.update(self.id, params, query)
end
- def self.destroy(id, query = {})
- response = self.delete("/#{self.route}/#{id}.xml", :query => query)
+ def self.destroy(id, query = {})
+ response = self.delete("/#{self.route}/#{id}.xml", :query => query)
handle_not_found(response)
handle_errors(response)
handle_response(response)
end
-
+
def destroy(query = {})
self.class.destroy(self.id, query)
end
def self.all(query = {})
- response = self.get("/#{self.route}.xml", :query => query)
+ response = self.get("/#{self.route}.xml", :query => query)
handle_errors(response)
items = response.values.first
- items.map{|hash| self.from_xml hash }
+ items.map{|hash| self.from_xml hash }
end
private
def self.handle_not_found(response)
raise RecordNotFound.new(response.body) if response.code == "404"
- end
+ end
def self.handle_errors(response)
raise ServerError.new(response.body) if response.code != "200"
end
-
+
def self.handle_response(response)
# Rails will return ' ' when you render :nothing => true
- return nil if response.body.blank? || response.body == ' '
+ return nil if ['', nil, ' '].include? response.body
hash = Hash.from_xml(response.body).values.first
- self.from_xml hash
- end
+ self.from_xml hash
+ end
def self.body_for(params)
require 'cgi' unless defined?(CGI) && defined?(CGI::escape)
@@ -91,7 +91,7 @@ def self.body_for(params)
params.each{|n,v| body["#{self.resource}[#{n}]"] = v }
body.map{|n,v| "#{CGI.escape(n.to_s)}=#{CGI.escape(v.to_s)}"} * "&"
end
-
+
def self.from_xml(params, obj = nil)
obj ||= self.new({}, {}, true)
decoded_params = params.dup
@@ -102,12 +102,12 @@ def self.from_xml(params, obj = nil)
define_method(k) do
return self.attributes[k]
end
-
+
define_method("#{k}=") do |val|
self.attributes[k] = HTMLEntities.new.decode(val)
end
end
- end
+ end
obj.send(:extend, mod)
obj
end
Please sign in to comment.
Something went wrong with that request. Please try again.