fson 0.0.4 → 0.0.6

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 17bfd7f187015864d738f9b5a460bdefd845eb25
4
- data.tar.gz: 843bc84ed8bbb0f326cc8e66a709dec22649d9fe
3
+ metadata.gz: 8cddfef5d630ad5cf03719127dcbd0e8a5fa4236
4
+ data.tar.gz: c116041b17ede76525c20f74f5c8258bf19b3fad
5
5
  SHA512:
6
- metadata.gz: d09d1d708e8b03065375ded5bdf6e7e5688710ccb05b699d35608e4361dd5fcc5160058f8353287601cdc6b34bd1f463a7594ab19d7bb74fc4f057122b57a79d
7
- data.tar.gz: ab36d815a9db5df6923f49fe7bfa6f4389e7ad67f4609697e00c404775533057749c881392d550f341785441b47fa9fb935aee1651df6309ea2aee40be3adfd8
6
+ metadata.gz: cb5cd1db604ab6d2309466e17800193a94386fbf75f745d2c5d4f19f3227a8cfc86e206ea1adf55e15d29f1d627d1459384dbcc0bbc0077edd1bbe8c86c3a01b
7
+ data.tar.gz: 45307d3145080234ea52640fc2f2c72596e2f675773d5b4a5625c49b2d8a20b37813b5c16e44b9b74870a19234c8735434881532c8b2fbf24f44d189c8e13c9f
@@ -0,0 +1 @@
1
+ fson
@@ -0,0 +1 @@
1
+ ruby-2.2.2
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
4
+ addons:
5
+ code_climate:
6
+ repo_token: bc20ea9e0d0b4ea4346a7902a4fe20294f373cc5817137880503b4803d9e6017
data/Gemfile CHANGED
@@ -1,4 +1,8 @@
1
1
  source 'https://rubygems.org'
2
2
  gemspec
3
3
 
4
- gem 'rspec'
4
+ gem 'oj', '~> 2.12.10'
5
+
6
+ group :test do
7
+ gem 'codeclimate-test-reporter', require: nil
8
+ end
@@ -1,13 +1,16 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- fson (0.0.1)
5
- oj (~> 2.12)
4
+ fson (0.0.6)
6
5
 
7
6
  GEM
8
7
  remote: https://rubygems.org/
9
8
  specs:
9
+ codeclimate-test-reporter (0.4.7)
10
+ simplecov (>= 0.7.1, < 1.0.0)
10
11
  diff-lcs (1.2.5)
12
+ docile (1.1.5)
13
+ json (1.8.3)
11
14
  oj (2.12.10)
12
15
  rake (10.4.2)
13
16
  rspec (3.3.0)
@@ -23,11 +26,18 @@ GEM
23
26
  diff-lcs (>= 1.2.0, < 2.0)
24
27
  rspec-support (~> 3.3.0)
25
28
  rspec-support (3.3.0)
29
+ simplecov (0.10.0)
30
+ docile (~> 1.1.0)
31
+ json (~> 1.8)
32
+ simplecov-html (~> 0.10.0)
33
+ simplecov-html (0.10.0)
26
34
 
27
35
  PLATFORMS
28
36
  ruby
29
37
 
30
38
  DEPENDENCIES
39
+ codeclimate-test-reporter
31
40
  fson!
41
+ oj (~> 2.12.10)
32
42
  rake (~> 10.4)
33
- rspec
43
+ rspec (~> 3.3)
data/README.md CHANGED
@@ -1 +1,159 @@
1
- # Fson - JSON API Response Fluent Builder
1
+ ## Description
2
+
3
+ Fson is a fluent builder for simple JSON API responses
4
+
5
+ [![Build Status](https://travis-ci.org/mkluczny/fson.svg?branch=develop)](https://travis-ci.org/mkluczny/fson)
6
+ [![Dependency Status](https://gemnasium.com/mkluczny/fson.svg)](https://gemnasium.com/mkluczny/fson)
7
+ [![Code Climate](https://codeclimate.com/github/mkluczny/fson/badges/gpa.svg)](https://codeclimate.com/github/mkluczny/fson)
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ gem 'fson'
14
+
15
+ for Rails projects also
16
+
17
+ rails g fson:install
18
+
19
+ ## Usage
20
+
21
+ Create response builder
22
+
23
+ ```ruby
24
+ Fson::Response.new() # {}
25
+ ```
26
+
27
+ with given status
28
+
29
+ ```ruby
30
+ Fson::Response.new('failure') # {"status": "failure"}
31
+ ```
32
+
33
+ or use one of predefined factory methods
34
+
35
+ ```ruby
36
+ Fson::Response.success # {"status": "success"}
37
+ Fson::Response.error # {"status": "error"}
38
+ Fson::Response.fail # {"status": "fail"}
39
+ ```
40
+
41
+ then add some data by passing hash
42
+
43
+ ```ruby
44
+ .data({:id => 12})
45
+ ```
46
+
47
+ ```json
48
+ {
49
+ "data": {
50
+ "id": 12
51
+ }
52
+ }
53
+ ```
54
+
55
+ or defining block
56
+
57
+ ```ruby
58
+ .data { |data|
59
+ data[:id] = 12
60
+ }
61
+ ```
62
+
63
+ ```json
64
+ {
65
+ "data": {
66
+ "id": 12
67
+ }
68
+ }
69
+ ```
70
+
71
+ optionally add errors
72
+
73
+ ```ruby
74
+ .error('not authorized') { |e|
75
+ e[:code] = 401
76
+ }.error('null pointer exception')
77
+ ```
78
+
79
+ ```json
80
+ {
81
+ "errors": [
82
+ {
83
+ "message": "not authorized",
84
+ "code": 401
85
+ },
86
+ {
87
+ "message": "null pointer exception"
88
+ }
89
+ ]
90
+ }
91
+ ```
92
+
93
+ and finally get JSON with
94
+
95
+ ```ruby
96
+ .as_json
97
+ ```
98
+
99
+ ## Example
100
+
101
+ Builder chain
102
+
103
+ ```ruby
104
+ Fson::Response.fail.data {|data| data[:id] = 12}.error('not authorized').as_json
105
+ ```
106
+
107
+ will return
108
+
109
+ ```json
110
+ {
111
+ "status": "fail",
112
+ "data": {
113
+ "id": 12
114
+ },
115
+ "errors": [
116
+ {
117
+ "message": "not authorized"
118
+ }
119
+ ]
120
+ }
121
+ ```
122
+
123
+ ## Custom builders
124
+
125
+ You can add custom builder methods operating on response hash objects
126
+
127
+ ```ruby
128
+ @_response # top level response hash
129
+ @_data # data hash
130
+ @_errors # errors hash
131
+ ```
132
+
133
+ For example you can add builder
134
+
135
+ ```ruby
136
+ module MyCustomBuilder
137
+
138
+ def attribute(value)
139
+ @_data[:attribute] = value
140
+ self
141
+ end
142
+ end
143
+ ```
144
+
145
+ by registering it in initializer
146
+
147
+ ```ruby
148
+ require 'fson/loader'
149
+
150
+ ::Fson::Loader::configure([MyCustomBuilder])
151
+ ```
152
+
153
+ ## Contributing
154
+
155
+ 1. Fork it
156
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
157
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
158
+ 4. Push to the branch (`git push origin my-new-feature`)
159
+ 5. Create new Pull Request
data/Rakefile CHANGED
@@ -1,8 +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
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
@@ -4,7 +4,7 @@ Gem::Specification.new do |s|
4
4
  s.name = 'fson'
5
5
  s.version = Fson::VERSION
6
6
 
7
- s.summary = 'Simple fluent builder for API JSON responses'
7
+ s.summary = 'Fluent builder for API JSON responses'
8
8
  s.description = 'Building simple JSON responses with fluent builder'
9
9
 
10
10
  s.authors = ['Mateusz Kluczny']
@@ -18,7 +18,6 @@ Gem::Specification.new do |s|
18
18
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
19
  s.require_path = ['lib']
20
20
 
21
- s.add_runtime_dependency 'oj', '~> 2.12'
22
21
  s.add_development_dependency 'rspec', '~> 3.3'
23
22
  s.add_development_dependency 'rake', '~> 10.4'
24
23
  end
@@ -1,5 +1,5 @@
1
1
  module Fson
2
- module Fson::Builder
2
+ module Builder
3
3
 
4
4
  def data(data = nil, &block)
5
5
  if data
@@ -15,12 +15,13 @@ module Fson
15
15
  self
16
16
  end
17
17
 
18
- def error(message, id = nil)
19
- @_errors << { :message => message }.tap {|error|
20
- unless id.nil?
21
- error[:id] = id
22
- end
23
- }
18
+ def error(message, &block)
19
+ error = { :message => message }
20
+
21
+ yield(error) if block_given?
22
+
23
+ @_errors << error
24
+
24
25
  self
25
26
  end
26
27
  end
@@ -0,0 +1,11 @@
1
+ require 'fson/response'
2
+
3
+ module Fson
4
+ module Loader
5
+ def self.configure(classes)
6
+ classes.each { |clazz|
7
+ Fson::Response.send(:include, clazz)
8
+ }
9
+ end
10
+ end
11
+ end
@@ -12,16 +12,18 @@ module Fson
12
12
  # Constructor
13
13
  ##
14
14
 
15
- def initialize(status)
16
- @_response = {
17
- :status => status
18
- }
15
+ def initialize(status = nil)
16
+ @_response = {}
19
17
  @_data = {}
20
18
  @_errors = []
19
+
20
+ unless status.nil?
21
+ @_response[:status] = status
22
+ end
21
23
  end
22
24
 
23
25
  def as_json
24
- ::Oj.dump(build)
26
+ ::Oj.dump(build, :mode => :compat)
25
27
  end
26
28
 
27
29
  class << self
@@ -1,3 +1,3 @@
1
1
  module Fson
2
- VERSION = '0.0.4'
2
+ VERSION = '0.0.6'
3
3
  end
@@ -0,0 +1,14 @@
1
+ module Fson
2
+ module Generators
3
+ class InstallGenerator < Rails::Generators::Base
4
+
5
+ def self.source_root
6
+ @source_root ||= File.expand_path(File.join(File.dirname(__FILE__), 'templates'))
7
+ end
8
+
9
+ def create_initializer
10
+ template 'initializer.rb', File.join('config', 'initializers', 'fson.rb')
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,3 @@
1
+ require 'fson/loader'
2
+
3
+ ::Fson::Loader::configure([])
@@ -7,11 +7,11 @@ describe ::Fson::Response do
7
7
  it 'should return json response' do
8
8
  # given
9
9
  response = ::Fson::Response.error
10
- .error('invalid', 'text-set-1')
10
+ .error('invalid') { |e| e[:id] = 'text-set-1'}
11
11
  .data { |data| data[:author] = 'Mateusz Kluczny' }
12
12
 
13
13
  # when/then
14
- expect(response.as_json).to eq("{\":status\":\":error\",\":data\":{\":author\":\"Mateusz Kluczny\"},\":errors\":[{\":message\":\"invalid\",\":id\":\"text-set-1\"}]}")
14
+ expect(response.as_json).to eq("{\"status\":\"error\",\"data\":{\"author\":\"Mateusz Kluczny\"},\"errors\":[{\"message\":\"invalid\",\"id\":\"text-set-1\"}]}")
15
15
  end
16
16
  end
17
17
  end
@@ -1 +1,4 @@
1
- require 'fson'
1
+ require 'codeclimate-test-reporter'
2
+ CodeClimate::TestReporter.start
3
+
4
+ require 'fson'
metadata CHANGED
@@ -1,29 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fson
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mateusz Kluczny
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-07-23 00:00:00.000000000 Z
11
+ date: 2015-07-24 00:00:00.000000000 Z
12
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
13
  - !ruby/object:Gem::Dependency
28
14
  name: rspec
29
15
  requirement: !ruby/object:Gem::Requirement
@@ -60,6 +46,9 @@ extra_rdoc_files: []
60
46
  files:
61
47
  - ".gitignore"
62
48
  - ".rspec"
49
+ - ".ruby-gemset"
50
+ - ".ruby-version"
51
+ - ".travis.yml"
63
52
  - Gemfile
64
53
  - Gemfile.lock
65
54
  - README.md
@@ -67,8 +56,11 @@ files:
67
56
  - fson.gemspec
68
57
  - lib/fson.rb
69
58
  - lib/fson/builder.rb
59
+ - lib/fson/loader.rb
70
60
  - lib/fson/response.rb
71
61
  - lib/fson/version.rb
62
+ - lib/generators/fson/install_generator.rb
63
+ - lib/generators/fson/templates/initializer.rb
72
64
  - spec/fson/response/as_json_spec.rb
73
65
  - spec/fson/response/error_spec.rb
74
66
  - spec/fson/response/fail_spec.rb
@@ -95,10 +87,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
95
87
  version: '0'
96
88
  requirements: []
97
89
  rubyforge_project:
98
- rubygems_version: 2.4.5
90
+ rubygems_version: 2.4.6
99
91
  signing_key:
100
92
  specification_version: 4
101
- summary: Simple fluent builder for API JSON responses
93
+ summary: Fluent builder for API JSON responses
102
94
  test_files:
103
95
  - spec/fson/response/as_json_spec.rb
104
96
  - spec/fson/response/error_spec.rb