rails_api_response 0.1.0

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b066361b714562c86f833d37c7a8aa3db621ecdf
4
+ data.tar.gz: 408c68b4102e9ccf96aa17b3adb5df9be860096c
5
+ SHA512:
6
+ metadata.gz: 3cfde9a86a413c9c200ffbbb5131b903a43334e0d85041fb07eae0d38c9c881d1c3fe9109ce80f2b3ab508f4f2539ada01ee020aee3c392f64e7f84a70c3e3b2
7
+ data.tar.gz: 9d47f3540f70b002a1277b93abf7641331d9e2e2481e66febf9d8cb80be96512da2d97df38f0b02b78d9a4d99a498f04d57745fa60d98218c7bd41006e54e756
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in api_response.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,37 @@
1
+ # ApiResponse
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/api_response`. 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 'api_response'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install api_response
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ 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).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/api_response.
36
+
37
+ # api-response
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
@@ -0,0 +1,35 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'api_response/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rails_api_response"
8
+ spec.version = ApiResponse::VERSION
9
+ spec.authors = ["Vinh Chau"]
10
+ spec.email = ["cpvinh@gmail.com"]
11
+
12
+ spec.summary = "The gem for rails framework to modify the api response"
13
+ spec.description = "The gem for rails framework to modify the api response"
14
+ spec.homepage = "https://github.com/vinhcp/api-response"
15
+ spec.license = 'MIT'
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ #if spec.respond_to?(:metadata)
20
+ # spec.metadata['allowed_push_host'] = "https://rubygems.org/gems/api-response"
21
+ #else
22
+ # raise "RubyGems 2.0 or newer is required to protect against " \
23
+ # "public gem pushes."
24
+ #end
25
+
26
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
27
+ f.match(%r{^(test|spec|features)/})
28
+ end
29
+ spec.bindir = "exe"
30
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
31
+ spec.require_paths = ["lib"]
32
+
33
+ spec.add_development_dependency "bundler", "~> 1.13"
34
+ spec.add_development_dependency "rake", "~> 10.0"
35
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "api_response"
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
data/bin/setup ADDED
@@ -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,12 @@
1
+ require "api_response/version"
2
+
3
+ module Core
4
+ module ControllerHelpers
5
+ autoload :ResponseTemplate, 'core/controller_helpers/response_template'
6
+ autoload :Responses, 'core/controller_helpers/responses'
7
+ end
8
+ end
9
+
10
+ module ApiResponse
11
+ # Your code goes here...
12
+ end
@@ -0,0 +1,3 @@
1
+ module ApiResponse
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,30 @@
1
+ module ResponseTemplate
2
+ def self.template(code, metadata, data={})
3
+ response = { code: code, metadata: metadata, data: data }
4
+
5
+ Rails.logger.info Time.zone
6
+ Rails.logger.info response
7
+
8
+ response
9
+ end
10
+
11
+ def self.success(metadata, data = {})
12
+ template(200, metadata, data)
13
+ end
14
+
15
+ def self.create_success(metadata, data = {})
16
+ template(201, metadata, data)
17
+ end
18
+
19
+ def self.error_credential(metadata, data = {})
20
+ template(401, metadata, data)
21
+ end
22
+
23
+ def self.error_not_found(metadata, data = {})
24
+ template(404, metadata, data)
25
+ end
26
+
27
+ def self.error(metadata, data = {})
28
+ template(500, metadata, data)
29
+ end
30
+ end
@@ -0,0 +1,54 @@
1
+ module Core
2
+ module ControllerHelpers
3
+ module Responses
4
+
5
+ # Response methods
6
+ def respond_success(message: '', data: {})
7
+ render json: ResponseTemplate.success(message_metadata(message), data)
8
+ end
9
+
10
+ def respond_error(message: '', instance: nil, data: {})
11
+ metadata = instance.present? ? error_instance_metadata(instance) : message_metadata(message)
12
+ render json: ResponseTemplate.error(metadata, data)
13
+ end
14
+
15
+ def respond_create_success(message: "", data: {})
16
+ render json: ResponseTemplate.create_success(message_metadata(message), data)
17
+ end
18
+
19
+ def respond_error_credential(message:, data: {})
20
+ render json: ResponseTemplate.error_credential(message_metadata(message), data)
21
+ end
22
+
23
+ def respond_error_not_found(message:, data: {})
24
+ render json: ResponseTemplate.error_not_found(message_metadata(message), data)
25
+ end
26
+
27
+ def respond_json(code: 200, message: '', data: {})
28
+ render json: ResponseTemplate.template(code, message_metadata(message), data)
29
+ end
30
+
31
+ private
32
+
33
+ # Convert instance to error metadata
34
+ def error_instance_metadata(instance)
35
+ errors = []
36
+ instance.errors.to_hash.each do |k,v|
37
+ errors << {
38
+ field: k.to_s,
39
+ description: v
40
+ }
41
+ end
42
+ {
43
+ message: instance.errors.full_messages,
44
+ errors: errors
45
+ }
46
+ end
47
+
48
+ # Convert message text to error metadata
49
+ def message_metadata(message)
50
+ { message: message }
51
+ end
52
+ end
53
+ end
54
+ end
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails_api_response
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Vinh Chau
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-10-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.13'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.13'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: The gem for rails framework to modify the api response
42
+ email:
43
+ - cpvinh@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - Gemfile
50
+ - README.md
51
+ - Rakefile
52
+ - api_response.gemspec
53
+ - bin/console
54
+ - bin/setup
55
+ - lib/api_response.rb
56
+ - lib/api_response/version.rb
57
+ - lib/core/controller_helpers/response_template.rb
58
+ - lib/core/controller_helpers/responses.rb
59
+ homepage: https://github.com/vinhcp/api-response
60
+ licenses:
61
+ - MIT
62
+ metadata: {}
63
+ post_install_message:
64
+ rdoc_options: []
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ requirements: []
78
+ rubyforge_project:
79
+ rubygems_version: 2.4.8
80
+ signing_key:
81
+ specification_version: 4
82
+ summary: The gem for rails framework to modify the api response
83
+ test_files: []
84
+ has_rdoc: