render_json_rails 0.1.3

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,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: b82eabebc8a9b20c855e8f75f77fce6bf92105cd07de76f5e377a935114bb6b1
4
+ data.tar.gz: 175b1a4817a38e2c768c5b8827fe0d020043721d6998d73c9c0fafe063479cbf
5
+ SHA512:
6
+ metadata.gz: 2dfaad84541f4d4e07e1dd4b16695ab06ae8345f768ac1defe288d188f3e4771a82d5d56812d3d14198d9ab1d66de84fc8138e7a5a9ff228993e680da1546c41
7
+ data.tar.gz: a7d110349b9fd3e84366676f42963914cc74cd071e9af54da9181bf3761e786516405427331bea4a648c14844b085f505ec488371ad2c81c5981b8980e4aef8d
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in render_json_rails.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "minitest", "~> 5.0"
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Marcin
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,51 @@
1
+ # RenderJsonRails
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/render_json_rails`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'render_json_rails', git: 'https://github.com/intum/render_json_rails'
13
+
14
+ # or
15
+
16
+ source "https://rubygems.pkg.github.com/intum" do
17
+ gem "render_json_rails"
18
+ end
19
+ ```
20
+
21
+ And then execute:
22
+
23
+ $ bundle install
24
+
25
+ Or install it yourself as:
26
+
27
+ $ gem install render_json_rails
28
+
29
+ ## Usage
30
+
31
+ TODO: Write usage instructions here
32
+
33
+ ## Development
34
+
35
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
36
+
37
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
38
+
39
+ ## Contributing
40
+
41
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/render_json_rails.
42
+
43
+
44
+ Tworzenie gema
45
+
46
+ ```
47
+ gem build render_json_rails.gemspec
48
+
49
+ gem push --key github --host https://rubygems.pkg.github.com/intum render_json_rails-0.1.XXX.gem
50
+ ```
51
+
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "render_json_rails"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,7 @@
1
+ require "render_json_rails/version"
2
+ require "render_json_rails/helper"
3
+ require "render_json_rails/concern"
4
+
5
+ module RenderJsonRails
6
+ class Error < StandardError; end
7
+ end
@@ -0,0 +1,95 @@
1
+ module RenderJsonRails
2
+ module Concern
3
+ extend ActiveSupport::Concern
4
+
5
+ # http://marcin.xxx.test/api/organize/questions/2.json?formatted=yes&include=last_answers,last_answers.user&fields[question]=id&fields[user]=email,login,get_name
6
+ # http://marcin.xxx.test/api/organize/questions/2.json?formatted=yes&include=last_answers,team,team.questions
7
+
8
+ # http://marcin.yyy.test/xx/55200174.json?formatted=yes&mass_fiscal_print=yes
9
+ # http://marcin.yyy.test/xx/55200174.json?formatted=yes&fields[invoice_position]=id,fiscal_short_name
10
+ # http://marcin.yyy.test/yy/uu.json?formatted=yes&fields[payment]=id
11
+ # http://marcin.yyy.test/yy/uu.json?formatted=yes&fields[payment]=id&include=invoices,payment_connections
12
+
13
+ class_methods do
14
+ # Metoda przygotowuje parametry `options` do generowania json-a
15
+ # `methods` to dodatkowe metody które domyślnie będą wyświetlane w jsonie
16
+ # `allowed_methods` definiuje metody, które możemy wymienić w `fields` i wtedy
17
+ # zostaną one wyświelone w json-ie
18
+ # TODO:
19
+ # [ ] spradzanie czy parametry "fields" i "include" sa ok i jesli nie to error
20
+ def default_json_options(name:, fields: nil, except: nil, methods: nil, allowed_methods: nil)
21
+ # name ||= self.name.underscore.gsub('/', '_')
22
+ # raise self.name.underscore.gsub('/', '_')
23
+ except ||= [:account_id, :agent, :ip]
24
+
25
+ options = {}
26
+ if fields && fields[name].present?
27
+ options[:only] = fields[name].split(',').find_all{ |el| !except.include?(el.to_sym) }
28
+ options[:methods] = methods&.find_all{ |el| options[:only].include?(el.to_s) }
29
+ if allowed_methods
30
+ options[:methods] = (options[:methods] || []) | allowed_methods.find_all{ |el| options[:only].include?(el.to_s) }
31
+ end
32
+ else
33
+ options[:except] = except
34
+ options[:methods] = methods
35
+ end
36
+ options
37
+ end
38
+
39
+ def render_json_config(config)
40
+ @render_json_config = config
41
+ # @render_json_config[:methods] = [:image]
42
+ end
43
+
44
+ # rubocop:disable Lint/UnusedMethodArgument
45
+ def render_json_options(includes: nil, fields: nil, additional_config: nil)
46
+ raise "należy skonfigurowac render_json metodą: render_json_config" if !defined?(@render_json_config)
47
+
48
+ options = default_json_options(
49
+ name: @render_json_config[:name].to_s,
50
+ fields: fields,
51
+ except: @render_json_config[:except],
52
+ methods: @render_json_config[:methods],
53
+ allowed_methods: @render_json_config[:allowed_methods]
54
+ )
55
+
56
+ if includes
57
+ include_options = []
58
+ @render_json_config[:includes].each do |name, klass|
59
+ if includes.include?(name.to_s)
60
+ includes2 = RenderJsonRails::Concern.includes_for_model(includes: includes, model: name.to_s)
61
+ # raise includes2.inspect + ' ' + includes.inspect
62
+ include_options << { name => klass.render_json_options(includes: includes2, fields: fields) }
63
+ end
64
+ end if @render_json_config[:includes]
65
+
66
+ # if includes.include?('questions')
67
+ # includes2 = RenderJsonRails::Concern.includes_for_model(includes: includes, model: 'questions')
68
+ # # raise includes2.inspect + ' ' + includes.inspect
69
+ # include_options << { questions: Organize::Question.render_json_options(includes: includes2, fields: fields) }
70
+ # end
71
+ options[:include] = include_options
72
+ end
73
+
74
+ options
75
+ end # render_json_options
76
+ # rubocop:enable Lint/UnusedMethodArgument
77
+
78
+
79
+ end # class_methods
80
+
81
+ def self.includes_for_model(includes:, model:)
82
+ # include.split(',')
83
+ # includes.delete(to_remove)
84
+ includes = includes.map do |el|
85
+ if el.start_with?(model + '.')
86
+ el = el.gsub(/^#{model}\./, '')
87
+ else
88
+ el = nil
89
+ end
90
+ end
91
+ includes.find_all{ |el| el.present? }
92
+ # raise includes.to_json
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,47 @@
1
+ module RenderJsonRails
2
+ module Helper
3
+ # https://jsonapi.org/format/#fetching-sparse-fieldsets
4
+ # https://jsonapi.org/examples/
5
+ # https://jsonapi-resources.com/v0.10/guide/serializer.html#include
6
+ # http://xxx.yyyy.test/banking/object1/758449.json?formatted=1&fields[payment]=id,price&fields[invoice]=price_gross&fields[payment_connection]=amount&include=payment_connections,connectable
7
+ # parametry:
8
+ # formatted=1
9
+ # fields[payment]=id,price
10
+ # fields[invoice]=price_gross
11
+ # fields[payment_connection]=amount&
12
+ # include=payment_connections,connectable
13
+
14
+ # http://xxx.yyyy.test/object.json?formatted=yes&fields[invoice]=number,sales_code&fields[invoice_position]=price_gross&include=positions
15
+ # parametry:
16
+ # formatted=yes&
17
+ # fields[invoice]=number,sales_code
18
+ # fields[invoice_position]=price_gross
19
+ # include=positions
20
+ def render_json(object, additional_config: nil, status: nil, location: nil)
21
+ raise "objekt nie moze byc null" if object == nil
22
+
23
+ if object.class.to_s.include?('ActiveRecord_Relation')
24
+ return render json: [] if !object[0]
25
+
26
+ class_object = object[0].class
27
+ else
28
+ class_object = object.class
29
+ end
30
+ includes = params[:include].to_s.split(',').map{ |el| el.to_s.strip } if params[:include]
31
+ options = class_object.render_json_options(
32
+ includes: includes,
33
+ fields: params[:fields],
34
+ additional_config: additional_config
35
+ )
36
+ if params[:formatted] && !Rails.env.development? || params[:formatted] != 'no' && Rails.env.development?
37
+ json = JSON.pretty_generate(object.as_json(options))
38
+ render json: json, status: status, location: location
39
+ else
40
+ options[:json] = object
41
+ options[:status] = status
42
+ options[:location] = location
43
+ render options
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,3 @@
1
+ module RenderJsonRails
2
+ VERSION = "0.1.3"
3
+ end
@@ -0,0 +1,29 @@
1
+ require_relative 'lib/render_json_rails/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "render_json_rails"
5
+ spec.version = RenderJsonRails::VERSION
6
+ spec.licenses = ['MIT']
7
+ spec.authors = ["Marcin"]
8
+ spec.email = ["marcin@radgost.com"]
9
+
10
+ spec.summary = "Simle JSON render like JonApi"
11
+ spec.description = "render json with 'includes' and 'fields' with simple config"
12
+ spec.homepage = "https://radgost.com/gems/render_json_rails"
13
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
14
+
15
+ # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
16
+
17
+ # spec.metadata["https://radgost.pl/gems"] = spec.homepage
18
+ # spec.metadata["source_code_uri"] = "TODO: Put your gem's public repo URL here."
19
+ # spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
24
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ end
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+ end
metadata ADDED
@@ -0,0 +1,55 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: render_json_rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.3
5
+ platform: ruby
6
+ authors:
7
+ - Marcin
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-07-01 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: render json with 'includes' and 'fields' with simple config
14
+ email:
15
+ - marcin@radgost.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".gitignore"
21
+ - Gemfile
22
+ - LICENSE.txt
23
+ - README.md
24
+ - Rakefile
25
+ - bin/console
26
+ - bin/setup
27
+ - lib/render_json_rails.rb
28
+ - lib/render_json_rails/concern.rb
29
+ - lib/render_json_rails/helper.rb
30
+ - lib/render_json_rails/version.rb
31
+ - render_json_rails.gemspec
32
+ homepage: https://radgost.com/gems/render_json_rails
33
+ licenses:
34
+ - MIT
35
+ metadata: {}
36
+ post_install_message:
37
+ rdoc_options: []
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: 2.3.0
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ requirements: []
51
+ rubygems_version: 3.1.2
52
+ signing_key:
53
+ specification_version: 4
54
+ summary: Simle JSON render like JonApi
55
+ test_files: []