rails_http_options 0.1.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: f3ef19162093ef69b8a3156754f3678cbbf04524
4
+ data.tar.gz: b8618274ececbaa1af1c4412bb2182b6d994ea31
5
+ SHA512:
6
+ metadata.gz: 9b70176673e7a3efb7bd1dba6317b6af2c3674d06da2dd0f41769312994648cbe85e50420208cdf9f61162ae2a708457a785251644eb0d6293a88a527cb22a00
7
+ data.tar.gz: 521f1ccd9fd4df229a05b5adafb6e16af744c1d2930da3cf2c5aebd5f0daeafa9d6b0427e07a4ffaae3d9248c1e37cf3d3a7d3cbaa014f6662ec7f71c0555dd2
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.1
5
+ before_install: gem install bundler -v 1.15.4
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in rails_http_options.gemspec
6
+ gemspec
@@ -0,0 +1,100 @@
1
+ # RailsHttpOptions
2
+
3
+ Simple gem that allows you to handle HTTP OPTIONS in Rails.
4
+ Ideal for API introspection, like fetching request/response schemas and other
5
+ meaningful for the client information.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'rails_http_options'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install rails_http_options
22
+
23
+ ## Usage
24
+ Include `RailsHttpOptions` in your application controller.
25
+ Then add a Rails route in your routes.rb denoting that you want to handle all
26
+ HTTP OPTIONS in `option` method, defined by this gem.
27
+
28
+ ```ruby
29
+ match '*path', {
30
+ controller: 'application',
31
+ action: 'options',
32
+ constraints: { method: 'OPTIONS' },
33
+ via: [:options]
34
+ }
35
+ ```
36
+
37
+ Then in any of your resource-based controller, add your options response:
38
+
39
+ ```ruby
40
+ class Api::V1::UsersController < ApplicationController
41
+ options do
42
+ {
43
+ schemas: {
44
+ accepts: Company.json_schema,
45
+ returns: Company.json_schema
46
+ },
47
+ meta: { max_per_page: 100 }
48
+ }
49
+ end
50
+ end
51
+ ```
52
+
53
+ You can also respond differently depending on request information.
54
+
55
+ Specifically, you get route details param
56
+ coming from Rails routing mechanism and it's a simple hash.
57
+ However, you have access to the regular request/response context inside the block,
58
+ because just before is being called its context is changed to a controller's method,
59
+ defined by this gem. Hence, you can access
60
+ [request](http://api.rubyonrails.org/classes/ActionDispatch/Request.html),
61
+ [response](http://api.rubyonrails.org/v5.0.1/classes/ActionDispatch/Response.html),
62
+ [params](http://api.rubyonrails.org/classes/ActionController/Parameters.html) etc,
63
+ like a regular controller method.
64
+
65
+
66
+ ```ruby
67
+ class Api::V1::UsersController < ApplicationController
68
+ options do |route_details|
69
+ if route_details[:id] #member route
70
+ {
71
+ schemas: { #params is available through context switching
72
+ accepts: Company.find(params[:id]).introspection_schema,
73
+ returns: Company.find(params[:id]).introspection_schema,
74
+ }
75
+ }
76
+ else #collection route
77
+ {
78
+ schemas: {
79
+ returns: [Company.introspection_schema]
80
+ },
81
+ meta: { max_per_page: 100 }
82
+ }
83
+ end
84
+ end
85
+ end
86
+ ```
87
+
88
+ The response is always in JSON, but if you need something else (like yaml or XML),
89
+ you can always override the default
90
+ [`options`](https://github.com/kollegorna/rails_http_options/blob/master/lib/rails_http_options.rb#L16-L25) method.
91
+
92
+ ## Development
93
+
94
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
95
+
96
+ 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).
97
+
98
+ ## Contributing
99
+
100
+ Bug reports and pull requests are welcome on GitHub at https://github.com/kollegorna/rails_http_options.
@@ -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 "rails_http_options"
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(__FILE__)
@@ -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,53 @@
1
+ require "rails_http_options/version"
2
+
3
+ module RailsHttpOptions
4
+ def self.included(base)
5
+ base.extend(ClassMethods)
6
+ end
7
+
8
+ module ClassMethods
9
+ def options(&options_block)
10
+ @options_block = options_block if block_given?
11
+
12
+ return @options_block
13
+ end
14
+ end
15
+
16
+ def options
17
+ return head :ok if controller_for(url: request.url).options.nil?
18
+
19
+ return render({
20
+ json: instance_exec(
21
+ route_details_for(request.url),
22
+ &controller_for(url: request.url).options
23
+ )
24
+ })
25
+ end
26
+
27
+ private
28
+ def controller_for(url:)
29
+ route_details = route_details_for(url)
30
+ name = route_details[:controller].titleize.gsub('/', '::').gsub(' ','')
31
+
32
+ return "#{name}Controller".constantize
33
+ end
34
+
35
+ def route_details_for(url)
36
+ @route_details ||= begin
37
+ methods = [:get, :post, :put, :patch, :delete]
38
+ method = methods[0]
39
+ tries ||= 0
40
+ route_details = nil
41
+ begin
42
+ route_details = Rails.application.routes.recognize_path(url, method: method)
43
+ raise ActionController::RoutingError, '' if route_details[:action] == 'route_not_found'
44
+ rescue ActionController::RoutingError => _
45
+ method = methods[tries]
46
+ retry unless (tries += 1) == 5
47
+ else
48
+ return route_details
49
+ end
50
+ end
51
+ end
52
+
53
+ end
@@ -0,0 +1,3 @@
1
+ module RailsHttpOptions
2
+ VERSION = "0.1.0"
3
+ end
@@ -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
+ require "rails_http_options/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rails_http_options"
8
+ spec.version = RailsHttpOptions::VERSION
9
+ spec.authors = ["Filippos Vasilakis", "Kollegorna"]
10
+ spec.email = ["vasilakisfil@gmail.com"]
11
+
12
+ spec.summary = %q{Simple gem that makes it easy to handle HTTP OPTIONS requests in Rails.}
13
+ spec.description = %q{Simple gem that makes it easy to handle HTTP OPTIONS requests in Rails.}
14
+ spec.homepage = "https://github.com/kollegorna/rails_http_options"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_development_dependency "Rails", "> 3.2.0"
24
+ spec.add_development_dependency "bundler", "~> 1.15"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec", "~> 3.0"
27
+ end
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails_http_options
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Filippos Vasilakis
8
+ - Kollegorna
9
+ autorequire:
10
+ bindir: exe
11
+ cert_chain: []
12
+ date: 2017-12-11 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: Rails
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">"
19
+ - !ruby/object:Gem::Version
20
+ version: 3.2.0
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">"
26
+ - !ruby/object:Gem::Version
27
+ version: 3.2.0
28
+ - !ruby/object:Gem::Dependency
29
+ name: bundler
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '1.15'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '1.15'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rake
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: '10.0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: '10.0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: rspec
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - "~>"
61
+ - !ruby/object:Gem::Version
62
+ version: '3.0'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: '3.0'
70
+ description: Simple gem that makes it easy to handle HTTP OPTIONS requests in Rails.
71
+ email:
72
+ - vasilakisfil@gmail.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - ".rspec"
79
+ - ".travis.yml"
80
+ - Gemfile
81
+ - README.md
82
+ - Rakefile
83
+ - bin/console
84
+ - bin/setup
85
+ - lib/rails_http_options.rb
86
+ - lib/rails_http_options/version.rb
87
+ - rails_http_options.gemspec
88
+ homepage: https://github.com/kollegorna/rails_http_options
89
+ licenses: []
90
+ metadata: {}
91
+ post_install_message:
92
+ rdoc_options: []
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ requirements: []
106
+ rubyforge_project:
107
+ rubygems_version: 2.6.12
108
+ signing_key:
109
+ specification_version: 4
110
+ summary: Simple gem that makes it easy to handle HTTP OPTIONS requests in Rails.
111
+ test_files: []