apiarist 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ YWMwYzdkMTU3YTM2M2NhNTE3ZTZiY2YwYWZjNjBmNDIxNmU4ZDVhZQ==
5
+ data.tar.gz: !binary |-
6
+ OGQ2ZWM3MzhhYTBmODdiM2NiNTY2MmM3MDMzZmQ0ZjAzNGZhYTQ1OQ==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ NjgxMzNmNmVjNWZlNTBjNWNhYzljNmEwNWIyMDJlZjg3ODZkOTJkOTM3Yjhl
10
+ OGYzMDgzOGI0NDhiMGFlZTdlZTBkYWIyNTZjNjNmMGFiNTFkZDhhNmE2NWZj
11
+ NDlhODc4NmE2ODQ4NjE0ZWRjYWNlOTI0YzZkYTg2ZjkxMzA2MzM=
12
+ data.tar.gz: !binary |-
13
+ MTI3NzQwMTY2MDAyYmY5ZjM3OWIyNTIxZTAyNDA0NmMyOTJkOWRlYzUwMTE2
14
+ ZTkzMmYxZGEzNzZhMjg1MzFmMmRmMGFhNzI0YWQ1MjhhYjEyMzhmMTI3ZTgy
15
+ ZjhlNDVkZmM3ODg0YjlhMWExM2E2OGJiYjY1ZmZjZWM3YWU4OWY=
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Victor Martinez
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,66 @@
1
+ # Apiarist
2
+
3
+ Apiarist is an opinionated [ActiveModel::Serializers](https://github.com/rails-api/active_model_serializers) alternative
4
+
5
+ ## Why?
6
+
7
+ * Built on the top of [inherited_resources](https://github.com/josevalim/inherited_resources). Creating an API is as easy as defining your resource representations.
8
+ * Serializers inherit controller's namespace making it easier to version and maintain your API.
9
+ * Serialization scopes: allows you to define different representations of the same resource.
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ gem 'apiarist'
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install apiarist
24
+
25
+ ## Usage
26
+
27
+ ~~~ruby
28
+ # app/controllers/api/v1/products_controller.rb
29
+ class Api::V1::Products < Apiarist::ResourceController::Base
30
+ actions :index, :show
31
+ belongs_to :category
32
+
33
+ serialization_scope :basic, only: :index
34
+ serialization_scope :extended, only: :show
35
+ end
36
+
37
+ # app/serializers/api/v1/category_serializer.rb
38
+ class Api::V1::CategorySerializer < Apiarist::Serializer
39
+ attributes :id, :name
40
+ end
41
+
42
+ # app/serializers/api/v1/product_serializer.rb
43
+ class Api::V1::ProductSerializer < Apiarist::Serializer
44
+ attributes :id, :name, :description
45
+
46
+ scope :basic do
47
+ attribute :category_name
48
+ end
49
+
50
+ scope :extended do
51
+ association :category
52
+ end
53
+
54
+ def category_name
55
+ category.name
56
+ end
57
+ end
58
+ ~~~
59
+
60
+ ## Contributing
61
+
62
+ 1. Fork it
63
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
64
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
65
+ 4. Push to the branch (`git push origin my-new-feature`)
66
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'apiarist/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "apiarist"
8
+ spec.version = Apiarist::VERSION
9
+ spec.authors = ["Victor Martinez"]
10
+ spec.email = ["knoopx@gmail.com"]
11
+ spec.description = %q{An opinionated ActiveModel::Serializers alternative}
12
+ spec.summary = %q{An opinionated ActiveModel::Serializers alternative}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency 'activesupport', '>= 3.0'
22
+ spec.add_dependency 'actionpack', '>= 3.0'
23
+ spec.add_dependency 'inherited_resources', '>= 1.3.1'
24
+ spec.add_development_dependency "bundler", "~> 1.3"
25
+ spec.add_development_dependency "rake"
26
+ end
@@ -0,0 +1,8 @@
1
+ module Apiarist
2
+ autoload :AbstractController, 'apiarist/abstract_controller'
3
+ autoload :Controller, 'apiarist/controller'
4
+ autoload :ResourceController, 'apiarist/resource_controller'
5
+ autoload :Serialization, 'apiarist/serialization'
6
+ autoload :Serializer, 'apiarist/serializer'
7
+ autoload :VERSION, 'apiarist/version'
8
+ end
@@ -0,0 +1,30 @@
1
+ require 'action_controller/metal'
2
+
3
+ module Apiarist
4
+ class AbstractController < ActionController::Metal
5
+ SKIP_MODULES = [:Cookies, :Flash, :RequestForgeryProtection, :HttpAuthentication]
6
+
7
+ abstract!
8
+
9
+ module Compatibility
10
+ def cache_store; end
11
+ def cache_store=(*); end
12
+ def assets_dir=(*); end
13
+ def javascripts_dir=(*); end
14
+ def stylesheets_dir=(*); end
15
+ def page_cache_directory=(*); end
16
+ def asset_path=(*); end
17
+ def asset_host=(*); end
18
+ def relative_url_root=(*); end
19
+ def perform_caching=(*); end
20
+ def helpers_path=(*); end
21
+ def allow_forgery_protection=(*); end
22
+ def helper_method(*); end
23
+ def helper(*); end
24
+ end
25
+
26
+ extend Compatibility
27
+ ActionController::Base.without_modules(SKIP_MODULES).each { |mod| include mod }
28
+ ActiveSupport.run_load_hooks(:action_controller, self)
29
+ end
30
+ end
@@ -0,0 +1,6 @@
1
+ module Apiarist
2
+ module Controller
3
+ autoload :Base, 'apiarist/controller/base'
4
+ autoload :Serialization, 'apiarist/controller/serialization'
5
+ end
6
+ end
@@ -0,0 +1,9 @@
1
+ module Apiarist
2
+ module Controller
3
+ class Base < Apiarist::AbstractController
4
+ respond_to :json
5
+
6
+ include Apiarist::Controller::Serialization
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,25 @@
1
+ require 'active_support/concern'
2
+
3
+ module Apiarist
4
+ module Controller
5
+ module Serialization
6
+ extend ActiveSupport::Concern
7
+ include Apiarist::Serialization
8
+
9
+ included do
10
+ class_attribute :_serialization_scope
11
+ self._serialization_scope = :default
12
+ end
13
+
14
+ def serialization_context
15
+ self
16
+ end
17
+
18
+ module ClassMethods
19
+ def serialization_scope(name, opts = {})
20
+ before_filter(opts) { self._serialization_scope = name }
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,6 @@
1
+ module Apiarist
2
+ module ResourceController
3
+ autoload :Base, 'apiarist/resource_controller/base'
4
+ autoload :Responder, 'apiarist/resource_controller/responder'
5
+ end
6
+ end
@@ -0,0 +1,12 @@
1
+ require 'inherited_resources/base'
2
+
3
+ module Apiarist
4
+ module ResourceController
5
+ class Base < Apiarist::Controller::Base
6
+ InheritedResources::Base.inherit_resources(self)
7
+
8
+ respond_to :json
9
+ self.responder = Responder
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,20 @@
1
+ require 'action_controller/metal/responder'
2
+ require 'active_support/core_ext/module/delegation'
3
+
4
+ module Apiarist
5
+ module ResourceController
6
+ class Responder < ActionController::Responder
7
+ delegate :serialize, :resource_class, :_serialization_scope, to: :controller
8
+
9
+ def display(resource, options = {})
10
+ if resource.respond_to?(:each)
11
+ root = resource_class.name.underscore.pluralize
12
+ else
13
+ root = resource_class.name.underscore
14
+ end
15
+
16
+ super({root => serialize(resource, scope: _serialization_scope)}, options)
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,25 @@
1
+ require 'active_support/core_ext/hash/keys'
2
+ require 'active_support/core_ext/string/inflections'
3
+
4
+ module Apiarist
5
+ module Serialization
6
+ def serialize(obj, opts = {})
7
+ if obj.respond_to?(:map)
8
+ obj.map { |resource| serialize_object(resource, opts) }
9
+ else
10
+ serialize_object(obj, opts)
11
+ end
12
+ end
13
+
14
+ private
15
+
16
+ def serialize_object(obj, opts = {})
17
+ opts.assert_valid_keys(:scope, :context, :serializer)
18
+ obj && opts.fetch(:serializer, serializer_class(obj.class)).new(obj, serialization_context, opts)
19
+ end
20
+
21
+ def serializer_class(resource_class)
22
+ "#{self.class.name.deconstantize}::#{resource_class}Serializer".constantize
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,79 @@
1
+ require 'delegate'
2
+ require 'active_support/core_ext/array/extract_options'
3
+
4
+ module Apiarist
5
+ class Serializer < SimpleDelegator
6
+ include Apiarist::Serialization
7
+
8
+ attr_reader :context
9
+
10
+ def initialize(resource, context = self, opts = {})
11
+ @resource = resource, @context = context
12
+ @options = opts
13
+ super(resource)
14
+ end
15
+
16
+ alias_method :resource, :__getobj__
17
+
18
+ def as_json(_ = {})
19
+ json = {}
20
+ _keys.each_with_object(json) do |(name, opts), hash|
21
+ serialization_opts = opts.dup
22
+ key = serialization_opts.delete(:as) || name
23
+
24
+ if serialization_scope_include?(serialization_opts.delete(:if))
25
+ case serialization_opts.delete(:type)
26
+ when :association
27
+ value = serialize(send(name), serialization_opts)
28
+ else
29
+ value = send(name)
30
+ end
31
+
32
+ hash[key] = value
33
+ end
34
+ end
35
+
36
+ json
37
+ end
38
+
39
+ def serialization_context
40
+ context.serialization_context
41
+ end
42
+
43
+ class << self
44
+ def attribute(name, opts = {})
45
+ self._keys = _keys.dup
46
+ _keys[name] = opts
47
+ end
48
+
49
+ def attributes(*names)
50
+ opts = names.extract_options!
51
+ names.each { |name| attribute(name, opts) }
52
+ end
53
+
54
+ def association(name, opts = {})
55
+ attribute(name, opts.merge(type: :association))
56
+ end
57
+
58
+ def associations(*associations)
59
+ opts = associations.extract_options!
60
+ associations.each { |name| association(name, opts) }
61
+ end
62
+
63
+ def scope(name, &block)
64
+ with_options(if: name) do |scope|
65
+ scope.instance_eval(&block)
66
+ end
67
+ end
68
+ end
69
+
70
+ private
71
+
72
+ class_attribute :_keys
73
+ self._keys = {}
74
+
75
+ def serialization_scope_include?(scope)
76
+ [@options[:scope], nil].flatten.include?(scope)
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,3 @@
1
+ module Apiarist
2
+ VERSION = '0.0.1'
3
+ end
metadata ADDED
@@ -0,0 +1,131 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: apiarist
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Victor Martinez
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-05-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ! '>='
17
+ - !ruby/object:Gem::Version
18
+ version: '3.0'
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ! '>='
22
+ - !ruby/object:Gem::Version
23
+ version: '3.0'
24
+ type: :runtime
25
+ prerelease: false
26
+ name: activesupport
27
+ - !ruby/object:Gem::Dependency
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '3.0'
33
+ version_requirements: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '3.0'
38
+ type: :runtime
39
+ prerelease: false
40
+ name: actionpack
41
+ - !ruby/object:Gem::Dependency
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: 1.3.1
47
+ version_requirements: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ! '>='
50
+ - !ruby/object:Gem::Version
51
+ version: 1.3.1
52
+ type: :runtime
53
+ prerelease: false
54
+ name: inherited_resources
55
+ - !ruby/object:Gem::Dependency
56
+ requirement: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ~>
59
+ - !ruby/object:Gem::Version
60
+ version: '1.3'
61
+ version_requirements: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ~>
64
+ - !ruby/object:Gem::Version
65
+ version: '1.3'
66
+ type: :development
67
+ prerelease: false
68
+ name: bundler
69
+ - !ruby/object:Gem::Dependency
70
+ requirement: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ! '>='
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ version_requirements: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ! '>='
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ type: :development
81
+ prerelease: false
82
+ name: rake
83
+ description: An opinionated ActiveModel::Serializers alternative
84
+ email:
85
+ - knoopx@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - .gitignore
91
+ - Gemfile
92
+ - LICENSE.txt
93
+ - README.md
94
+ - Rakefile
95
+ - apiarist.gemspec
96
+ - lib/apiarist.rb
97
+ - lib/apiarist/abstract_controller.rb
98
+ - lib/apiarist/controller.rb
99
+ - lib/apiarist/controller/base.rb
100
+ - lib/apiarist/controller/serialization.rb
101
+ - lib/apiarist/resource_controller.rb
102
+ - lib/apiarist/resource_controller/base.rb
103
+ - lib/apiarist/resource_controller/responder.rb
104
+ - lib/apiarist/serialization.rb
105
+ - lib/apiarist/serializer.rb
106
+ - lib/apiarist/version.rb
107
+ homepage: ''
108
+ licenses:
109
+ - MIT
110
+ metadata: {}
111
+ post_install_message:
112
+ rdoc_options: []
113
+ require_paths:
114
+ - lib
115
+ required_ruby_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ! '>='
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ required_rubygems_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ! '>='
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ requirements: []
126
+ rubyforge_project:
127
+ rubygems_version: 2.0.3
128
+ signing_key:
129
+ specification_version: 4
130
+ summary: An opinionated ActiveModel::Serializers alternative
131
+ test_files: []