Skip to content

Commit

Permalink
drop ruby 1.8 syntax, rubocop enhancements
Browse files Browse the repository at this point in the history
  • Loading branch information
fabrik42 committed Nov 24, 2016
1 parent c462815 commit 4ae08d3
Show file tree
Hide file tree
Showing 84 changed files with 790 additions and 1,162 deletions.
4 changes: 2 additions & 2 deletions Gemfile
@@ -1,4 +1,4 @@
source "http://rubygems.org"
source 'http://rubygems.org'

# Specify your gem's dependencies in acts_as_api.gemspec
gemspec
Expand All @@ -15,5 +15,5 @@ group :test do
gem 'rspec-rails', '>= 2.5.0'
gem 'webrat'
gem 'responders'
gem 'shared_engine', :path => './spec/shared_engine'
gem 'shared_engine', path: './spec/shared_engine'
end
17 changes: 5 additions & 12 deletions Rakefile
Expand Up @@ -7,13 +7,12 @@ Bundler::GemHelper.install_tasks
RSpec::Core::RakeTask.new

namespace :spec do

supported_orms = %w(mongoid active_record)

supported_orms.each do |orm|
desc "Run #{orm} specs only"
RSpec::Core::RakeTask.new(orm) do |t|
t.rspec_opts = ["--color"]
t.rspec_opts = ['--color']
end

task "prepare_#{orm}" do
Expand All @@ -23,26 +22,20 @@ namespace :spec do
Rake::Task["spec:#{orm}"].prerequisites << "spec:prepare_#{orm}"
end


# task :all => supported_orms.map{|orm| "spec:#{orm}"}

desc "Runs specs for all ORMs (#{supported_orms.join(', ')})"
task :all do
supported_orms.each do |orm|
puts "Starting to run specs for #{orm}..."
system("bundle exec rake spec:#{orm}")
raise "#{orm} failed!" unless $?.exitstatus == 0
raise "#{orm} failed!" unless $?.exitstatus.zero?
end
end

end

task default: 'spec:all'

gemspec = Gem::Specification.load("acts_as_api.gemspec")

task :default => "spec:all"

desc "Generate the gh_pages site"
desc 'Generate the gh_pages site'
task :rocco do
system "bundle exec rocco examples/introduction/index.rb -t examples/introduction/layout.mustache"
system 'bundle exec rocco examples/introduction/index.rb -t examples/introduction/layout.mustache'
end
26 changes: 13 additions & 13 deletions acts_as_api.gemspec
@@ -1,20 +1,20 @@
# encoding: utf-8
$:.push File.expand_path("../lib", __FILE__)
require "acts_as_api/version"
$LOAD_PATH.push File.expand_path('../lib', __FILE__)
require 'acts_as_api/version'

Gem::Specification.new do |s|
s.name = "acts_as_api"
s.name = 'acts_as_api'
s.version = ActsAsApi::VERSION
s.platform = Gem::Platform::RUBY
s.authors = ["Christian Bäuerlein"]
s.email = ["christian@ffwdme.com"]
s.homepage = "https://github.com/fabrik42/acts_as_api"
s.summary = %q{Makes creating XML/JSON responses in Rails 3, 4 and 5 easy and fun.}
s.description = %q{acts_as_api enriches the models and controllers of your app in a rails-like way so you can easily determine how your XML/JSON API responses should look like.}
s.authors = ['Christian Bäuerlein']
s.email = ['christian@ffwdme.com']
s.homepage = 'https://github.com/fabrik42/acts_as_api'
s.summary = 'Makes creating XML/JSON responses in Rails 3, 4 and 5 easy and fun.'
s.description = 'acts_as_api enriches the models and controllers of your app in a rails-like way so you can easily determine how your XML/JSON API responses should look like.'

s.add_dependency('activemodel','>= 3.0.0')
s.add_dependency('activesupport','>= 3.0.0')
s.add_dependency('rack','>= 1.1.0')
s.add_dependency('activemodel', '>= 3.0.0')
s.add_dependency('activesupport', '>= 3.0.0')
s.add_dependency('rack', '>= 1.1.0')

s.add_development_dependency('rails', ['>= 3.2.22.2'])
s.add_development_dependency('mongoid', ['>= 3.0.1'])
Expand All @@ -23,6 +23,6 @@ Gem::Specification.new do |s|

s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.require_paths = ["lib"]
s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
s.require_paths = ['lib']
end
22 changes: 11 additions & 11 deletions lib/acts_as_api.rb
@@ -1,12 +1,12 @@
require 'active_model'
require 'active_support/core_ext/class'

$:.unshift(File.dirname(__FILE__)) unless
$:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
$LOAD_PATH.unshift(File.dirname(__FILE__)) unless
$LOAD_PATH.include?(File.dirname(__FILE__)) || $LOAD_PATH.include?(File.expand_path(File.dirname(__FILE__)))

require "acts_as_api/collection"
require "acts_as_api/rails_renderer"
require "acts_as_api/exceptions"
require 'acts_as_api/collection'
require 'acts_as_api/rails_renderer'
require 'acts_as_api/exceptions'

# acts_as_api is a gem that aims to make the construction of JSON and XML
# responses in Rails 3, 4 and 5 easy and fun.
Expand All @@ -17,12 +17,12 @@
# acts_as_api uses the default serializers of your rails app and doesn't
# force you into more dependencies.
module ActsAsApi
autoload :Config, "acts_as_api/config"
autoload :ApiTemplate, "acts_as_api/api_template"
autoload :Base, "acts_as_api/base"
autoload :Rendering, "acts_as_api/rendering"
autoload :Responder, "acts_as_api/responder"
autoload :Adapters, "acts_as_api/adapters"
autoload :Config, 'acts_as_api/config'
autoload :ApiTemplate, 'acts_as_api/api_template'
autoload :Base, 'acts_as_api/base'
autoload :Rendering, 'acts_as_api/rendering'
autoload :Responder, 'acts_as_api/responder'
autoload :Adapters, 'acts_as_api/adapters'
end

# Neccessary to render an Array of models, e.g. the result of a search.
Expand Down
2 changes: 1 addition & 1 deletion lib/acts_as_api/adapters.rb
@@ -1,5 +1,5 @@
module ActsAsApi
module Adapters
autoload :Mongoid, "acts_as_api/adapters/mongoid"
autoload :Mongoid, 'acts_as_api/adapters/mongoid'
end
end
48 changes: 23 additions & 25 deletions lib/acts_as_api/api_template.rb
Expand Up @@ -8,7 +8,6 @@ module ActsAsApi
# Please note that +ApiTemplate+ inherits from +Hash+ so you can use all
# kind of +Hash+ and +Enumerable+ methods to manipulate the template.
class ApiTemplate < Hash

# The name of the api template as a Symbol.
attr_accessor :api_template

Expand All @@ -21,7 +20,7 @@ def initialize(api_template)

def merge!(other_hash, &block)
super
self.options.merge!(other_hash.options) if other_hash.respond_to?(:options)
options.merge!(other_hash.options) if other_hash.respond_to?(:options)
end

# Adds a field to the api template
Expand All @@ -44,7 +43,7 @@ def add(val, options = {})

# Removes a field from the template
def remove(field)
self.delete(field)
delete(field)
end

# Returns the options of a field in the api template
Expand All @@ -67,9 +66,9 @@ def api_template_for(fieldset, field)
# the response based on the conditional options passed.
def allowed_to_render?(fieldset, field, model, options)
return true unless fieldset.is_a? ActsAsApi::ApiTemplate

fieldset_options = fieldset.options_for(field)

if fieldset_options[:unless]
!(condition_fulfilled?(model, fieldset_options[:unless], options))
elsif fieldset_options[:if]
Expand Down Expand Up @@ -112,28 +111,27 @@ def to_response_hash(model, fieldset = self, options = {})
api_output
end

private

def process_value(model, value, options)
case value
when Symbol
model.send(value)
when Proc
call_proc(value,model,options)
when String
value.split('.').inject(model) { |result, method| result.send(method) }
when Hash
to_response_hash(model, value)
private

def process_value(model, value, options)
case value
when Symbol
model.send(value)
when Proc
call_proc(value, model, options)
when String
value.split('.').inject(model) { |result, method| result.send(method) }
when Hash
to_response_hash(model, value)
end
end
end

def call_proc(the_proc,model,options)
if the_proc.arity == 2
the_proc.call(model, options)
else
the_proc.call(model)
def call_proc(the_proc, model, options)
if the_proc.arity == 2
the_proc.call(model, options)
else
the_proc.call(model)
end
end
end

end
end
31 changes: 11 additions & 20 deletions lib/acts_as_api/base.rb
Expand Up @@ -10,7 +10,6 @@ def acts_as_api?
# When invoked, it enriches the current model with the
# class and instance methods to act as api.
def acts_as_api

class_eval do
include ActsAsApi::Base::InstanceMethods
extend ActsAsApi::Base::ClassMethods
Expand All @@ -19,23 +18,19 @@ def acts_as_api
if block_given?
yield ActsAsApi::Config
end

end

module ClassMethods

def acts_as_api?#:nodoc:
self.included_modules.include?(InstanceMethods)
def acts_as_api? #:nodoc:
included_modules.include?(InstanceMethods)
end

# Determines the attributes, methods of the model that are accessible in the api response.
# *Note*: There is only whitelisting for api accessible attributes.
# So once the model acts as api, you have to determine all attributes here that should
# be contained in the api responses.
def api_accessible(api_template, options = {}, &block)

attributes = api_accessible_attributes(api_template).try(:dup) || ApiTemplate.new(api_template)

attributes.merge!(api_accessible_attributes(options[:extend])) if options[:extend]

if block_given?
Expand All @@ -53,12 +48,11 @@ def api_accessible_attributes(api_template)
end

module InstanceMethods

# Creates the api response of the model and returns it as a Hash.
# Will raise an exception if the passed api template is not defined for the model
def as_api_response(api_template, options = {})
api_attributes = self.class.api_accessible_attributes(api_template)
raise ActsAsApi::TemplateNotFoundError.new("acts_as_api template :#{api_template.to_s} was not found for model #{self.class}") if api_attributes.nil?
raise ActsAsApi::TemplateNotFoundError.new("acts_as_api template :#{api_template} was not found for model #{self.class}") if api_attributes.nil?

before_api_response(api_template)
response_hash = around_api_response(api_template) do
Expand All @@ -69,20 +63,17 @@ def as_api_response(api_template, options = {})
response_hash
end

protected

def before_api_response(api_remplate)
end
protected

def after_api_response(api_remplate)
end
def before_api_response(_api_template)
end

def around_api_response(api_remplate)
yield
end
def after_api_response(_api_template)
end

def around_api_response(_api_template)
yield
end
end

end

end
3 changes: 1 addition & 2 deletions lib/acts_as_api/collection.rb
Expand Up @@ -6,12 +6,11 @@ module Collection
def as_api_response(api_template, options = {})
collect do |item|
if item.respond_to?(:as_api_response)
item.as_api_response(api_template,options)
item.as_api_response(api_template, options)
else
item
end
end
end

end
end
5 changes: 0 additions & 5 deletions lib/acts_as_api/config.rb
@@ -1,9 +1,6 @@
module ActsAsApi

module Config

class << self

attr_writer :accepted_api_formats, :dasherize_for, :include_root_in_json_collections, :add_root_node_for, :default_root, :allow_jsonp_callback, :add_http_status_to_jsonp_response

# The accepted response formats
Expand Down Expand Up @@ -52,7 +49,5 @@ def add_http_status_to_jsonp_response
@add_http_status_to_jsonp_response.nil? ? true : @add_http_status_to_jsonp_response
end
end

end

end
4 changes: 2 additions & 2 deletions lib/acts_as_api/exceptions.rb
@@ -1,4 +1,4 @@
module ActsAsApi
class ActsAsApiError < RuntimeError; end
class TemplateNotFoundError < ActsAsApiError; end
end
class TemplateNotFoundError < ActsAsApiError; end
end
2 changes: 0 additions & 2 deletions lib/acts_as_api/rails_renderer.rb
Expand Up @@ -3,7 +3,6 @@ module ActsAsApi
#
# See ActsAsApi::Config about the possible configurations
module RailsRenderer

def self.setup
ActionController.add_renderer :acts_as_api_jsonp do |json, options|
json = ActiveSupport::JSON.encode(json) unless json.respond_to?(:to_str)
Expand All @@ -16,6 +15,5 @@ def self.setup
self.response_body = json
end
end

end
end

0 comments on commit 4ae08d3

Please sign in to comment.