jsos 1.0.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: 3d441cffbf9bb670f441a13fa351d1cad6970af3
4
+ data.tar.gz: 6ccb84d6350854b0ee6e859404087163669ff49f
5
+ SHA512:
6
+ metadata.gz: b822ba701f192cd25076818b23123a112affb0774012404f52acad154418ed0525a5218e130e31c7b94cb2b0f2a055e669d3fd3d0707e17c20edcf8fd1f26961
7
+ data.tar.gz: cd6d096acc2a6cf5ee0e0cba723bec0acc83ad8deffd3da61f5657cd06821a197495856e450199a6ff894951513df97c8a2a82b50c25cc41aaf2602aaf8d44a0
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1,8 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
4
+ - 2.1.0
5
+ - 2.0.0
6
+ - 1.9.3
7
+ before_install: gem install bundler -v 1.10.6
8
+ script: bundle exec rspec spec
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Johnson Denen
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,83 @@
1
+ # JSOS
2
+ [![Build Status](https://travis-ci.org/jdenen/jsos.svg?branch=master)](https://travis-ci.org/jdenen/jsos)
3
+
4
+ Use Ruby's OpenStruct object to represent JSON strings, making nested data a method call away.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'jsos'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install jsos
21
+
22
+ ## Usage
23
+
24
+ JSOS turns a JSON string into an OpenStruct. It also allows you to construct JSON strings from scratch. Setter methods are turned into JSON keys and their arguments become the values. Getter methods return those values.
25
+
26
+ ```ruby
27
+ # parsing JSON into OpenStruct
28
+ jsos = JSOS.new("{\"foo\":\"bar\"}")
29
+ jsos.foo
30
+ #=> "bar"
31
+
32
+ # parsing Hash into OpenStruct
33
+ jsos = JSOS.new({foo: "bar"})
34
+ jsos.foo
35
+ #=> "bar"
36
+
37
+ # creating an empty object and converting to JSON
38
+ jsos = JSOS.new
39
+ jsos.to_json
40
+ #=> "{}"
41
+
42
+ # adding to the empty JSON
43
+ jsos.foo = "bar"
44
+ jsos.to_json
45
+ #=> "{\"foo\":\"bar\"}"
46
+ ```
47
+
48
+ A missing getter method is created with an empty JSOS object as its value. This allows you to chain methods to created nested JSON strings.
49
+
50
+ ```ruby
51
+ # nesting empty JSON objects
52
+ jsos = JSOS.new
53
+ jsos.foo
54
+ jsos.to_json
55
+ #=> "{\"foo\":{}}"
56
+
57
+ # chaining methods to create nested JSON strings
58
+ jsos = JSOS.new
59
+ jsos.abc.foo = "bar"
60
+ jsos.xyz.foo = "baz"
61
+ jsos.to_json
62
+ #=> "{\"abc\":{\"foo\":\"bar\"}, \"xyz\":{\"foo\":\"baz\"}}"
63
+ ```
64
+
65
+ You can grab object level keys and values like a normal Ruby Hash. These do not return nested keys/values.
66
+
67
+ ```ruby
68
+ jsos = JSOS.new("{\"abc\":{\"foo\":\"bar\"}, \"xyz\":{\"foo\":\"baz\"}}")
69
+ jsos.keys
70
+ #=> ["abc", "xyz"]
71
+ jsos.values
72
+ #=> ["#<JSOS foo=\"bar\">", "#<JSOS foo=\"baz\">"]
73
+ ```
74
+
75
+ ## Contributing
76
+
77
+ Bug reports and pull requests are welcome on GitHub at https://github.com/jdenen/jsos.
78
+
79
+
80
+ ## License
81
+
82
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
83
+
@@ -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 "rejoinder"
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,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "jsos"
7
+ spec.version = "1.0.0"
8
+ spec.authors = ["Johnson Denen"]
9
+ spec.email = ["johnson.denen@gmail.com"]
10
+
11
+ spec.summary = %q{JSON as OpenStruct}
12
+ spec.description = %q{Convert JSON into nested OpenStruct objects with ease.}
13
+ spec.homepage = "https://github.com/jdenen/jsos"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.10"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec", "~> 3.0"
24
+ spec.add_development_dependency "simplecov", "~> 0.11"
25
+ spec.add_development_dependency "pry"
26
+ spec.add_development_dependency "yard"
27
+ end
@@ -0,0 +1,86 @@
1
+ require "ostruct"
2
+ require "json"
3
+
4
+ #
5
+ # Parse or construct JSON strings with OpenStruct objects.
6
+ #
7
+ # @example
8
+ # JSOS.new("{\"foo\":\"bar\"}")
9
+ #
10
+ # Setter methods add a key and a value to the object and are converted to JSON.
11
+ #
12
+ # @example
13
+ # jsos = JSOS.new
14
+ # jsos.foo = "bar"
15
+ # jsos.to_json
16
+ # #=> "{\"foo\":\"bar\"}"
17
+ #
18
+ # Getter methods represent the object keys and return their values.
19
+ #
20
+ # @example
21
+ # jsos = JSOS.new("{\"foo\":\"bar\"}")
22
+ # jsos.foo
23
+ # #=> "bar"
24
+ #
25
+ # Undefined getter methods act like setter methods. Their values become empty JSOS objects.
26
+ #
27
+ # @example
28
+ # jsos = JSOS.new
29
+ # jsos.foo
30
+ # jsos.to_json
31
+ # #=> "{\"foo\":{}}"
32
+ #
33
+ # Undefined getters can be chained with setters to create nested JSON objects.
34
+ #
35
+ # @example
36
+ # jsos = JSOS.new
37
+ # jsos.abc.foo = "bar"
38
+ # jsos.to_json
39
+ # #=> "{\"abc\":{\"foo\":\"bar\"}}"
40
+ #
41
+ class JSOS < OpenStruct
42
+
43
+ # @param state [String|Hash] defaults to nil
44
+ def initialize(state = nil)
45
+ state.is_a?(String) ? super(JSON.parse state) : super(state)
46
+ end
47
+
48
+ # @return [Hash]
49
+ def to_h
50
+ @table.each_with_object({}) do |(key, value), new_hash|
51
+ new_hash[key] = value.is_a?(JSOS) ? value.to_h : value
52
+ end
53
+ end
54
+
55
+ # @return [String]
56
+ def to_json
57
+ self.to_h.to_json
58
+ end
59
+
60
+ # @return [Array<Symbol>]
61
+ def keys
62
+ self.to_h.keys
63
+ end
64
+
65
+ # @return [Array]
66
+ def values
67
+ self.to_h.values
68
+ end
69
+
70
+ # @return [true|false]
71
+ def empty?
72
+ @table.empty?
73
+ end
74
+
75
+ private
76
+
77
+ # @api private
78
+ def method_missing methd, *args
79
+ methd.to_s.end_with?('=') ? super(methd, *args) : super(make_setter(methd), JSOS.new)
80
+ end
81
+
82
+ # @api private
83
+ def make_setter getter
84
+ getter.to_s.concat('=').to_sym
85
+ end
86
+ end
metadata ADDED
@@ -0,0 +1,139 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jsos
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Johnson Denen
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-12-31 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.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
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
+ - !ruby/object:Gem::Dependency
56
+ name: simplecov
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.11'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.11'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: yard
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Convert JSON into nested OpenStruct objects with ease.
98
+ email:
99
+ - johnson.denen@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".travis.yml"
106
+ - Gemfile
107
+ - LICENSE.txt
108
+ - README.md
109
+ - Rakefile
110
+ - bin/console
111
+ - bin/setup
112
+ - jsos.gemspec
113
+ - lib/jsos.rb
114
+ homepage: https://github.com/jdenen/jsos
115
+ licenses:
116
+ - MIT
117
+ metadata: {}
118
+ post_install_message:
119
+ rdoc_options: []
120
+ require_paths:
121
+ - lib
122
+ required_ruby_version: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ required_rubygems_version: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ requirements: []
133
+ rubyforge_project:
134
+ rubygems_version: 2.4.5
135
+ signing_key:
136
+ specification_version: 4
137
+ summary: JSON as OpenStruct
138
+ test_files: []
139
+ has_rdoc: