jsend 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e216bf7f39d1a52c90163fa55102aba32fb13632
4
+ data.tar.gz: bba5752e6b78d8989d81e772f2fc2fdd7242dc11
5
+ SHA512:
6
+ metadata.gz: d768930f37d2cafb3913b25eebcd614fe2a8924cd45106b2ef72c3ad0d1b9b95de928d6e0e0694fcab17e2e32f157d0b7029c06d275c334e27e2e12baa4e220c
7
+ data.tar.gz: d768ab734b7c73742140d2d9c901a532c01c1aa8be747abe320f943951629cd5406f8d1b6858b971dcbdabec4a3fee5996a11c88771ab0190ff20b0a048bab47
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /.idea/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1 @@
1
+ jsend
@@ -0,0 +1 @@
1
+ ruby-2.0.0
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.13.6
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at alvaro@alvaromaceda.es. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in jsend.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Alvaro Maceda
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,73 @@
1
+ # JSend
2
+
3
+ JSend facilitates a simple jsend implementation for ruby. You can generate and parse JSend messages
4
+
5
+ For more info about JSend refer to https://labs.omniti.com/labs/jsend
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'jsend'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install jsend
22
+
23
+ ## Usage
24
+
25
+ You can generate sucess, error and failure messages:
26
+ ```ruby
27
+ JSend.success(banana: 'gross michel').to_json
28
+ ```
29
+ This will generate the following string:
30
+ ```
31
+ "{\"status\":\"success\",\"data\":{\"hola\":\"caracola\"}}"
32
+ ```
33
+ Corresponding to this JSon:
34
+ ```
35
+ {
36
+ "status":"success",
37
+ "data": {"banana":"gross michel"}
38
+ }
39
+ ```
40
+
41
+ It works the same way with failure messages:
42
+ ```ruby
43
+ JSend.fail(data)
44
+ ```
45
+
46
+ And error messages, with or withouth error code and data
47
+ ```ruby
48
+ jsend_message = JSend.error(error_message)
49
+ jsend_message = JSend.error(error_message, error_code, data)
50
+ ```
51
+
52
+ Unlike other libraries, you can also parse JSon strings. It will raise an error if the string can't be parsed to a Jsend message:
53
+ ```ruby
54
+ response = JSend.parse(last_response.body)
55
+ expect(response.success?).to eq(true)
56
+ puts response.data
57
+ ```
58
+
59
+ ## Development
60
+
61
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
62
+
63
+ 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).
64
+
65
+ ## Contributing
66
+
67
+ Bug reports and pull requests are welcome on GitHub at https://github.com/amaceda/jsend-ruby. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
68
+
69
+
70
+ ## License
71
+
72
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
73
+
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "jsend"
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
@@ -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,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'jsend/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'jsend'
9
+ spec.version = JSend::VERSION
10
+ spec.authors = ['Alvaro Maceda']
11
+ spec.email = ['alvaro@alvaromaceda.es']
12
+
13
+ spec.summary = 'Generates and parses JSend style JSON messages'
14
+ # spec.description = %q(TODO: Write a longer description or delete this line.
15
+ spec.homepage = 'https://github.com/AlvaroMaceda/jsend-ruby'
16
+ spec.license = 'MIT'
17
+
18
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
19
+ f.match(%r{^(test|spec|features)/})
20
+ end
21
+ spec.bindir = 'exe'
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ['lib']
24
+ spec.required_ruby_version = ['>=2.0.0']
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.13"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_development_dependency "rspec", "~> 3.0"
29
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'jsend/version'
4
+ require 'json'
5
+
6
+ require_relative 'jsend/responses/success_response'
7
+ require_relative 'jsend/responses/fail_response'
8
+ require_relative 'jsend/responses/error_response'
9
+ require_relative 'jsend/parser/parser'
10
+
11
+ module JSend
12
+
13
+ SUCCESS = 'success'
14
+ ERROR = 'error'
15
+ FAIL = 'fail'
16
+
17
+ def parse(json)
18
+ values = JSON.parse(json)
19
+ Parser.parser_for(values['status']).response_from_hash(values)
20
+ end
21
+
22
+ def success(data)
23
+ SucessResponse.new(data)
24
+ end
25
+
26
+ def fail(error_data)
27
+ FailResponse.new(error_data)
28
+ end
29
+
30
+ def error(msg, code = nil, data = nil)
31
+ ErrorResponse.new(data, msg, code)
32
+ end
33
+
34
+ module_function :parse, :success, :fail, :error
35
+
36
+ end
@@ -0,0 +1,15 @@
1
+ module JSend
2
+
3
+ class HashUtils
4
+ def self.remove_fields(hash, fields)
5
+ hash.keep_if { |x| !fields.include? x }
6
+ end
7
+
8
+ def self.has_fields_different_from(hash, fields)
9
+ not remove_fields(hash.clone, fields).empty?
10
+ end
11
+ end
12
+
13
+ private_constant :HashUtils
14
+
15
+ end
@@ -0,0 +1,19 @@
1
+ require_relative '../hash_utils'
2
+ require_relative '../responses/error_response'
3
+
4
+ module JSend
5
+
6
+ class ErrorMessageParser
7
+
8
+ def self.response_from_hash(hash)
9
+ raise InvalidData if hash['message'].nil?
10
+ raise InvalidData if HashUtils.has_fields_different_from(hash, %w(status message code data))
11
+
12
+ ErrorResponse.new hash['data'],hash['message'],hash['code']
13
+ end
14
+
15
+ end
16
+
17
+ private_constant :ErrorMessageParser
18
+
19
+ end
@@ -0,0 +1,14 @@
1
+ module JSend
2
+
3
+ class InvalidType < StandardError
4
+ def message
5
+ 'Unsupported type of message'
6
+ end
7
+ end
8
+
9
+ class InvalidData < StandardError
10
+ def message
11
+ 'Incorrect JSend message'
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,19 @@
1
+ require_relative '../hash_utils'
2
+ require_relative '../responses/fail_response'
3
+
4
+ module JSend
5
+
6
+ class FailMessageParser
7
+
8
+ def self.response_from_hash(hash)
9
+ raise InvalidData if hash['data'].nil?
10
+ raise InvalidData if HashUtils.has_fields_different_from(hash, %w(status data))
11
+
12
+ FailResponse.new hash['data']
13
+ end
14
+
15
+ end
16
+
17
+ private_constant :FailMessageParser
18
+
19
+ end
@@ -0,0 +1,31 @@
1
+ require_relative 'exceptions'
2
+ require_relative 'success_message_parser'
3
+ require_relative 'error_message_parser'
4
+ require_relative 'fail_message_parser'
5
+
6
+ module JSend
7
+
8
+ class Parser
9
+
10
+ def self.parser_for(type)
11
+ case type
12
+ when JSend::SUCCESS
13
+ SuccessMessageParser
14
+ when JSend::ERROR
15
+ ErrorMessageParser
16
+ when JSend::FAIL
17
+ FailMessageParser
18
+ else
19
+ raise JSend::InvalidType
20
+ end
21
+ end
22
+
23
+ end
24
+
25
+ private_constant :Parser
26
+ private_constant :SuccessMessageParser
27
+ private_constant :ErrorMessageParser
28
+ private_constant :FailMessageParser
29
+
30
+ end
31
+
@@ -0,0 +1,19 @@
1
+ require_relative '../hash_utils'
2
+ require_relative '../responses/success_response'
3
+
4
+ module JSend
5
+
6
+ class SuccessMessageParser
7
+
8
+ def self.response_from_hash(hash)
9
+ raise InvalidData if hash['data'].nil?
10
+ raise InvalidData if HashUtils.has_fields_different_from(hash, %w(status data))
11
+
12
+ SucessResponse.new hash['data']
13
+ end
14
+
15
+ end
16
+
17
+ private_constant :SuccessMessageParser
18
+
19
+ end
@@ -0,0 +1,22 @@
1
+ require_relative 'response'
2
+
3
+ module JSend
4
+
5
+ class ErrorResponse < Response
6
+
7
+ attr_reader :data, :message, :code
8
+
9
+ def initialize(data = nil, msg = nil, code = nil)
10
+ @status = JSend::ERROR
11
+ @data = data
12
+ @message = msg
13
+ @code = code
14
+ end
15
+
16
+ public_class_method :new
17
+
18
+ end
19
+
20
+ private_constant :ErrorResponse
21
+
22
+ end
@@ -0,0 +1,20 @@
1
+ require_relative 'response'
2
+
3
+ module JSend
4
+
5
+ class FailResponse < Response
6
+
7
+ attr_reader :data
8
+
9
+ def initialize(data)
10
+ @status = JSend::FAIL
11
+ @data = data
12
+ end
13
+
14
+ public_class_method :new
15
+
16
+ end
17
+
18
+ private_constant :FailResponse
19
+
20
+ end
@@ -0,0 +1,27 @@
1
+ module JSend
2
+
3
+ class Response
4
+
5
+ def to_json
6
+ data = {status: @status, message: @message, code: @code, data: @data}
7
+ relevant_data = data.keep_if { |_, v| !v.nil? }
8
+ relevant_data.to_json
9
+ end
10
+
11
+ def success?
12
+ @status == JSend::SUCCESS
13
+ end
14
+
15
+ def fail?
16
+ @status == JSend::FAIL
17
+ end
18
+
19
+ def error?
20
+ @status == JSend::ERROR
21
+ end
22
+
23
+ private_class_method :new
24
+
25
+ end
26
+
27
+ end
@@ -0,0 +1,20 @@
1
+ require_relative 'response'
2
+
3
+ module JSend
4
+
5
+ class SucessResponse < Response
6
+
7
+ attr_reader :data
8
+
9
+ def initialize(data)
10
+ @status = JSend::SUCCESS
11
+ @data = data
12
+ end
13
+
14
+ public_class_method :new
15
+
16
+ end
17
+
18
+ private_constant :SucessResponse
19
+
20
+ end
@@ -0,0 +1,3 @@
1
+ module JSend
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jsend
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Alvaro Maceda
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-07-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
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description:
56
+ email:
57
+ - alvaro@alvaromaceda.es
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - .rspec
64
+ - .ruby-gemset
65
+ - .ruby-version
66
+ - .travis.yml
67
+ - CODE_OF_CONDUCT.md
68
+ - Gemfile
69
+ - LICENSE.txt
70
+ - README.md
71
+ - Rakefile
72
+ - bin/console
73
+ - bin/setup
74
+ - jsend.gemspec
75
+ - lib/jsend.rb
76
+ - lib/jsend/hash_utils.rb
77
+ - lib/jsend/parser/error_message_parser.rb
78
+ - lib/jsend/parser/exceptions.rb
79
+ - lib/jsend/parser/fail_message_parser.rb
80
+ - lib/jsend/parser/parser.rb
81
+ - lib/jsend/parser/success_message_parser.rb
82
+ - lib/jsend/responses/error_response.rb
83
+ - lib/jsend/responses/fail_response.rb
84
+ - lib/jsend/responses/response.rb
85
+ - lib/jsend/responses/success_response.rb
86
+ - lib/jsend/version.rb
87
+ homepage: https://github.com/AlvaroMaceda/jsend-ruby
88
+ licenses:
89
+ - MIT
90
+ metadata: {}
91
+ post_install_message:
92
+ rdoc_options: []
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - '>='
98
+ - !ruby/object:Gem::Version
99
+ version: 2.0.0
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - '>='
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ requirements: []
106
+ rubyforge_project:
107
+ rubygems_version: 2.4.8
108
+ signing_key:
109
+ specification_version: 4
110
+ summary: Generates and parses JSend style JSON messages
111
+ test_files: []