simplify_api 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
+ SHA256:
3
+ metadata.gz: 60a0e19049ebda23b3ab1eba59e4eca72579f590da586d6196bfdf6e496f5d79
4
+ data.tar.gz: efc09f9794de5e0857eeda5724cfb12e0e9e7fc9269d4c705c9c107ae4ab2a4a
5
+ SHA512:
6
+ metadata.gz: 9b6e714f718dee139a49b4af1f605079e1863aeb2d6e7cca656ca5d7eef5c546788fb7bd820424ce0a5723b77dbb9fba7d43ead11537c684f8a86ccbf8e15948
7
+ data.tar.gz: 77a6264dd517f32ed8ec9243b6207695e1d7cb0fb7cb4f3d3ca5ee5f92edddc983798ef6f4a53a385696050ebbdb44b00b02dea905de34243cb23f7af4b3f16d
data/.gitignore ADDED
@@ -0,0 +1,6 @@
1
+ /.bundle/
2
+ /Gemfile.lock
3
+ /spec/reports/
4
+ /tmp/
5
+ /vendor/
6
+ *.gem
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in bot_engine.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,65 @@
1
+ # SimplifyApi
2
+
3
+ Simplify the use of APIs in Ruby.
4
+
5
+ The simplicity is really that you can define your API as Ruby classes, and throwing in a JSON string you'll have all your objects created.
6
+
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'simplify_api'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ ## Usage
21
+
22
+
23
+ ```ruby
24
+ require 'simplify_api'
25
+
26
+ class Options
27
+ include SimplifyApi
28
+ attribute :key, String, mandatory: true
29
+ attribute :value, String, mandatory: true
30
+ end
31
+
32
+ class ServiceDescription
33
+ include SimplifyApi
34
+ attribute :service_id, Integer, mandatory: true
35
+ attribute :services, [String]
36
+ end
37
+
38
+ class ApiCallParameters
39
+ include SimplifyApi
40
+ attribute :id, Integer, mandatory: true
41
+ attribute :name, String
42
+ attribute :gender, String, values: ["Male", "Female", "Other"]
43
+ attribute :email, String
44
+ attribute :options, [Options]
45
+ attribute :service_description, ServiceDescription
46
+ end
47
+
48
+ api_parameters = ApiCallParameters.new id: 1, name: "MyApi", options: [{key: "Create", value: "/api/create"}, {key: "Update", value: "/api/update"}], service_description: {service_id: 1, services: ["Create", "Update"]}
49
+ # => #<ApiCallParameters:0x0000000002b09ff8 @id=1, @name="MyApi", @options=[#<Options:0x0000000002b09670 @key="Create", @value="/api/create">, #<Options:0x0000000002b08608 @key="Update", @value="/api/update">], @service_description=#<ServiceDescription:0x0000000002b07aa0 @service_id=1, @services=["Create", "Update"]>>
50
+
51
+ api_parameters.to_h
52
+ # => {:id=>1, :name=>"MyApi", :options=>[{:key=>"Create", :value=>"/api/create"}, {:key=>"Update", :value=>"/api/update"}], :service_description=>{:service_id=>1, :services=>["Create", "Update"]}}
53
+ ```
54
+
55
+ This setup is ideal to toss in Hashed versions of JSON data and instantiate your objects.
56
+
57
+ ## Contributing
58
+
59
+ Bug reports and pull requests are welcome on GitHub at https://github.com/rodgco/simplify_api. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](https://www.contributor-covenant.org/) code of conduct.
60
+
61
+ ## License
62
+
63
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
64
+
65
+ Ruby is Beautiful!!!
@@ -0,0 +1,3 @@
1
+ require 'json'
2
+
3
+ require 'simplify_api/simplify_api'
@@ -0,0 +1,100 @@
1
+ module SimplifyApi
2
+ def self.included(base)
3
+ base.singleton_class.send(:attr_accessor, :attributes)
4
+ base.attributes = {}
5
+ base.singleton_class.send(:attr_accessor, :mandatory)
6
+ base.mandatory = []
7
+ base.singleton_class.send(:attr_accessor, :default)
8
+ base.default = {}
9
+ base.extend(ClassMethods)
10
+ end
11
+
12
+ module ClassMethods
13
+ def attribute(attr, type = Object, **args)
14
+ raise ArgumentError, "Duplicate attribute #{attr}." if self.attributes[attr]
15
+ if type.class == Array then
16
+ args[:default] = [] unless args[:default]
17
+ args[:array_type] = type[0]
18
+ end
19
+ self.attributes[attr] = {
20
+ name: attr.to_s,
21
+ type: type.class == Class ? type : type.class == Array ? Array : Object,
22
+ params: args
23
+ }
24
+ self.mandatory << attr if args[:mandatory]
25
+ self.default["#{attr}".to_sym] = args[:default] if args[:default]
26
+ self.attributes[attr]
27
+ end
28
+ end
29
+
30
+ def initialize(opts)
31
+ opts.each_pair do |k, v|
32
+ case v
33
+ when Hash
34
+ v = self.class.attributes[k][:type].new(v)
35
+ when Array
36
+ v.collect! { |i| self.class.attributes[k][:params][:array_type].new(i) }
37
+ end
38
+ create_and_set_instance_variable("#{k}", v)
39
+ end
40
+ raise ArgumentError, "Missing mandatory attributes => #{self.class.mandatory-self.instance_variables}" if not mandatory_attributes_in?
41
+ self.class.default.each_pair do |k, v|
42
+ create_and_set_instance_variable(k, v) unless self.instance_variables.include?("@#{k}".to_sym)
43
+ end
44
+ self
45
+ end
46
+
47
+ def to_h
48
+ h = {}
49
+ instance_variables.each do |i|
50
+ k = /\@(.*)/.match(i.to_s)[1].to_sym
51
+ v = instance_variable_get(i)
52
+ if v.class == Array then
53
+ h[k] = []
54
+ v.each {|a| h[k] << (a.respond_to?(:to_h) ? a.to_h : a) }
55
+ else
56
+ h[k] = v.respond_to?(:to_h) ? v.to_h : v
57
+ end
58
+ end
59
+ return h
60
+ end
61
+
62
+ def to_json
63
+ self.to_h.to_json
64
+ end
65
+
66
+ def method_missing(method_name, *args, &block)
67
+ case method_name
68
+ when /(.*)\=$/
69
+ create_and_set_instance_variable("#{$1}", args[0])
70
+ else
71
+ super(method_name, args, block)
72
+ end
73
+ end
74
+
75
+ def respond_to_missing?(method_name, *args)
76
+ case method_name
77
+ when /(.*)\=$/
78
+ true # always respond to assignment methods
79
+ else
80
+ super(method_name, args)
81
+ end
82
+ end
83
+
84
+ private
85
+ def create_and_set_instance_variable(name, value)
86
+ define_singleton_method("#{name}=") do |arg|
87
+ valid_values = self.class.attributes[name.to_sym][:params][:values]
88
+ raise "Argument Error. Invalid value for #{name} => #{arg}" if valid_values && !valid_values.include?(arg)
89
+ instance_variable_set("@#{name}", arg)
90
+ end
91
+ define_singleton_method("#{name}") do
92
+ instance_variable_get("@#{name}")
93
+ end
94
+ self.send("#{name}=", value)
95
+ end
96
+
97
+ def mandatory_attributes_in?
98
+ self.class.mandatory.collect {|v| self.instance_variables.include?("@#{v}".to_sym)}.inject(&:&)
99
+ end
100
+ end
@@ -0,0 +1,3 @@
1
+ module SimplifyApi
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,20 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'simplify_api/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "simplify_api"
8
+ spec.version = SimplifyApi::VERSION
9
+ spec.date = "2019-05-07"
10
+ spec.authors = ["Rodrigo Garcia Couto"]
11
+ spec.email = ["r@rodg.co"]
12
+ spec.summary = %q{A simple set of tools to help the use of APIs}
13
+ spec.description = %q{Fairly usable. A simple set of tools to simplify the use of APIs in Ruby}
14
+ spec.homepage = "https://github.com/rodgco/simplify_api"
15
+ spec.license = "MIT"
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.require_paths = ["lib"]
18
+
19
+ spec.add_development_dependency "bundler", "~> 2.0"
20
+ end
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simplify_api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Rodrigo Garcia Couto
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-05-07 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: '2.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ description: Fairly usable. A simple set of tools to simplify the use of APIs in Ruby
28
+ email:
29
+ - r@rodg.co
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".gitignore"
35
+ - Gemfile
36
+ - README.md
37
+ - lib/simplify_api.rb
38
+ - lib/simplify_api/simplify_api.rb
39
+ - lib/simplify_api/version.rb
40
+ - simplify_api.gemspec
41
+ homepage: https://github.com/rodgco/simplify_api
42
+ licenses:
43
+ - MIT
44
+ metadata: {}
45
+ post_install_message:
46
+ rdoc_options: []
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ requirements: []
60
+ rubyforge_project:
61
+ rubygems_version: 2.7.3
62
+ signing_key:
63
+ specification_version: 4
64
+ summary: A simple set of tools to help the use of APIs
65
+ test_files: []