Skip to content
Browse files

Remove Vim folds, they don’t belong in open source projects

  • Loading branch information...
1 parent e01a5e2 commit 863ced6a4c88c6429d5cd354429d2095896f9179 @remiprev committed Nov 11, 2012
View
8 Rakefile
@@ -7,12 +7,12 @@ require "rspec/core/rake_task"
task :default => :spec
desc "Run all specs"
-RSpec::Core::RakeTask.new(:spec) do |task| # {{{
+RSpec::Core::RakeTask.new(:spec) do |task|
task.pattern = "spec/**/*_spec.rb"
-end # }}}
+end
desc "Generate YARD Documentation"
-YARD::Rake::YardocTask.new do |task| # {{{
+YARD::Rake::YardocTask.new do |task|
task.options = [
"-o", File.expand_path("../doc", __FILE__),
"--readme=README.md",
@@ -24,4 +24,4 @@ YARD::Rake::YardocTask.new do |task| # {{{
"--title=Her",
]
task.files = ["lib/**/*.rb"]
-end # }}}
+end
View
16 lib/her/api.rb
@@ -6,10 +6,10 @@ class API
attr_reader :base_uri, :connection, :options
# Setup a default API connection. Accepted arguments and options are the same as {API#setup}.
- def self.setup(attrs={}, &block) # {{{
+ def self.setup(attrs={}, &block)
@@default_api = new
@@default_api.setup(attrs, &block)
- end # }}}
+ end
# Setup the API connection.
#
@@ -49,21 +49,21 @@ def self.setup(attrs={}, &block) # {{{
# connection.use MyCustomParser
# connection.use Faraday::Adapter::NetHttp
# end
- def setup(attrs={}) # {{{
+ def setup(attrs={})
attrs[:url] = attrs.delete(:base_uri) if attrs.include?(:base_uri) # Support legacy :base_uri option
@base_uri = attrs[:url]
@options = attrs
@connection = Faraday.new(attrs.slice(:url, :ssl)) do |connection|
yield connection if block_given?
end
- end # }}}
+ end
# Define a custom parsing procedure. The procedure is passed the response object and is
# expected to return a hash with three keys: a main data Hash, an errors Array
# and a metadata Hash.
#
# @private
- def request(attrs={}) # {{{
+ def request(attrs={})
method = attrs.delete(:_method)
path = attrs.delete(:_path)
headers = attrs.delete(:_headers)
@@ -80,12 +80,12 @@ def request(attrs={}) # {{{
end
end
response.env[:body]
- end # }}}
+ end
private
# @private
- def self.default_api(attrs={}) # {{{
+ def self.default_api(attrs={})
defined?(@@default_api) ? @@default_api : nil
- end # }}}
+ end
end
end
View
4 lib/her/collection.rb
@@ -3,10 +3,10 @@ class Collection < ::Array
attr_reader :metadata, :errors
# @private
- def initialize(items=[], metadata={}, errors=[]) # {{{
+ def initialize(items=[], metadata={}, errors=[])
super(items)
@metadata = metadata || {}
@errors = errors || []
- end # }}}
+ end
end
end
View
8 lib/her/middleware/accept_json.rb
@@ -2,14 +2,14 @@ module Her
module Middleware
# This middleware adds a "Accept: application/json" HTTP header
class AcceptJSON < Faraday::Middleware
- def add_header(headers) # {{{
+ def add_header(headers)
headers.merge! "Accept" => "application/json"
- end # }}}
+ end
- def call(env) # {{{
+ def call(env)
add_header(env[:request_headers])
@app.call(env)
- end # }}}
+ end
end
end
end
View
8 lib/her/middleware/first_level_parse_json.rb
@@ -6,7 +6,7 @@ class FirstLevelParseJSON < Faraday::Response::Middleware
#
# @param [String] body The response body
# @return [Mixed] the parsed response
- def parse(body) # {{{
+ def parse(body)
json = MultiJson.load(body, :symbolize_keys => true)
errors = json.delete(:errors) || []
metadata = json.delete(:metadata) || []
@@ -15,15 +15,15 @@ def parse(body) # {{{
:errors => errors,
:metadata => metadata
}
- end # }}}
+ end
# This method is triggered when the response has been received. It modifies
# the value of `env[:body]`.
#
# @param [Hash] env The response environment
- def on_complete(env) # {{{
+ def on_complete(env)
env[:body] = parse(env[:body])
- end # }}}
+ end
end
end
end
View
8 lib/her/middleware/second_level_parse_json.rb
@@ -7,22 +7,22 @@ class SecondLevelParseJSON < Faraday::Response::Middleware
#
# @param [String] body The response body
# @return [Mixed] the parsed response
- def parse(body) # {{{
+ def parse(body)
json = MultiJson.load(body, :symbolize_keys => true)
{
:data => json[:data],
:errors => json[:errors],
:metadata => json[:metadata]
}
- end # }}}
+ end
# This method is triggered when the response has been received. It modifies
# the value of `env[:body]`.
#
# @param [Hash] env The response environment
- def on_complete(env) # {{{
+ def on_complete(env)
env[:body] = parse(env[:body])
- end # }}}
+ end
end
end
end
View
24 lib/her/model/hooks.rb
@@ -53,55 +53,55 @@ def after_destroy(method=nil, &block); set_hook(:after, :destroy, method || bloc
# Wrap a block between “before” and “after” hooks
# @private
- def wrap_in_hooks(resource, *hooks) # {{{
+ def wrap_in_hooks(resource, *hooks)
perform_before_hooks(resource, *hooks)
yield(resource, resource.class)
perform_after_hooks(resource, *hooks.reverse)
- end # }}}
+ end
# @private
- def hooks # {{{
+ def hooks
@her_hooks ||= begin
if superclass.respond_to?(:hooks)
superclass.hooks.dup
else
{}
end
end
- end # }}}
+ end
private
# @private
- def set_hook(time, name, action) # {{{
+ def set_hook(time, name, action)
(self.hooks["#{time}_#{name}".to_sym] ||= []) << action
- end # }}}
+ end
# @private
- def perform_hook(record, time, name) # {{{
+ def perform_hook(record, time, name)
Array(self.hooks["#{time}_#{name}".to_sym]).each do |hook|
if hook.is_a? Symbol
record.send(hook)
else
hook.call(record)
end
end
- end # }}}
+ end
# Perform “after” hooks on a resource
# @private
- def perform_after_hooks(resource, *hooks) # {{{
+ def perform_after_hooks(resource, *hooks)
hooks.each do |hook|
perform_hook(resource, :after, hook)
end
- end # }}}
+ end
# Perform “before” hooks on a resource
# @private
- def perform_before_hooks(resource, *hooks) # {{{
+ def perform_before_hooks(resource, *hooks)
hooks.each do |hook|
perform_hook(resource, :before, hook)
end
- end # }}}
+ end
end
end
end
View
112 lib/her/model/http.rb
@@ -3,27 +3,27 @@ module Model
# This module interacts with Her::API to fetch HTTP data
module HTTP
# Automatically inherit a superclass' api
- def her_api # {{{
+ def her_api
@her_api ||= begin
superclass.her_api if superclass.respond_to?(:her_api)
end
- end # }}}
+ end
# Link a model with a Her::API object
- def uses_api(api) # {{{
+ def uses_api(api)
@her_api = api
- end # }}}
+ end
# Main request wrapper around Her::API. Used to make custom request to the API.
# @private
- def request(attrs={}) # {{{
+ def request(attrs={})
parsed_data = her_api.request(attrs)
if block_given?
yield parsed_data
else
parsed_data
end
- end # }}}
+ end
# Make a GET request and return either a collection or a resource
#
@@ -34,7 +34,7 @@ def request(attrs={}) # {{{
#
# @popular_users = User.get(:popular)
# # Fetched via GET "/users/popular"
- def get(path, attrs={}) # {{{
+ def get(path, attrs={})
path = "#{build_request_path(attrs)}/#{path}" if path.is_a?(Symbol)
get_raw(path, attrs) do |parsed_data|
if parsed_data[:data].is_a?(Array)
@@ -43,32 +43,32 @@ def get(path, attrs={}) # {{{
new(parsed_data[:data].merge :_metadata => parsed_data[:data], :_errors => parsed_data[:errors])
end
end
- end # }}}
+ end
# Make a GET request and return the parsed JSON response (not mapped to objects)
- def get_raw(path, attrs={}, &block) # {{{
+ def get_raw(path, attrs={}, &block)
path = "#{build_request_path(attrs)}/#{path}" if path.is_a?(Symbol)
request(attrs.merge(:_method => :get, :_path => path), &block)
- end # }}}
+ end
# Make a GET request and return a collection of resources
- def get_collection(path, attrs={}) # {{{
+ def get_collection(path, attrs={})
path = "#{build_request_path(attrs)}/#{path}" if path.is_a?(Symbol)
get_raw(path, attrs) do |parsed_data|
new_collection(parsed_data)
end
- end # }}}
+ end
# Make a GET request and return a collection of resources
- def get_resource(path, attrs={}) # {{{
+ def get_resource(path, attrs={})
path = "#{build_request_path(attrs)}/#{path}" if path.is_a?(Symbol)
get_raw(path, attrs) do |parsed_data|
new(parsed_data[:data].merge :_metadata => parsed_data[:data], :_errors => parsed_data[:errors])
end
- end # }}}
+ end
# Make a POST request and return either a collection or a resource
- def post(path, attrs={}) # {{{
+ def post(path, attrs={})
path = "#{build_request_path(attrs)}/#{path}" if path.is_a?(Symbol)
post_raw(path, attrs) do |parsed_data|
if parsed_data[:data].is_a?(Array)
@@ -77,32 +77,32 @@ def post(path, attrs={}) # {{{
new(parsed_data[:data].merge :_metadata => parsed_data[:data], :_errors => parsed_data[:errors])
end
end
- end # }}}
+ end
# Make a POST request and return the parsed JSON response (not mapped to objects)
- def post_raw(path, attrs={}, &block) # {{{
+ def post_raw(path, attrs={}, &block)
path = "#{build_request_path(attrs)}/#{path}" if path.is_a?(Symbol)
request(attrs.merge(:_method => :post, :_path => path), &block)
- end # }}}
+ end
# Make a POST request and return a collection of resources
- def post_collection(path, attrs={}) # {{{
+ def post_collection(path, attrs={})
path = "#{build_request_path(attrs)}/#{path}" if path.is_a?(Symbol)
post_raw(path, attrs) do |parsed_data|
new_collection(parsed_data)
end
- end # }}}
+ end
# Make a POST request and return a collection of resources
- def post_resource(path, attrs={}) # {{{
+ def post_resource(path, attrs={})
path = "#{build_request_path(attrs)}/#{path}" if path.is_a?(Symbol)
post_raw(path, attrs) do |parsed_data|
new(parsed_data[:data])
end
- end # }}}
+ end
# Make a PUT request and return either a collection or a resource
- def put(path, attrs={}) # {{{
+ def put(path, attrs={})
path = "#{build_request_path(attrs)}/#{path}" if path.is_a?(Symbol)
put_raw(path, attrs) do |parsed_data|
if parsed_data[:data].is_a?(Array)
@@ -111,32 +111,32 @@ def put(path, attrs={}) # {{{
new(parsed_data[:data].merge :_metadata => parsed_data[:data], :_errors => parsed_data[:errors])
end
end
- end # }}}
+ end
# Make a PUT request and return the parsed JSON response (not mapped to objects)
- def put_raw(path, attrs={}, &block) # {{{
+ def put_raw(path, attrs={}, &block)
path = "#{build_request_path(attrs)}/#{path}" if path.is_a?(Symbol)
request(attrs.merge(:_method => :put, :_path => path), &block)
- end # }}}
+ end
# Make a PUT request and return a collection of resources
- def put_collection(path, attrs={}) # {{{
+ def put_collection(path, attrs={})
path = "#{build_request_path(attrs)}/#{path}" if path.is_a?(Symbol)
put_raw(path, attrs) do |parsed_data|
new_collection(parsed_data)
end
- end # }}}
+ end
# Make a PUT request and return a collection of resources
- def put_resource(path, attrs={}) # {{{
+ def put_resource(path, attrs={})
path = "#{build_request_path(attrs)}/#{path}" if path.is_a?(Symbol)
put_raw(path, attrs) do |parsed_data|
new(parsed_data[:data].merge :_metadata => parsed_data[:data], :_errors => parsed_data[:errors])
end
- end # }}}
+ end
# Make a PATCH request and return either a collection or a resource
- def patch(path, attrs={}) # {{{
+ def patch(path, attrs={})
path = "#{build_request_path(attrs)}/#{path}" if path.is_a?(Symbol)
patch_raw(path, attrs) do |parsed_data|
if parsed_data[:data].is_a?(Array)
@@ -145,32 +145,32 @@ def patch(path, attrs={}) # {{{
new(parsed_data[:data].merge :_metadata => parsed_data[:data], :_errors => parsed_data[:errors])
end
end
- end # }}}
+ end
# Make a PATCH request and return the parsed JSON response (not mapped to objects)
- def patch_raw(path, attrs={}, &block) # {{{
+ def patch_raw(path, attrs={}, &block)
path = "#{build_request_path(attrs)}/#{path}" if path.is_a?(Symbol)
request(attrs.merge(:_method => :patch, :_path => path), &block)
- end # }}}
+ end
# Make a PATCH request and return a collection of resources
- def patch_collection(path, attrs={}) # {{{
+ def patch_collection(path, attrs={})
path = "#{build_request_path(attrs)}/#{path}" if path.is_a?(Symbol)
patch_raw(path, attrs) do |parsed_data|
new_collection(parsed_data)
end
- end # }}}
+ end
# Make a PATCH request and return a collection of resources
- def patch_resource(path, attrs={}) # {{{
+ def patch_resource(path, attrs={})
path = "#{build_request_path(attrs)}/#{path}" if path.is_a?(Symbol)
patch_raw(path, attrs) do |parsed_data|
new(parsed_data[:data].merge :_metadata => parsed_data[:data], :_errors => parsed_data[:errors])
end
- end # }}}
+ end
# Make a DELETE request and return either a collection or a resource
- def delete(path, attrs={}) # {{{
+ def delete(path, attrs={})
path = "#{build_request_path(attrs)}/#{path}" if path.is_a?(Symbol)
delete_raw(path, attrs) do |parsed_data|
if parsed_data[:data].is_a?(Array)
@@ -179,29 +179,29 @@ def delete(path, attrs={}) # {{{
new(parsed_data[:data].merge :_metadata => parsed_data[:data], :_errors => parsed_data[:errors])
end
end
- end # }}}
+ end
# Make a DELETE request and return the parsed JSON response (not mapped to objects)
- def delete_raw(path, attrs={}, &block) # {{{
+ def delete_raw(path, attrs={}, &block)
path = "#{build_request_path(attrs)}/#{path}" if path.is_a?(Symbol)
request(attrs.merge(:_method => :delete, :_path => path), &block)
- end # }}}
+ end
# Make a DELETE request and return a collection of resources
- def delete_collection(path, attrs={}) # {{{
+ def delete_collection(path, attrs={})
path = "#{build_request_path(attrs)}/#{path}" if path.is_a?(Symbol)
delete_raw(path, attrs) do |parsed_data|
new_collection(parsed_data)
end
- end # }}}
+ end
# Make a DELETE request and return a collection of resources
- def delete_resource(path, attrs={}) # {{{
+ def delete_resource(path, attrs={})
path = "#{build_request_path(attrs)}/#{path}" if path.is_a?(Symbol)
delete_raw(path, attrs) do |parsed_data|
new(parsed_data[:data].merge :_metadata => parsed_data[:data], :_errors => parsed_data[:errors])
end
- end # }}}
+ end
# Define custom GET requests
#
@@ -213,54 +213,54 @@ def delete_resource(path, attrs={}) # {{{
#
# User.popular
# # Fetched from GET "/users/popular"
- def custom_get(*paths) # {{{
+ def custom_get(*paths)
metaclass = (class << self; self; end)
paths.each do |path|
metaclass.send(:define_method, path.to_sym) do |*attrs|
get(path, attrs.first || Hash.new)
end
end
- end # }}}
+ end
# Define custom POST requests
- def custom_post(*paths) # {{{
+ def custom_post(*paths)
metaclass = (class << self; self; end)
paths.each do |path|
metaclass.send(:define_method, path.to_sym) do |*attrs|
post(path, attrs.first || Hash.new)
end
end
- end # }}}
+ end
# Define custom PUT requests
- def custom_put(*paths) # {{{
+ def custom_put(*paths)
metaclass = (class << self; self; end)
paths.each do |path|
metaclass.send(:define_method, path.to_sym) do |*attrs|
put(path, attrs.first || Hash.new)
end
end
- end # }}}
+ end
# Define custom PATCH requests
- def custom_patch(*paths) # {{{
+ def custom_patch(*paths)
metaclass = (class << self; self; end)
paths.each do |path|
metaclass.send(:define_method, path.to_sym) do |*attrs|
patch(path, attrs.first || Hash.new)
end
end
- end # }}}
+ end
# Define custom DELETE requests
- def custom_delete(*paths) # {{{
+ def custom_delete(*paths)
metaclass = (class << self; self; end)
paths.each do |path|
metaclass.send(:define_method, path.to_sym) do |*attrs|
delete(path, attrs.first || Hash.new)
end
end
- end # }}}
+ end
end
end
end
View
20 lib/her/model/introspection.rb
@@ -11,46 +11,46 @@ module Introspection
#
# @user = User.find(1)
# p @user # => #<User(/users/1) id=1 name="Tobias Fünke">
- def inspect # {{{
+ def inspect
"#<#{self.class}(#{request_path}) #{@data.inject([]) { |memo, item| key, value = item; memo << "#{key}=#{attribute_for_inspect(value)}"}.join(" ")}>"
- end # }}}
+ end
private
# @private
- def attribute_for_inspect(value) # {{{
+ def attribute_for_inspect(value)
if value.is_a?(String) && value.length > 50
"#{value[0..50]}...".inspect
elsif value.is_a?(Date) || value.is_a?(Time)
%("#{value}")
else
value.inspect
end
- end # }}}
+ end
module ClassMethods
# Finds a class at the same level as this one or at the global level.
# @private
- def nearby_class(name) # {{{
+ def nearby_class(name)
sibling_class(name) || name.constantize rescue nil
- end # }}}
+ end
protected
# Looks for a class at the same level as this one with the given name.
# @private
- def sibling_class(name) # {{{
+ def sibling_class(name)
if mod = self.containing_module
@sibling_class ||= {}
@sibling_class[mod] ||= {}
@sibling_class[mod][name] ||= "#{mod.name}::#{name}".constantize rescue nil
end
- end # }}}
+ end
# If available, returns the containing Module for this class.
# @private
- def containing_module # {{{
+ def containing_module
return unless self.name =~ /::/
self.name.split("::")[0..-2].join("::").constantize
- end # }}}
+ end
end
end
end
View
87 lib/her/model/orm.rb
@@ -6,8 +6,7 @@ module ORM
attr_accessor :data, :metadata, :errors
# Initialize a new object with data received from an HTTP request
- # @private
- def initialize(params={}) # {{{
+ def initialize(params={})
@data = {}
@metadata = params.delete(:_metadata) || {}
@errors = params.delete(:_errors) || {}
@@ -17,18 +16,19 @@ def initialize(params={}) # {{{
unset_data = Her::Model::ORM.use_setter_methods(self, params)
parsed_data = self.class.parse_relationships(unset_data)
@data.update(parsed_data)
- end # }}}
+ end
# Initialize a collection of resources
# @private
- def self.initialize_collection(klass, parsed_data={}) # {{{
+ def self.initialize_collection(klass, parsed_data={})
collection_data = parsed_data[:data].map { |item_data| klass.new(item_data) }
Her::Collection.new(collection_data, parsed_data[:metadata], parsed_data[:errors])
- end # }}}
+ end
# Use setter methods of model for each key / value pair in params
# Return key / value pairs for which no setter method was defined on the model
- def self.use_setter_methods(model, params) # {{{
+ # @private
+ def self.use_setter_methods(model, params)
setter_method_names = model.class.setter_method_names
params.inject({}) do |memo, (key, value)|
setter_method = key.to_s + '='
@@ -39,11 +39,11 @@ def self.use_setter_methods(model, params) # {{{
end
memo
end
- end # }}}
+ end
# Handles missing methods by routing them through @data
# @private
- def method_missing(method, *args, &blk) # {{{
+ def method_missing(method, *args, &blk)
if method.to_s.end_with?('=')
@data[method.to_s.chomp('=').to_sym] = args.first
elsif method.to_s.end_with?('?')
@@ -53,49 +53,49 @@ def method_missing(method, *args, &blk) # {{{
else
super
end
- end # }}}
+ end
# Handles returning true for the cases handled by method_missing
- def respond_to?(method, include_private = false) # {{{
+ def respond_to?(method, include_private = false)
method.to_s.end_with?('=') || method.to_s.end_with?('?') || @data.include?(method) || super
- end # }}}
+ end
# Override the method to prevent from returning the object ID (in ruby-1.8.7)
# @private
- def id # {{{
+ def id
@data[:id] || super
- end # }}}
+ end
# Return `true` if a resource was not saved yet
- def new? # {{{
+ def new?
!@data.include?(:id)
- end # }}}
+ end
# Return `true` if a resource does not contain errors
- def valid? # {{{
+ def valid?
@errors.empty?
- end # }}}
+ end
# Return `true` if a resource contains errors
- def invalid? # {{{
+ def invalid?
@errors.any?
- end # }}}
+ end
# Return `true` if the other object is also a Her::Model and has matching data
- def ==(other) # {{{
+ def ==(other)
other.is_a?(Her::Model) && @data == other.data
- end # }}}
+ end
# Delegate to the == method
- def eql?(other) # {{{
+ def eql?(other)
self == other
- end # }}}
+ end
# Delegate to @data, allowing models to act correctly in code like:
# [ Model.find(1), Model.find(1) ].uniq # => [ Model.find(1) ]
- def hash # {{{
+ def hash
@data.hash
- end # }}}
+ end
# Save a resource
#
@@ -110,7 +110,7 @@ def hash # {{{
# @user = User.new({ :fullname => "Tobias Fünke" })
# @user.save
# # Called via POST "/users"
- def save # {{{
+ def save
params = to_params
resource = self
@@ -133,15 +133,15 @@ def save # {{{
end
self
- end # }}}
+ end
# Destroy a resource
#
# @example
# @user = User.find(1)
# @user.destroy
# # Called via DELETE "/users/1"
- def destroy # {{{
+ def destroy
resource = self
self.class.wrap_in_hooks(resource, :destroy) do |resource, klass|
klass.request(:_method => :delete, :_path => "#{request_path}") do |parsed_data|
@@ -151,24 +151,24 @@ def destroy # {{{
end
end
self
- end # }}}
+ end
# Convert into a hash of request parameters
#
# @example
# @user.to_params
# # => { :id => 1, :name => 'John Smith' }
- def to_params # {{{
+ def to_params
@data.dup
- end # }}}
+ end
module ClassMethods
# Initialize a collection of resources with raw data from an HTTP request
#
# @param [Array] parsed_data
- def new_collection(parsed_data) # {{{
+ def new_collection(parsed_data)
Her::Model::ORM.initialize_collection(self, parsed_data)
- end # }}}
+ end
# Fetch specific resource(s) by their ID
#
@@ -179,7 +179,7 @@ def new_collection(parsed_data) # {{{
# @example
# @users = User.find([1, 2])
# # Fetched via GET "/users/1" and GET "/users/2"
- def find(*ids) # {{{
+ def find(*ids)
params = ids.last.is_a?(Hash) ? ids.pop : {}
results = ids.flatten.compact.uniq.map do |id|
request(params.merge(:_method => :get, :_path => "#{build_request_path(params.merge(:id => id))}")) do |parsed_data|
@@ -191,25 +191,25 @@ def find(*ids) # {{{
else
results.first
end
- end # }}}
+ end
# Fetch a collection of resources
#
# @example
# @users = User.all
# # Fetched via GET "/users"
- def all(params={}) # {{{
+ def all(params={})
request(params.merge(:_method => :get, :_path => "#{build_request_path(params)}")) do |parsed_data|
new_collection(parsed_data)
end
- end # }}}
+ end
# Create a resource and return it
#
# @example
# @user = User.create({ :fullname => "Tobias Fünke" })
# # Called via POST "/users/1"
- def create(params={}) # {{{
+ def create(params={})
resource = new(params)
wrap_in_hooks(resource, :create, :save) do |resource, klass|
params = resource.to_params
@@ -222,29 +222,30 @@ def create(params={}) # {{{
end
end
resource
- end # }}}
+ end
# Save an existing resource and return it
#
# @example
# @user = User.save_existing(1, { :fullname => "Tobias Fünke" })
# # Called via PUT "/users/1"
- def save_existing(id, params) # {{{
+ def save_existing(id, params)
resource = new(params.merge(:id => id))
resource.save
- end # }}}
+ end
# Destroy an existing resource
#
# @example
# User.destroy_existing(1)
# # Called via DELETE "/users/1"
- def destroy_existing(id, params={}) # {{{
+ def destroy_existing(id, params={})
request(params.merge(:_method => :delete, :_path => "#{build_request_path(params.merge(:id => id))}")) do |parsed_data|
new(parsed_data[:data])
end
- end # }}}
+ end
+ # @private
def setter_method_names
@setter_method_names ||= instance_methods.inject(Set.new) do |memo, method_name|
memo << method_name.to_s if method_name.to_s.end_with?('=')
View
16 lib/her/model/paths.rb
@@ -11,9 +11,9 @@ module Paths
# end
#
# User.find(1) # Fetched via GET /utilisateurs/1
- def request_path # {{{
+ def request_path
self.class.build_request_path(@data.dup)
- end # }}}
+ end
module ClassMethods
# Defines a custom collection path for the resource
@@ -23,15 +23,15 @@ module ClassMethods
# include Her::Model
# collection_path "/users"
# end
- def collection_path(path=nil) # {{{
+ def collection_path(path=nil)
@her_collection_path ||= begin
superclass.collection_path.dup if superclass.respond_to?(:collection_path)
end
return @her_collection_path unless path
@her_resource_path = "#{path}/:id"
@her_collection_path = path
- end # }}}
+ end
# Defines a custom resource path for the resource
#
@@ -40,14 +40,14 @@ def collection_path(path=nil) # {{{
# include Her::Model
# resource_path "/users/:id"
# end
- def resource_path(path=nil) # {{{
+ def resource_path(path=nil)
@her_resource_path ||= begin
superclass.resource_path.dup if superclass.respond_to?(:resource_path)
end
return @her_resource_path unless path
@her_resource_path = path
- end # }}}
+ end
# Return a custom path based on the collection path and variable parameters
#
@@ -58,7 +58,7 @@ def resource_path(path=nil) # {{{
# end
#
# User.all # Fetched via GET /utilisateurs
- def build_request_path(path=nil, parameters={}) # {{{
+ def build_request_path(path=nil, parameters={})
unless path.is_a?(String)
parameters = path || {}
path = parameters.include?(:id) ? resource_path : collection_path
@@ -68,7 +68,7 @@ def build_request_path(path=nil, parameters={}) # {{{
# Look for :key or :_key, otherwise raise an exception
parameters.delete($1.to_sym) || parameters.delete("_#{$1}".to_sym) || raise(Her::Errors::PathError.new("Missing :_#{$1} parameter to build the request path (#{path})."))
end
- end # }}}
+ end
end
end
end
View
26 lib/her/model/relationships.rb
@@ -4,20 +4,22 @@ module Model
module Relationships
# Return @her_relationships, lazily initialized with copy of the
# superclass' her_relationships, or an empty hash.
+ #
# @private
- def relationships # {{{
+ def relationships
@her_relationships ||= begin
if superclass.respond_to?(:relationships)
superclass.relationships.dup
else
{}
end
end
- end # }}}
+ end
# Parse relationships data after initializing a new object
+ #
# @private
- def parse_relationships(data) # {{{
+ def parse_relationships(data)
relationships.each_pair do |type, definitions|
definitions.each do |relationship|
name = relationship[:name]
@@ -34,7 +36,7 @@ def parse_relationships(data) # {{{
end
end
data
- end # }}}
+ end
# Define an *has_many* relationship.
#
@@ -54,7 +56,7 @@ def parse_relationships(data) # {{{
# @user = User.find(1)
# @user.articles # => [#<Article(articles/2) id=2 title="Hello world.">]
# # Fetched via GET "/users/1/articles"
- def has_many(name, attrs={}) # {{{
+ def has_many(name, attrs={})
attrs = {
:class_name => name.to_s.classify,
:name => name,
@@ -71,7 +73,7 @@ def has_many(name, attrs={}) # {{{
@data[name] ||= klass.get_collection("#{self.class.build_request_path(:id => id)}#{attrs[:path]}")
end
end
- end # }}}
+ end
# Define an *has_one* relationship.
#
@@ -91,7 +93,7 @@ def has_many(name, attrs={}) # {{{
# @user = User.find(1)
# @user.organization # => #<Organization(organizations/2) id=2 name="Foobar Inc.">
# # Fetched via GET "/users/1/organization"
- def has_one(name, attrs={}) # {{{
+ def has_one(name, attrs={})
attrs = {
:class_name => name.to_s.classify,
:name => name,
@@ -108,7 +110,7 @@ def has_one(name, attrs={}) # {{{
@data[name] ||= klass.get_resource("#{self.class.build_request_path(:id => id)}#{attrs[:path]}")
end
end
- end # }}}
+ end
# Define a *belongs_to* relationship.
#
@@ -128,7 +130,7 @@ def has_one(name, attrs={}) # {{{
# @user = User.find(1)
# @user.team # => #<Team(teams/2) id=2 name="Developers">
# # Fetched via GET "/teams/2"
- def belongs_to(name, attrs={}) # {{{
+ def belongs_to(name, attrs={})
attrs = {
:class_name => name.to_s.classify,
:name => name,
@@ -146,17 +148,17 @@ def belongs_to(name, attrs={}) # {{{
@data[name] ||= klass.get_resource("#{klass.build_request_path(:id => @data[attrs[:foreign_key].to_sym])}")
end
end
- end # }}}
+ end
# @private
- def relationship_accessor(type, attrs) # {{{
+ def relationship_accessor(type, attrs)
name = attrs[:name]
class_name = attrs[:class_name]
define_method(name) do
klass = self.class.nearby_class(attrs[:class_name])
@data[name] ||= klass.get_resource("#{klass.build_request_path(attrs[:path], :id => @data[attrs[:foreign_key].to_sym])}")
end
- end # }}}
+ end
end
end
end
View
32 spec/api_spec.rb
@@ -4,20 +4,20 @@
describe Her::API do
context "initialization" do
describe ".setup" do
- it "creates a default connection" do # {{{
+ it "creates a default connection" do
Her::API.setup :url => "https://api.example.com"
Her::API.default_api.base_uri.should == "https://api.example.com"
- end # }}}
+ end
end
describe "#setup" do
- it "sets a base URI" do # {{{
+ it "sets a base URI" do
@api = Her::API.new
@api.setup :url => "https://api.example.com"
@api.base_uri.should == "https://api.example.com"
- end # }}}
+ end
- it "sets custom middleware with #use" do # {{{
+ it "sets custom middleware with #use" do
class Foo; end;
class Bar; end;
@@ -27,17 +27,17 @@ class Bar; end;
builder.use Bar
end
@api.connection.builder.handlers.should == [Foo, Bar]
- end # }}}
+ end
- it "takes custom options" do # {{{
+ it "takes custom options" do
@api = Her::API.new
@api.setup :foo => { :bar => "baz" }, :url => "https://api.example.com"
@api.options.should == { :foo => { :bar => "baz" }, :url => "https://api.example.com" }
- end # }}}
+ end
end
describe "#request" do
- it "makes HTTP requests" do # {{{
+ it "makes HTTP requests" do
class SimpleParser < Faraday::Response::Middleware
def on_complete(env)
env[:body] = { :data => env[:body] }
@@ -55,9 +55,9 @@ def on_complete(env)
parsed_data = @api.request(:_method => :get, :_path => "/foo")
parsed_data[:data] == "Foo, it is."
- end # }}}
+ end
- it "makes HTTP requests while specifying custom HTTP headers" do # {{{
+ it "makes HTTP requests while specifying custom HTTP headers" do
class SimpleParser < Faraday::Response::Middleware
def on_complete(env)
env[:body] = { :data => env[:body] }
@@ -75,9 +75,9 @@ def on_complete(env)
parsed_data = @api.request(:_method => :get, :_path => "/foo", :_headers => { "X-Page" => 2 })
parsed_data[:data] == "Foo, it is page 2."
- end # }}}
+ end
- it "parses a request with the default parser" do # {{{
+ it "parses a request with the default parser" do
@api = Her::API.new
@api.setup :url => "https://api.example.com" do |builder|
builder.use Her::Middleware::FirstLevelParseJSON
@@ -90,9 +90,9 @@ def on_complete(env)
parsed_data[:data].should == { :id => 1, :name => "George Michael Bluth" }
parsed_data[:errors].should == ["This is a single error"]
parsed_data[:metadata].should == { :page => 1, :per_page => 10 }
- end # }}}
+ end
- it "parses a request with a custom parser" do # {{{
+ it "parses a request with a custom parser" do
class CustomParser < Faraday::Response::Middleware
def on_complete(env)
json = MultiJson.load(env[:body], :symbolize_keys => true)
@@ -118,7 +118,7 @@ def on_complete(env)
parsed_data[:data].should == { :id => 1, :name => "George Michael Bluth" }
parsed_data[:errors].should == []
parsed_data[:metadata].should == {}
- end # }}}
+ end
end
end
end
View
4 spec/middleware/accept_json_spec.rb
@@ -2,9 +2,9 @@
require "spec_helper"
describe Her::Middleware::AcceptJSON do
- it "adds an Accept header" do # {{{
+ it "adds an Accept header" do
described_class.new.add_header({}).tap do |headers|
headers["Accept"].should == "application/json"
end
- end # }}}
+ end
end
View
8 spec/middleware/first_level_parse_json_spec.rb
@@ -5,21 +5,21 @@
subject { described_class.new }
let(:body) { "{\"id\": 1, \"name\": \"Tobias Fünke\", \"errors\": 2, \"metadata\": 3}" }
- it "parses body as json" do # {{{
+ it "parses body as json" do
subject.parse(body).tap do |json|
json[:data].should == { :id => 1, :name => "Tobias Fünke" }
json[:errors].should == 2
json[:metadata].should == 3
end
- end # }}}
+ end
- it "parses :body key as json in the env hash" do # {{{
+ it "parses :body key as json in the env hash" do
env = { :body => body }
subject.on_complete(env)
env[:body].tap do |json|
json[:data].should == { :id => 1, :name => "Tobias Fünke" }
json[:errors].should == 2
json[:metadata].should == 3
end
- end # }}}
+ end
end
View
8 spec/middleware/second_level_parse_json_spec.rb
@@ -5,21 +5,21 @@
subject { described_class.new }
let(:body) { "{\"data\": 1, \"errors\": 2, \"metadata\": 3}" }
- it "parses body as json" do # {{{
+ it "parses body as json" do
subject.parse(body).tap do |json|
json[:data].should == 1
json[:errors].should == 2
json[:metadata].should == 3
end
- end # }}}
+ end
- it "parses :body key as json in the env hash" do # {{{
+ it "parses :body key as json in the env hash" do
env = { :body => body }
subject.on_complete(env)
env[:body].tap do |json|
json[:data].should == 1
json[:errors].should == 2
json[:metadata].should == 3
end
- end # }}}
+ end
end
View
132 spec/model/hooks_spec.rb
@@ -3,124 +3,124 @@
describe Her::Model::Hooks do
context "adding hooks to a model" do
- before do # {{{
+ before do
spawn_model "Foo::User"
- end # }}}
+ end
describe "method hooks" do
- it "handles “before save” method hooks" do # {{{
+ it "handles “before save” method hooks" do
Foo::User.before_save :set_internal_id
Foo::User.hooks[:before_save].length.should == 1
Foo::User.hooks[:before_save].first.class.should == Symbol
- end # }}}
+ end
- it "handles “before create” method hooks" do # {{{
+ it "handles “before create” method hooks" do
Foo::User.before_create :set_internal_id
Foo::User.hooks[:before_create].length.should == 1
Foo::User.hooks[:before_create].first.class.should == Symbol
- end # }}}
+ end
- it "handles “before update” method hooks" do # {{{
+ it "handles “before update” method hooks" do
Foo::User.before_update :set_internal_id
Foo::User.hooks[:before_update].length.should == 1
Foo::User.hooks[:before_update].first.class.should == Symbol
- end # }}}
+ end
- it "handles “before destroy” method hooks" do # {{{
+ it "handles “before destroy” method hooks" do
Foo::User.before_destroy :set_internal_id
Foo::User.hooks[:before_destroy].length.should == 1
Foo::User.hooks[:before_destroy].first.class.should == Symbol
- end # }}}
+ end
- it "handles “after save” method hooks" do # {{{
+ it "handles “after save” method hooks" do
Foo::User.after_save :set_internal_id
Foo::User.hooks[:after_save].length.should == 1
Foo::User.hooks[:after_save].first.class.should == Symbol
- end # }}}
+ end
- it "handles “after create” method hooks" do # {{{
+ it "handles “after create” method hooks" do
Foo::User.after_create :set_internal_id
Foo::User.hooks[:after_create].length.should == 1
Foo::User.hooks[:after_create].first.class.should == Symbol
- end # }}}
+ end
- it "handles “after update” method hooks" do # {{{
+ it "handles “after update” method hooks" do
Foo::User.after_update :set_internal_id
Foo::User.hooks[:after_update].length.should == 1
Foo::User.hooks[:after_update].first.class.should == Symbol
- end # }}}
+ end
- it "handles “after destroy” method hooks" do # {{{
+ it "handles “after destroy” method hooks" do
Foo::User.after_destroy :set_internal_id
Foo::User.hooks[:after_destroy].length.should == 1
Foo::User.hooks[:after_destroy].first.class.should == Symbol
- end # }}}
+ end
end
describe "block hooks" do
- it "handles “before save” block hooks" do # {{{
+ it "handles “before save” block hooks" do
Foo::User.before_save { |record| record.internal_id = 42 }
Foo::User.hooks[:before_save].length.should == 1
Foo::User.hooks[:before_save].first.class.should == Proc
- end # }}}
+ end
- it "handles “before create” block hooks" do # {{{
+ it "handles “before create” block hooks" do
Foo::User.before_create { |record| record.internal_id = 42 }
Foo::User.hooks[:before_create].length.should == 1
Foo::User.hooks[:before_create].first.class.should == Proc
- end # }}}
+ end
- it "handles “before update” block hooks" do # {{{
+ it "handles “before update” block hooks" do
Foo::User.before_update { |record| record.internal_id = 42 }
Foo::User.hooks[:before_update].length.should == 1
Foo::User.hooks[:before_update].first.class.should == Proc
- end # }}}
+ end
- it "handles “before destroy” block hooks" do # {{{
+ it "handles “before destroy” block hooks" do
Foo::User.before_destroy { |record| record.internal_id = 42 }
Foo::User.hooks[:before_destroy].length.should == 1
Foo::User.hooks[:before_destroy].first.class.should == Proc
- end # }}}
+ end
- it "handles “after save” block hooks" do # {{{
+ it "handles “after save” block hooks" do
Foo::User.after_save { |record| record.internal_id = 42 }
Foo::User.hooks[:after_save].length.should == 1
Foo::User.hooks[:after_save].first.class.should == Proc
- end # }}}
+ end
- it "handles “after create” block hooks" do # {{{
+ it "handles “after create” block hooks" do
Foo::User.after_create { |record| record.internal_id = 42 }
Foo::User.hooks[:after_create].length.should == 1
Foo::User.hooks[:after_create].first.class.should == Proc
- end # }}}
+ end
- it "handles “after update” block hooks" do # {{{
+ it "handles “after update” block hooks" do
Foo::User.after_update { |record| record.internal_id = 42 }
Foo::User.hooks[:after_update].length.should == 1
Foo::User.hooks[:after_update].first.class.should == Proc
- end # }}}
+ end
- it "handles “after destroy” block hooks" do # {{{
+ it "handles “after destroy” block hooks" do
Foo::User.after_destroy { |record| record.internal_id = 42 }
Foo::User.hooks[:after_destroy].length.should == 1
Foo::User.hooks[:after_destroy].first.class.should == Proc
- end # }}}
+ end
end
context "inheriting hooks from a superclass" do
- it "copies hooks to the subclass" do # {{{
+ it "copies hooks to the subclass" do
Foo::User.before_save :set_internal_id
Foo::User.after_create { |record| record.internal_id = 42 }
subclass = Class.new(Foo::User)
subclass.hooks.object_id.should_not == Foo::User.hooks.object_id
subclass.hooks[:before_save].should == [:set_internal_id]
subclass.hooks[:after_create].length.should == 1
- end # }}}
+ end
end
end
context "perform hooks on a model" do
- before do # {{{
+ before do
Her::API.setup :url => "https://api.example.com" do |builder|
builder.use Her::Middleware::FirstLevelParseJSON
builder.use Faraday::Request::UrlEncoded
@@ -146,10 +146,10 @@ def change_internal_after_create_id; @internal_after_create_id = 101; end
def change_internal_after_update_id; @internal_after_update_id = 102; end
def change_internal_after_destroy_id; @internal_after_destroy_id = 103; end
end
- end # }}}
+ end
describe "method hooks" do
- before do # {{{
+ before do
Foo::User.before_save :change_internal_save_id
Foo::User.before_update :change_internal_update_id
Foo::User.before_create :change_internal_create_id
@@ -159,66 +159,66 @@ def change_internal_after_destroy_id; @internal_after_destroy_id = 103; end
Foo::User.after_update :change_internal_after_update_id
Foo::User.after_create :change_internal_after_create_id
Foo::User.after_destroy :change_internal_after_destroy_id
- end # }}}
+ end
- it "perform “before save” “before create” method hook on Model#save without an ID" do # {{{
+ it "perform “before save” “before create” method hook on Model#save without an ID" do
@user = Foo::User.new(:fullname => "Tobias Fünke")
@user.save
@user.internal_save_id.should == 100
@user.internal_create_id.should == 101
@user.internal_update_id.should == nil
- end # }}}
+ end
- it "perform “before save” and “before update” method hook on Model#save with an ID" do # {{{
+ it "perform “before save” and “before update” method hook on Model#save with an ID" do
@user = Foo::User.find(1)
@user.save
@user.internal_save_id.should == 100
@user.internal_create_id.should == nil
@user.internal_update_id.should == 102
- end # }}}
+ end
- it "perform “before destroy” method hook on Model#destroy" do # {{{
+ it "perform “before destroy” method hook on Model#destroy" do
@user = Foo::User.find(1)
@user.destroy
@user.internal_save_id.should == nil
@user.internal_create_id.should == nil
@user.internal_update_id.should == nil
@user.internal_destroy_id.should == 103
- end # }}}
+ end
- it "perform “after save” “after create” method hook on Model#save without an ID" do # {{{
+ it "perform “after save” “after create” method hook on Model#save without an ID" do
@user = Foo::User.new(:fullname => "Tobias Fünke")
@user.save
@user.internal_after_save_id.should == 100
@user.internal_after_create_id.should == 101
@user.internal_after_update_id.should == nil
- end # }}}
+ end
- it "perform “after save” “after update” method hook on Model#save with an ID" do # {{{
+ it "perform “after save” “after update” method hook on Model#save with an ID" do
@user = Foo::User.find(1)
@user.save
@user.internal_after_save_id.should == 100
@user.internal_after_create_id.should == nil
@user.internal_after_update_id.should == 102
- end # }}}
+ end
- it "perform “after save” “after update” method hook on Model.save_existing" do # {{{
+ it "perform “after save” “after update” method hook on Model.save_existing" do
@user = Foo::User.save_existing(1, { :fullname => "Tobias Fünke" })
@user.internal_after_save_id.should == 100
@user.internal_after_create_id.should == nil
@user.internal_after_update_id.should == 102
- end # }}}
+ end
- it "perform “after save” “after create” method hook on Model.create" do # {{{
+ it "perform “after save” “after create” method hook on Model.create" do
@user = Foo::User.create({ :fullname => "Tobias Fünke" })
@user.internal_after_save_id.should == 100
@user.internal_after_create_id.should == 101
@user.internal_after_update_id.should == nil
- end # }}}
+ end
end
describe "block hooks" do
- before do # {{{
+ before do
Foo::User.before_save { |record| record.internal_save_id = 200 }
Foo::User.before_create { |record| record.internal_create_id = 201 }
Foo::User.before_update { |record| record.internal_update_id = 202 }
@@ -228,48 +228,48 @@ def change_internal_after_destroy_id; @internal_after_destroy_id = 103; end
Foo::User.after_create { |record| record.internal_after_create_id = 201 }
Foo::User.after_update { |record| record.internal_after_update_id = 202 }
Foo::User.after_destroy { |record| record.internal_after_destroy_id = 203 }
- end # }}}
+ end
- it "perform “before save” and “before create” block hook on Model#save without an ID" do # {{{
+ it "perform “before save” and “before create” block hook on Model#save without an ID" do
@user = Foo::User.new(:fullname => "Tobias Fünke")
@user.save
@user.internal_save_id.should == 200
@user.internal_create_id.should == 201
@user.internal_update_id.should == nil
- end # }}}
+ end
- it "perform “before save” and “before update” block hook on Model#save with an ID" do # {{{
+ it "perform “before save” and “before update” block hook on Model#save with an ID" do
@user = Foo::User.find(1)
@user.save
@user.internal_save_id.should == 200
@user.internal_create_id.should == nil
@user.internal_update_id.should == 202
- end # }}}
+ end
- it "perform “before destroy” block hook on Model#destroy" do # {{{
+ it "perform “before destroy” block hook on Model#destroy" do
@user = Foo::User.find(1)
@user.destroy
@user.internal_save_id.should == nil
@user.internal_create_id.should == nil
@user.internal_update_id.should == nil
@user.internal_destroy_id.should == 203
- end # }}}
+ end
- it "perform “after save” “after create” block hook on Model#save without an ID" do # {{{
+ it "perform “after save” “after create” block hook on Model#save without an ID" do
@user = Foo::User.new(:fullname => "Tobias Fünke")
@user.save
@user.internal_after_save_id.should == 200
@user.internal_after_create_id.should == 201
@user.internal_after_update_id.should == nil
- end # }}}
+ end
- it "perform “after save” “after update” block hook on Model#save with an ID" do # {{{
+ it "perform “after save” “after update” block hook on Model#save with an ID" do
@user = Foo::User.find(1)
@user.save
@user.internal_after_save_id.should == 200
@user.internal_after_create_id.should == nil
@user.internal_after_update_id.should == 202
- end # }}}
+ end
end
end
end
View
112 spec/model/http_spec.rb
@@ -3,26 +3,26 @@
describe Her::Model::HTTP do
context "binding a model with an API" do
- it "binds a model to an instance of Her::API" do # {{{
+ it "binds a model to an instance of Her::API" do
api = Her::API.new
api.setup :url => "https://api.example.com"
spawn_model "Foo::User"
Foo::User.uses_api api
Foo::User.her_api.should_not == nil
Foo::User.her_api.base_uri.should == "https://api.example.com"
- end # }}}
+ end
- it "binds a model directly to Her::API" do # {{{
+ it "binds a model directly to Her::API" do
Her::API.setup :url => "https://api.example.com"
spawn_model "Foo::User"
Foo::User.her_api.should_not == nil
Foo::User.her_api.base_uri.should == "https://api.example.com"
- end # }}}
+ end
- it "binds two models to two different instances of Her::API" do # {{{
+ it "binds two models to two different instances of Her::API" do
api1 = Her::API.new
api1.setup :url => "https://api1.example.com" do |builder|
builder.use Her::Middleware::FirstLevelParseJSON
@@ -42,9 +42,9 @@
spawn_model "Foo::Comment"
Foo::Comment.uses_api api2
Foo::Comment.her_api.base_uri.should == "https://api2.example.com"
- end # }}}
+ end
- it "binds one model to Her::API and another one to an instance of Her::API" do # {{{
+ it "binds one model to Her::API and another one to an instance of Her::API" do
Her::API.setup :url => "https://api1.example.com" do |builder|
builder.use Her::Middleware::FirstLevelParseJSON
builder.use Faraday::Request::UrlEncoded
@@ -63,9 +63,9 @@
spawn_model "Foo::Comment"
Foo::Comment.uses_api api
Foo::Comment.her_api.base_uri.should == "https://api2.example.com"
- end # }}}
+ end
- it "binds a a model to it's superclass' her_api" do # {{{
+ it "binds a a model to it's superclass' her_api" do
api = Her::API.new
api.setup :url => "http://api.example.com" do |builder|
builder.use Her::Middleware::FirstLevelParseJSON
@@ -78,9 +78,9 @@
Foo::Subclass = Class.new(Foo::Superclass)
Foo::Subclass.her_api.should == Foo::Superclass.her_api
- end # }}}
+ end
- it "allows subclasses to change her_api without changing the parent class' her_api" do # {{{
+ it "allows subclasses to change her_api without changing the parent class' her_api" do
api1 = Her::API.new
api1.setup :url => "http://api.example.com" do |builder|
builder.use Her::Middleware::FirstLevelParseJSON
@@ -102,11 +102,11 @@
end
Foo::Subclass.her_api.should_not == Foo::Superclass.her_api
- end # }}}
+ end
end
context "making HTTP requests" do
- before do # {{{
+ before do
Her::API.setup :url => "https://api.example.com" do |builder|
builder.use Her::Middleware::FirstLevelParseJSON
builder.use Faraday::Request::UrlEncoded
@@ -128,109 +128,109 @@
end
spawn_model "Foo::User"
- end # }}}
+ end
- it "handles GET wrapper method" do # {{{
+ it "handles GET wrapper method" do
@users = Foo::User.get(:popular)
@users.length.should == 2
@users.first.id.should == 1
@user = Foo::User.get(:"1")
@user.id.should == 1
- end # }}}
+ end
- it "handles raw GET with a block" do # {{{
+ it "handles raw GET with a block" do
Foo::User.get_raw("/users") do |parsed_data|
parsed_data[:data].should == [{ :id => 1 }]
end
- end # }}}
+ end
- it "handles raw GET with return value" do # {{{
+ it "handles raw GET with return value" do
parsed_data = Foo::User.get_raw("/users")
parsed_data[:data].should == [{ :id => 1 }]
- end # }}}
+ end
- it "handles raw POST with a block" do # {{{
+ it "handles raw POST with a block" do
Foo::User.post_raw("/users") do |parsed_data|
parsed_data[:data].should == [{ :id => 3 }]
end
- end # }}}
+ end
- it "handles raw POST with return value" do # {{{
+ it "handles raw POST with return value" do
parsed_data = Foo::User.post_raw("/users")
parsed_data[:data].should == [{ :id => 3 }]
- end # }}}
+ end
- it "handles raw PUT with a block" do # {{{
+ it "handles raw PUT with a block" do
Foo::User.put_raw("/users/4") do |parsed_data|
parsed_data[:data].should == [{ :id => 4 }]
end
- end # }}}
+ end
- it "handles raw PUT with return value" do # {{{
+ it "handles raw PUT with return value" do
parsed_data = Foo::User.put_raw("/users/4")
parsed_data[:data].should == [{ :id => 4 }]
- end # }}}
+ end
- it "handles raw PATCH with a block" do # {{{
+ it "handles raw PATCH with a block" do
Foo::User.patch_raw("/users/6") do |parsed_data|
parsed_data[:data].should == [{ :id => 6 }]
end
- end # }}}
+ end
- it "handles raw PATCH with return value" do # {{{
+ it "handles raw PATCH with return value" do
parsed_data = Foo::User.patch_raw("/users/6")
parsed_data[:data].should == [{ :id => 6 }]
- end # }}}
+ end
- it "handles raw DELETE with a block" do # {{{
+ it "handles raw DELETE with a block" do
Foo::User.delete_raw("/users/5") do |parsed_data|
parsed_data[:data].should == [{ :id => 5 }]
end
- end # }}}
+ end
- it "handles raw DELETE with return value" do # {{{
+ it "handles raw DELETE with return value" do
parsed_data = Foo::User.delete_raw("/users/5")
parsed_data[:data].should == [{ :id => 5 }]
- end # }}}
+ end
- it "handles querystring parameters" do # {{{
+ it "handles querystring parameters" do
Foo::User.get_raw("/users", :page => 2) do |parsed_data|
parsed_data[:data].should == [{ :id => 2 }]
end
- end # }}}
+ end
- it "handles GET collection" do # {{{
+ it "handles GET collection" do
@users = Foo::User.get_collection("/users/popular")
@users.length.should == 2
@users.first.id.should == 1
- end # }}}
+ end
- it "handles GET resource" do # {{{
+ it "handles GET resource" do
@user = Foo::User.get_resource("/users/1")
@user.id.should == 1
- end # }}}
+ end
- it "handles GET collection through a symbol" do # {{{
+ it "handles GET collection through a symbol" do
@users = Foo::User.get_collection(:popular)
@users.length.should == 2
@users.first.id.should == 1
- end # }}}
+ end
- it "handles GET resource through a symbol" do # {{{
+ it "handles GET resource through a symbol" do
@user = Foo::User.get_resource(:"1")
@user.id.should == 1
- end # }}}
+ end
- it "handles raw GET through a symbol" do # {{{
+ it "handles raw GET through a symbol" do
Foo::User.get_raw(:popular) do |parsed_data|
parsed_data[:data].should == [{ :id => 1 }, { :id => 2 }]
end
- end # }}}
+ end
end
context "setting custom requests" do
- before do # {{{
+ before do
Her::API.setup :url => "https://api.example.com" do |builder|
builder.use Her::Middleware::FirstLevelParseJSON
builder.use Faraday::Request::UrlEncoded
@@ -243,23 +243,23 @@
spawn_model "Foo::User"
Foo::User.custom_get :popular, :foobar
Foo::User.custom_post :from_default
- end # }}}
+ end
- it "handles custom methods" do # {{{
+ it "handles custom methods" do
Foo::User.respond_to?(:popular).should be_true
Foo::User.respond_to?(:foobar).should be_true
Foo::User.respond_to?(:from_default).should be_true
- end # }}}
+ end
- it "handles custom GET requests" do # {{{
+ it "handles custom GET requests" do
@users = Foo::User.popular
@users.length.should == 2
@users.first.id.should == 1
- end # }}}
+ end
- it "handles custom POST requests" do # {{{
+ it "handles custom POST requests" do
@user = Foo::User.from_default(:name => "Tobias Fünke")
@user.id.should be_true
- end # }}}
+ end
end
end
View
24 spec/model/introspection_spec.rb
@@ -3,7 +3,7 @@
describe Her::Model::Introspection do
context "introspecting a resource" do
- before do # {{{
+ before do
Her::API.setup :url => "https://api.example.com" do |builder|
builder.use Her::Middleware::FirstLevelParseJSON
builder.use Faraday::Request::UrlEncoded
@@ -16,36 +16,36 @@
end
spawn_model "Foo::User"
- end # }}}
+ end
describe "#inspect" do
- it "outputs resource attributs for an existing resource" do # {{{
+ it "outputs resource attributs for an existing resource" do
@user = Foo::User.find(1)
["#<Foo::User(users/1) name=\"Tobias Funke\" id=1>", "#<Foo::User(users/1) id=1 name=\"Tobias Funke\">"].should include(@user.inspect)
- end # }}}
+ end
- it "outputs resource attributs for an not-saved-yet resource" do # {{{
+ it "outputs resource attributs for an not-saved-yet resource" do
@user = Foo::User.new(:name => "Tobias Funke")
@user.inspect.should == "#<Foo::User(users) name=\"Tobias Funke\">"
- end # }}}
+ end
end
end
describe "#nearby_class" do
- context "for a class inside of a module" do # {{{
- before do # {{{
+ context "for a class inside of a module" do
+ before do
spawn_model "Foo::User"
spawn_model "Foo::AccessRecord"
spawn_model "AccessRecord"
spawn_model "Log"
- end # }}}
+ end
- it "returns a sibling class, if found" do # {{{
+ it "returns a sibling class, if found" do
Foo::User.nearby_class("AccessRecord").should == Foo::AccessRecord
AccessRecord.nearby_class("Log").should == Log
Foo::User.nearby_class("Log").should == Log
Foo::User.nearby_class("X").should be_nil
- end # }}}
- end # }}}
+ end
+ end
end
end
View
196 spec/model/orm_spec.rb
@@ -3,7 +3,7 @@
describe Her::Model::ORM do
context "mapping data to Ruby objects" do
- before do # {{{
+ before do
api = Her::API.new
api.setup :url => "https://api.example.com" do |builder|
builder.use Her::Middleware::FirstLevelParseJSON
@@ -22,68 +22,68 @@
spawn_model "Foo::AdminUser" do
uses_api api
end
- end # }}}
+ end
- it "maps a single resource to a Ruby object" do # {{{
+ it "maps a single resource to a Ruby object" do
@user = Foo::User.find(1)
@user.id.should == 1
@user.name.should == "Tobias Fünke"
- end # }}}
+ end
- it "maps a collection of resources to an array of Ruby objects" do # {{{
+ it "maps a collection of resources to an array of Ruby objects" do
@users = Foo::User.all
@users.length.should == 2
@users.first.name.should == "Tobias Fünke"
@users = Foo::AdminUser.all
@users.length.should == 2
@users.first.name.should == "Tobias Fünke"
- end # }}}
+ end
- it "handles new resource" do # {{{
+ it "handles new resource" do
@new_user = Foo::User.new(:fullname => "Tobias Fünke")
@new_user.new?.should be_true
@new_user.fullname.should == "Tobias Fünke"
@existing_user = Foo::User.find(1)
@existing_user.new?.should be_false
- end # }}}
+ end
- it "handles method missing for getter" do# {{{
+ it "handles method missing for getter" do
@new_user = Foo::User.new(:fullname => 'Mayonegg')
lambda { @new_user.unknown_method_for_a_user }.should raise_error(NoMethodError)
expect { @new_user.fullname }.to_not raise_error(NoMethodError)
- end# }}}
+ end
- it "handles method missing for setter" do# {{{
+ it "handles method missing for setter" do
@new_user = Foo::User.new
expect { @new_user.fullname = "Tobias Fünke" }.to_not raise_error(NoMethodError)
- end# }}}
+ end
- it "handles method missing for query" do#{{{
+ it "handles method missing for query" do
@new_user = Foo::User.new
expect { @new_user.fullname? }.to_not raise_error(NoMethodError)
- end# }}}
+ end
- it "handles respond_to for getter" do#{{{
+ it "handles respond_to for getter" do
@new_user = Foo::User.new(:fullname => 'Mayonegg')
@new_user.should_not respond_to(:unknown_method_for_a_user)
@new_user.should respond_to(:fullname)
- end#}}}
+ end