lib/restful_resource/base.rb in restful_resource-0.0.11 vs lib/restful_resource/base.rb in restful_resource-0.8.0
- old
+ new
@@ -1,174 +1,116 @@
module RestfulResource
- class Base
- def self.url=(url)
- @url = url
- end
+ class Base < OpenObject
+ extend RestfulResource::Associations
- def self.processed_url_and_params(params={})
- uri = URI(@url)
- path = uri.path
- other_params = params.clone
- missing_params = []
-
- path_params = path.scan(/:([A-Za-z][^\/]*)/).flatten
- path_params.each do |key|
- value = other_params.delete(key.to_sym)
- if value.nil?
- missing_params << key
- else
- path = path.gsub(':'+key.to_s, value.to_s)
- end
- end
-
- if missing_params.any?
- raise ParameterMissingError.new(missing_params)
- end
-
- uri.path = path
- [uri.to_s, other_params]
+ def self.http=(http)
+ @@http = http
end
- def self.url(params={})
- processed_url_and_params(params).first
+ def self.http
+ @@http ||= RestfulResource::HttpClient.new()
end
-
- def self.has_one(nested_resource_type)
- klass = nested_resource_type.to_s.camelize.safe_constantize
- klass = OpenStruct if (klass.nil? || klass.superclass != RestfulResource)
-
- self.send(:define_method, nested_resource_type) do
- nested_resource = @inner_object.send(nested_resource_type)
- return nil if nested_resource.nil?
- klass.new(@inner_object.send(nested_resource_type))
- end
+ def self.base_url=(url)
+ @base_url = URI.parse(url)
end
- def self.has_many(nested_resource_type)
- klass = nested_resource_type.to_s.singularize.camelize.safe_constantize
- klass = OpenStruct if (klass.nil? || (klass.superclass != RestfulResource))
-
- self.send(:define_method, nested_resource_type) do
- @inner_object.send(nested_resource_type).map { |obj| klass.new(obj) }
- end
+ def self.base_url
+ @base_url
end
- def initialize(attributes = {}, hack_for_activeresource = false)
- @inner_object = OpenStruct.new(attributes)
+ def self.resource_path(url)
+ @resource_path = url
end
- def method_missing(method)
- if @inner_object.respond_to?(method)
- @inner_object.send(method)
- else
- super(method)
- end
+ def self.find(id, params={})
+ response = http.get(member_url(id, params))
+ self.new(parse_json(response.body))
end
- def respond_to?(method, include_private = false)
- super || @inner_object.respond_to?(method, include_private)
+ def self.where(params={})
+ response = http.get(collection_url(params))
+ self.paginate_response(response)
end
- def valid?
- errors.nil? || errors.count == g
+ def self.get(params={})
+ response = http.get(collection_url(params))
+ RestfulResource::OpenObject.new(parse_json(response.body))
end
- def self.find(id, url_params={})
- response = RestClient.get("#{url(url_params)}/#{id}", params: {})
- self.new(ActiveSupport::JSON.decode(response))
+ def self.all
+ self.where
end
- def self.get_one(url_params={})
- resource = create_new_resource(url_params)
- response = resource.get
- self.new(ActiveSupport::JSON.decode(response))
+ def self.action(action_name)
+ clone = self.clone
+ clone.action_prefix = action_name
+ clone
end
- def self.update_attributes(id, attributes)
- begin
- result = parse(RestClient.put("#{url}/#{id}", attributes))
- rescue RestClient::UnprocessableEntity => e
- errors = parse(e.response)
- result = attributes.merge(errors: errors)
- end
- self.new(result)
+ def self.action_prefix=(action_prefix)
+ @action_prefix = action_prefix.to_s
end
- def self.create(attributes)
- begin
- result = parse(RestClient.post("#{url}", attributes))
- rescue RestClient::UnprocessableEntity => e
- errors = parse(e.response)
- result = attributes.merge(errors: errors)
- end
- self.new(result)
+ def as_json(options=nil)
+ @inner_object.send(:table).as_json(options)
end
- def self.search(params = {})
- response = RestClient.get("#{url}/search", params: params)
- paginate_response(response)
+ private
+ def self.merge_url_paths(uri, *paths)
+ uri.merge(paths.compact.join('/')).to_s
end
- def self.all(params = {})
- resource = create_new_resource(params)
- response = resource.get
- paginate_response(response)
+ def self.member_url(id, params)
+ url = merge_url_paths(superclass.base_url, @resource_path, id, @action_prefix)
+ replace_parameters(url, params)
end
- def self.get(postfix_url = "", params = {})
- response = RestClient.get("#{url}#{postfix_url}", params: params)
- paginate_response(response)
+ def self.collection_url(params)
+ url = merge_url_paths(superclass.base_url, @resource_path, @action_prefix)
+ replace_parameters(url, params)
end
- def self.put(postfix_url = "", params = {})
- response = RestClient.put("#{url}#{postfix_url}", params)
+ def self.new_collection(json)
+ json.map do |element|
+ self.new(element)
+ end
end
- def self.post(postfix_url = "", params = {})
- response = RestClient.post("#{url}#{postfix_url}", params)
+ def self.parse_json(json)
+ ActiveSupport::JSON.decode(json)
end
- def self.all_not_paginated
- page = 1
- results = []
- while page
- page_data = self.all(page: page);
- results += page_data
- page = page_data.next_page
+ def self.replace_parameters(url, params)
+ missing_params = []
+ params = params.with_indifferent_access
+
+ url_params = url.scan(/:([A-Za-z][^\/]*)/).flatten
+ url_params.each do |key|
+ value = params.delete(key)
+ if value.nil?
+ missing_params << key
+ else
+ url = url.gsub(':'+key, value.to_s)
+ end
end
- results
- end
+ if missing_params.any?
+ raise ParameterMissingError.new(missing_params)
+ end
- def self.parse(json)
- ActiveSupport::JSON.decode(json)
+ url = url + "?#{params.to_query}" unless params.empty?
+ url
end
- def to_json(*args)
- @inner_object.send(:table).to_json(*args)
- end
-
- def as_json(*)
- @inner_object.send(:table).as_json
- end
-
- private
def self.paginate_response(response)
links_header = response.headers[:links]
links = LinkHeader.parse(links_header)
prev_url = links.find_link(['rel', 'prev']).try(:href)
next_url = links.find_link(['rel', 'next']).try(:href)
- array = ActiveSupport::JSON.decode(response).map { |attributes| self.new(attributes) }
+ array = parse_json(response.body).map { |attributes| self.new(attributes) }
PaginatedArray.new(array, previous_page_url: prev_url, next_page_url: next_url)
end
-
- def self.create_new_resource(params={})
- url, other_params = processed_url_and_params(params)
- url += "?#{other_params.to_query}" if not other_params.empty?
- resource = RestClient::Resource.new("#{url}")
- end
-
end
end