fson 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7fcf0f1b392bdfd4ea2e8485eec01d46b5f088e0
4
+ data.tar.gz: 8fa8d6725d8dc28e2e52fe9a6c3c3973c0f9207c
5
+ SHA512:
6
+ metadata.gz: 0f3f7ac0f5652189e286bc02891625cf41f44136e6b4249854065d5faf1c5bb08e827b26dd8e0181c4d522ab541672076f8fa7a9a01a1ef07337342c2f008e3e
7
+ data.tar.gz: 1344fe823ac6814ea117677d502891d5302500dcdcc24ae72309223de1d28fac508578952011c81e9b499b57169bd1ea81229118f21b297143ae7bb3842d8afb
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ *.gem
2
+ projectFilesBackup
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
3
+
4
+ gem 'rspec'
data/Gemfile.lock ADDED
@@ -0,0 +1,33 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ fson (0.0.1)
5
+ oj (~> 2.12)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ diff-lcs (1.2.5)
11
+ oj (2.12.10)
12
+ rake (10.4.2)
13
+ rspec (3.3.0)
14
+ rspec-core (~> 3.3.0)
15
+ rspec-expectations (~> 3.3.0)
16
+ rspec-mocks (~> 3.3.0)
17
+ rspec-core (3.3.2)
18
+ rspec-support (~> 3.3.0)
19
+ rspec-expectations (3.3.1)
20
+ diff-lcs (>= 1.2.0, < 2.0)
21
+ rspec-support (~> 3.3.0)
22
+ rspec-mocks (3.3.2)
23
+ diff-lcs (>= 1.2.0, < 2.0)
24
+ rspec-support (~> 3.3.0)
25
+ rspec-support (3.3.0)
26
+
27
+ PLATFORMS
28
+ ruby
29
+
30
+ DEPENDENCIES
31
+ fson!
32
+ rake (~> 10.4)
33
+ rspec
data/README.md ADDED
@@ -0,0 +1 @@
1
+ # fluent-json-response
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # require "bundler/gem_tasks"
2
+ # require "rspec/core/rake_task"
3
+ # require 'rspec/core/rake_task'
4
+ #
5
+ # RSpec::Core::RakeTask.new
6
+ #
7
+ # task :default => :spec
8
+ # task :test => :spec
data/fson.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ require File.expand_path('../lib/fson/version', __FILE__)
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = 'fson'
5
+ s.version = Fson::VERSION
6
+
7
+ s.summary = 'Simple fluent builder for API JSON responses'
8
+ s.description = 'Building simple JSON responses with fluent builder'
9
+
10
+ s.authors = ['Mateusz Kluczny']
11
+ s.email = 'mkluczny@gmail.com'
12
+
13
+ s.homepage = 'https://github.com/mkluczny/fson'
14
+ s.license = 'MIT'
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_path = ['lib']
20
+
21
+ s.add_runtime_dependency 'oj', '~> 2.12'
22
+ s.add_development_dependency 'rspec', '~> 3.3'
23
+ s.add_development_dependency 'rake', '~> 10.4'
24
+ end
@@ -0,0 +1,27 @@
1
+ module Fson
2
+ module Fson::Builder
3
+
4
+ def data(data = nil, &block)
5
+ if data
6
+ @_data = data
7
+ end
8
+
9
+ yield(@_data) if block_given?
10
+ self
11
+ end
12
+
13
+ def status(status)
14
+ @_response[:status] = status
15
+ self
16
+ end
17
+
18
+ def error(message, id = nil)
19
+ @_errors << { :message => message }.tap {|error|
20
+ unless id.nil?
21
+ error[:id] = id
22
+ end
23
+ }
24
+ self
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,64 @@
1
+ require 'oj'
2
+
3
+ module Fson
4
+
5
+ require_relative 'builder'
6
+
7
+ class Response
8
+
9
+ include ::Fson::Builder
10
+
11
+ ##
12
+ # Constructor
13
+ ##
14
+
15
+ def initialize(status)
16
+ @_response = {
17
+ :status => status
18
+ }
19
+ @_data = {}
20
+ @_errors = []
21
+ end
22
+
23
+ def to_json
24
+ ::Oj.dump(build)
25
+ end
26
+
27
+ class << self
28
+
29
+ ##
30
+ # Factory Methods
31
+ ##
32
+
33
+ def success
34
+ ::Fson::Response.new(:success)
35
+ end
36
+
37
+ def error
38
+ ::Fson::Response.new(:error)
39
+ end
40
+
41
+ def fail
42
+ ::Fson::Response.new(:fail)
43
+ end
44
+ end
45
+
46
+ ##
47
+ # Private
48
+ ##
49
+
50
+ private
51
+
52
+ def build
53
+ unless @_data.empty?
54
+ @_response[:data] = @_data
55
+ end
56
+
57
+ unless @_errors.empty?
58
+ @_response[:errors] = @_errors
59
+ end
60
+
61
+ @_response
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,3 @@
1
+ module Fson
2
+ VERSION = '0.0.3'
3
+ end
data/lib/fson.rb ADDED
@@ -0,0 +1 @@
1
+ require 'fson/response'
@@ -0,0 +1,12 @@
1
+ require 'spec_helper'
2
+
3
+ describe ::Fson::Response do
4
+
5
+ it 'should create error response' do
6
+ # when
7
+ response = Fson::Response.error
8
+
9
+ # then
10
+ expect(response.instance_eval {@_response[:status]}).to eq(:error)
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ require 'spec_helper'
2
+
3
+ describe ::Fson::Response do
4
+
5
+ it 'should create fail response' do
6
+ # when
7
+ response = Fson::Response.fail
8
+
9
+ # then
10
+ expect(response.instance_eval {@_response[:status]}).to eq(:fail)
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ require 'spec_helper'
2
+
3
+ describe ::Fson::Response do
4
+
5
+ it 'should create response object with status assigned' do
6
+ # when
7
+ response = Fson::Response.new(:success)
8
+
9
+ # then
10
+ expect(response.instance_eval {@_response[:status]}).to eq(:success)
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ require 'spec_helper'
2
+
3
+ describe ::Fson::Response do
4
+
5
+ it 'should create success response' do
6
+ # when
7
+ response = Fson::Response.success
8
+
9
+ # then
10
+ expect(response.instance_eval {@_response[:status]}).to eq(:success)
11
+ end
12
+ end
@@ -0,0 +1,16 @@
1
+ require 'spec_helper'
2
+
3
+ describe ::Fson::Response do
4
+
5
+ context 'when getting json representation of response' do
6
+ it 'should return json response' do
7
+ # given
8
+ response = ::Fson::Response.error
9
+ .error('invalid', 'text-set-1')
10
+ .data { |data| data[:author] = 'Mateusz Kluczny' }
11
+
12
+ # when/then
13
+ expect(response.to_json).to eq("{\":status\":\":error\",\":data\":{\":author\":\"Mateusz Kluczny\"},\":errors\":[{\":message\":\"invalid\",\":id\":\"text-set-1\"}]}")
14
+ end
15
+ end
16
+ end
@@ -0,0 +1 @@
1
+ require 'fson'
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fson
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ platform: ruby
6
+ authors:
7
+ - Mateusz Kluczny
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-07-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: oj
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.12'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.12'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.4'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.4'
55
+ description: Building simple JSON responses with fluent builder
56
+ email: mkluczny@gmail.com
57
+ executables: []
58
+ extensions: []
59
+ extra_rdoc_files: []
60
+ files:
61
+ - ".gitignore"
62
+ - ".rspec"
63
+ - Gemfile
64
+ - Gemfile.lock
65
+ - README.md
66
+ - Rakefile
67
+ - fson.gemspec
68
+ - lib/fson.rb
69
+ - lib/fson/builder.rb
70
+ - lib/fson/response.rb
71
+ - lib/fson/version.rb
72
+ - spec/fson/response/error_spec.rb
73
+ - spec/fson/response/fail_spec.rb
74
+ - spec/fson/response/initialize_spec.rb
75
+ - spec/fson/response/success_spec.rb
76
+ - spec/fson/response/to_json_spec.rb
77
+ - spec/spec_helper.rb
78
+ homepage: https://github.com/mkluczny/fson
79
+ licenses:
80
+ - MIT
81
+ metadata: {}
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubyforge_project:
98
+ rubygems_version: 2.4.5
99
+ signing_key:
100
+ specification_version: 4
101
+ summary: Simple fluent builder for API JSON responses
102
+ test_files:
103
+ - spec/fson/response/error_spec.rb
104
+ - spec/fson/response/fail_spec.rb
105
+ - spec/fson/response/initialize_spec.rb
106
+ - spec/fson/response/success_spec.rb
107
+ - spec/fson/response/to_json_spec.rb
108
+ - spec/spec_helper.rb