oven 0.1.0.rc1

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: e8d1611cc2d287709137cba352af6350191c4538
4
+ data.tar.gz: 1bef493b415943da2ada5b8029e89a685a61f610
5
+ SHA512:
6
+ metadata.gz: 5aceaa69c14fd73e8f3f01274049e4539443be2f0ec8f29fd3d6f08c4a7851e3337588c7fc82dac59f70e233c990229ab0dd42119df29f7a87ab5c7c6ca3fbd9
7
+ data.tar.gz: e86974106be06c90407ec3b1bf212f99e6a603d9c2a5eb97c6b5850b48282f7b56d6ee3f7847ebaf85f55e43fdfc5a80fca54503e552d36882a2eda9803956ae
@@ -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,23 @@
1
+ language: ruby
2
+ script: bundle exec rake
3
+ cache: bundler
4
+ sudo: false
5
+
6
+ before_install: gem install bundler
7
+
8
+ rvm:
9
+ - 2.2.5
10
+ - 2.3.3
11
+ - 2.4.0
12
+ - ruby-head
13
+ - jruby-9.1.5.0
14
+ - jruby-head
15
+ - rbx
16
+
17
+ matrix:
18
+ allow_failures:
19
+ - rvm: ruby-head
20
+ - rvm: jruby-9.1.5.0
21
+ - rvm: jruby-head
22
+ - rvm: rbx
23
+ fast_finish: true
@@ -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 mail@yukinishijima.net. 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,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in oven.gemspec
4
+ gemspec
5
+
6
+ gem 'byebug', platforms: :mri
7
+ gem 'object_mapper'
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Yuki Nishijima
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,39 @@
1
+ # Oven
2
+
3
+ More details coming soon.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'oven'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install oven
20
+
21
+ ## Usage
22
+
23
+ More details coming soon.
24
+
25
+ ## Development
26
+
27
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
28
+
29
+ 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).
30
+
31
+ ## Contributing
32
+
33
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/oven. 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.
34
+
35
+
36
+ ## License
37
+
38
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
39
+
@@ -0,0 +1,15 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new do |task|
5
+ task.libs << "test"
6
+
7
+ task.test_files = Dir['test/**/*_test.rb'].reject do |path|
8
+ /(examples)/ =~ path
9
+ end
10
+
11
+ task.verbose = true
12
+ task.warning = true
13
+ end
14
+
15
+ task :default => :test
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "oven"
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,54 @@
1
+ require "oven/version"
2
+ require "oven/http_statuses"
3
+ require "oven/refinements"
4
+ require "oven/extension_configurers"
5
+ require "oven/dsl_context"
6
+ require "oven/builders"
7
+
8
+ module Oven
9
+ using Patches::StringExt
10
+
11
+ @@extensions = [ ExceptionConfigurer.new ]
12
+
13
+ def self.register_extension(extension)
14
+ @@extensions << extension
15
+ end
16
+
17
+ @@format_mapping = { json: JsonConfigurer }
18
+
19
+ def self.register_format(format_type, format_configurer)
20
+ @@format_mapping[format_type.downcase.to_sym] = format_configurer
21
+ end
22
+
23
+ def self.bake(full_name, destination: './', object_mapping: nil, &block)
24
+ name_declaration = full_name.to_s
25
+ .deconstantize
26
+ .split("::")
27
+ .map(&Namespace.method(:new)) << ClientName.new(full_name.to_s.demodulize)
28
+
29
+ context = DslContext.new(@@extensions.dup, @@format_mapping.dup)
30
+ context.instance_eval(&block)
31
+
32
+ if object_mapping
33
+ PoroGenerator.new(object_mapping, name_declaration, destination).generate
34
+ context.extensions << ModelsConfigurer.new(object_mapping)
35
+ context.extensions << ObjectMapperConfigurer.new(context.method_definitions)
36
+ end
37
+
38
+ context.extensions.each {|extension| context.configure(extension) }
39
+
40
+ ApiClientBuilder.new(name_declaration, destination, context).generate
41
+ end
42
+
43
+ class Namespace < String
44
+ def type; 'module' end
45
+ def namespace?; true end
46
+ end
47
+
48
+ class ClientName < String
49
+ def type; 'class' end
50
+ def namespace?; false end
51
+ end
52
+
53
+ private_constant :Namespace, :ClientName
54
+ end
@@ -0,0 +1,82 @@
1
+ require 'forwardable'
2
+ require 'erb'
3
+ require 'fileutils'
4
+ require 'yaml'
5
+
6
+ module Oven
7
+ module NameDeclarationExt
8
+ refine Array do
9
+ alias client_name last
10
+
11
+ def namespace_or_client_name
12
+ size == 1 ? self : select(&:namespace?)
13
+ end
14
+ end
15
+ end
16
+
17
+ class ApiClientBuilder
18
+ using Patches::StringExt
19
+ using NameDeclarationExt
20
+
21
+ attr_reader :name_declaration, :destination, :dsl_context
22
+
23
+ extend Forwardable
24
+ delegate [:method_definitions, :interceptors, :observers, :requires] => :dsl_context
25
+
26
+ def initialize(name_declaration, destination, context)
27
+ @name_declaration, @destination, @dsl_context = name_declaration, destination, context
28
+ end
29
+
30
+ def generate
31
+ FileUtils.mkdir_p(destination)
32
+ ([ApiClientConfigurer.new] + dsl_context.extensions).each do |extension|
33
+ template = open(extension.template_path).read
34
+ path = extension.filename(destination, name_declaration.client_name.underscore)
35
+ code = ERB.new(template, nil, '-').result(binding)
36
+
37
+ puts "generated: #{path}"
38
+ File.write(path, code)
39
+ end
40
+ end
41
+ end
42
+
43
+ # PORO stands for Plain Old Ruby Object: https://en.wikipedia.org/wiki/Plain_Old_Java_Object
44
+ class PoroGenerator
45
+ using Patches::StringExt
46
+ using NameDeclarationExt
47
+
48
+ REVERSED_KEYWORD_MAP = {
49
+ 'self' => 'binding.local_variable_get(:self)',
50
+ 'class' => 'binding.local_variable_get(:class)',
51
+ 'end' => 'binding.local_variable_get(:end)',
52
+ 'if' => 'binding.local_variable_get(:if)',
53
+ 'unless' => 'binding.local_variable_get(:unless)'
54
+ }.freeze
55
+
56
+ PORO_TEMPLATE = open("#{__dir__}/templates/poro.rb.erb").read
57
+ ERB_PROCESSOR = -> (class_name, attributes, name_declaration, reserved_keyword_map) {
58
+ ERB.new(PORO_TEMPLATE, nil, '-').result(binding)
59
+ }
60
+
61
+ def initialize(filepath, name_declaration, destination)
62
+ @data = YAML.load_file(filepath)
63
+ @destination = destination
64
+ @name_declaration = name_declaration
65
+ end
66
+
67
+ def generate
68
+ root_path = File.join(@destination, 'models')
69
+
70
+ FileUtils.mkdir_p(root_path)
71
+ @data.each do |class_name, attributes|
72
+ code = ERB_PROCESSOR.call(class_name, attributes.keys, @name_declaration, REVERSED_KEYWORD_MAP)
73
+ path = File.join(root_path, "#{class_name.underscore}.rb")
74
+
75
+ puts "generated: #{path}"
76
+ File.write(path, code)
77
+ end
78
+ end
79
+ end
80
+
81
+ private_constant :NameDeclarationExt, :ApiClientBuilder, :PoroGenerator
82
+ end
@@ -0,0 +1,88 @@
1
+ require 'oven/path_parser'
2
+
3
+ module Oven
4
+ class DslContext < BasicObject
5
+ attr_reader :method_definitions, :interceptors, :observers, :requires, :extensions
6
+
7
+ def initialize(extensions, format_mapping)
8
+ @method_definitions = []
9
+ @interceptors = []
10
+ @observers = []
11
+ @requires = []
12
+ @extensions = extensions || []
13
+ @format_mapping = format_mapping || {}
14
+ end
15
+
16
+ def format(format)
17
+ @extensions << @format_mapping.fetch(format).new
18
+ end
19
+
20
+ def configure(extension)
21
+ extension.configure_interceptors(@interceptors) if extension.respond_to?(:configure_interceptors)
22
+ extension.configure_observers(@observers) if extension.respond_to?(:configure_observers)
23
+ extension.configure_requires(@requires) if extension.respond_to?(:configure_requires)
24
+ end
25
+
26
+ def get(resource_name, path, as: nil, **options)
27
+ @method_definitions << HttpVerb.new(:get, resource_name, path, as: as, aliases: ["find_#{resource_name}"], has_entity: false, **options)
28
+ end
29
+
30
+ def head(resource_name, path, as: nil, **options)
31
+ @method_definitions << HttpVerb.new(:head, resource_name, path, as: as, aliases: [], has_entity: false, **options)
32
+ end
33
+
34
+ def post(resource_name, path, as: nil, **options)
35
+ @method_definitions << HttpVerb.new(:post, resource_name, path, as: as, aliases: ["create_#{resource_name}"], has_entity: true, **options)
36
+ end
37
+
38
+ def put(resource_name, path, as: nil, **options)
39
+ @method_definitions << HttpVerb.new(:put, resource_name, path, as: as, aliases: ["update_#{resource_name}"], has_entity: true, **options)
40
+ end
41
+
42
+ def patch(resource_name, path, as: nil, **options)
43
+ @method_definitions << HttpVerb.new(:patch, resource_name, path, as: as, aliases: ["update_#{resource_name}"], has_entity: true, **options)
44
+ end
45
+
46
+ def delete(resource_name, path, as: nil, **options)
47
+ @method_definitions << HttpVerb.new(:delete, resource_name, path, as: as, aliases: ["destroy_#{resource_name}"], has_entity: false, **options)
48
+ end
49
+
50
+ def options(resource_name, path, as: nil, **options)
51
+ @method_definitions << HttpVerb.new(:options, resource_name, path, as: as, aliases: [], has_entity: false, **options)
52
+ end
53
+
54
+ class HttpVerb
55
+ attr_reader :verb, :name, :method_name, :aliases, :options
56
+
57
+ def initialize(verb, name, path, as: nil, aliases: [], has_entity: false, **options)
58
+ @verb = verb
59
+ @name = name
60
+ @path_ast = Oven::PathParser.parse(path)
61
+ @method_name = as || "#{verb}_#{name}"
62
+ @aliases = as ? [] : aliases
63
+ @has_entity = has_entity
64
+ @options = options
65
+ end
66
+
67
+ def variable_name_for_body
68
+ @has_entity ? 'body' : 'nil'
69
+ end
70
+
71
+ def path
72
+ @path_ast.to_argument_expression
73
+ end
74
+
75
+ def parameters
76
+ @path_ast.parameters + (@has_entity ? [:body] : [])
77
+ end
78
+
79
+ def parameter_signature
80
+ parameters.join(", ")
81
+ end
82
+ end
83
+
84
+ private_constant :HttpVerb
85
+ end
86
+
87
+ private_constant :DslContext
88
+ end