ruboty-rurema 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: e8e3e12d8a36e5dc4f5aa2a6c1b1a7b5dbbece4db23474f346f40a31aa168cb2
4
+ data.tar.gz: a793d127cf88ca99c59b9915cdbfe44911b6c37b429a055e69b3a5cb25522fa1
5
+ SHA512:
6
+ metadata.gz: ff25b0cd3514ef6f241abf3eac99e070dbd2985f99c3b76e3a19d0e081b722e51decb5956c4b8b7d2464c7e993b82a69ade34d3bf0fc74eefddf18005c7a8576
7
+ data.tar.gz: fe3993adb4a312165495a02bfcaf091d1d3ee77ee94eaac21f860a2dd17c578eadc4bf1c5d8aedf5a6e28b2c8b7f1cf69d75b3ead323c9748fccde1e311a467d
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ /Gemfile.lock
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in ruboty-rurema.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
data/LICENSE ADDED
@@ -0,0 +1,116 @@
1
+ CC0 1.0 Universal
2
+
3
+ Statement of Purpose
4
+
5
+ The laws of most jurisdictions throughout the world automatically confer
6
+ exclusive Copyright and Related Rights (defined below) upon the creator and
7
+ subsequent owner(s) (each and all, an "owner") of an original work of
8
+ authorship and/or a database (each, a "Work").
9
+
10
+ Certain owners wish to permanently relinquish those rights to a Work for the
11
+ purpose of contributing to a commons of creative, cultural and scientific
12
+ works ("Commons") that the public can reliably and without fear of later
13
+ claims of infringement build upon, modify, incorporate in other works, reuse
14
+ and redistribute as freely as possible in any form whatsoever and for any
15
+ purposes, including without limitation commercial purposes. These owners may
16
+ contribute to the Commons to promote the ideal of a free culture and the
17
+ further production of creative, cultural and scientific works, or to gain
18
+ reputation or greater distribution for their Work in part through the use and
19
+ efforts of others.
20
+
21
+ For these and/or other purposes and motivations, and without any expectation
22
+ of additional consideration or compensation, the person associating CC0 with a
23
+ Work (the "Affirmer"), to the extent that he or she is an owner of Copyright
24
+ and Related Rights in the Work, voluntarily elects to apply CC0 to the Work
25
+ and publicly distribute the Work under its terms, with knowledge of his or her
26
+ Copyright and Related Rights in the Work and the meaning and intended legal
27
+ effect of CC0 on those rights.
28
+
29
+ 1. Copyright and Related Rights. A Work made available under CC0 may be
30
+ protected by copyright and related or neighboring rights ("Copyright and
31
+ Related Rights"). Copyright and Related Rights include, but are not limited
32
+ to, the following:
33
+
34
+ i. the right to reproduce, adapt, distribute, perform, display, communicate,
35
+ and translate a Work;
36
+
37
+ ii. moral rights retained by the original author(s) and/or performer(s);
38
+
39
+ iii. publicity and privacy rights pertaining to a person's image or likeness
40
+ depicted in a Work;
41
+
42
+ iv. rights protecting against unfair competition in regards to a Work,
43
+ subject to the limitations in paragraph 4(a), below;
44
+
45
+ v. rights protecting the extraction, dissemination, use and reuse of data in
46
+ a Work;
47
+
48
+ vi. database rights (such as those arising under Directive 96/9/EC of the
49
+ European Parliament and of the Council of 11 March 1996 on the legal
50
+ protection of databases, and under any national implementation thereof,
51
+ including any amended or successor version of such directive); and
52
+
53
+ vii. other similar, equivalent or corresponding rights throughout the world
54
+ based on applicable law or treaty, and any national implementations thereof.
55
+
56
+ 2. Waiver. To the greatest extent permitted by, but not in contravention of,
57
+ applicable law, Affirmer hereby overtly, fully, permanently, irrevocably and
58
+ unconditionally waives, abandons, and surrenders all of Affirmer's Copyright
59
+ and Related Rights and associated claims and causes of action, whether now
60
+ known or unknown (including existing as well as future claims and causes of
61
+ action), in the Work (i) in all territories worldwide, (ii) for the maximum
62
+ duration provided by applicable law or treaty (including future time
63
+ extensions), (iii) in any current or future medium and for any number of
64
+ copies, and (iv) for any purpose whatsoever, including without limitation
65
+ commercial, advertising or promotional purposes (the "Waiver"). Affirmer makes
66
+ the Waiver for the benefit of each member of the public at large and to the
67
+ detriment of Affirmer's heirs and successors, fully intending that such Waiver
68
+ shall not be subject to revocation, rescission, cancellation, termination, or
69
+ any other legal or equitable action to disrupt the quiet enjoyment of the Work
70
+ by the public as contemplated by Affirmer's express Statement of Purpose.
71
+
72
+ 3. Public License Fallback. Should any part of the Waiver for any reason be
73
+ judged legally invalid or ineffective under applicable law, then the Waiver
74
+ shall be preserved to the maximum extent permitted taking into account
75
+ Affirmer's express Statement of Purpose. In addition, to the extent the Waiver
76
+ is so judged Affirmer hereby grants to each affected person a royalty-free,
77
+ non transferable, non sublicensable, non exclusive, irrevocable and
78
+ unconditional license to exercise Affirmer's Copyright and Related Rights in
79
+ the Work (i) in all territories worldwide, (ii) for the maximum duration
80
+ provided by applicable law or treaty (including future time extensions), (iii)
81
+ in any current or future medium and for any number of copies, and (iv) for any
82
+ purpose whatsoever, including without limitation commercial, advertising or
83
+ promotional purposes (the "License"). The License shall be deemed effective as
84
+ of the date CC0 was applied by Affirmer to the Work. Should any part of the
85
+ License for any reason be judged legally invalid or ineffective under
86
+ applicable law, such partial invalidity or ineffectiveness shall not
87
+ invalidate the remainder of the License, and in such case Affirmer hereby
88
+ affirms that he or she will not (i) exercise any of his or her remaining
89
+ Copyright and Related Rights in the Work or (ii) assert any associated claims
90
+ and causes of action with respect to the Work, in either case contrary to
91
+ Affirmer's express Statement of Purpose.
92
+
93
+ 4. Limitations and Disclaimers.
94
+
95
+ a. No trademark or patent rights held by Affirmer are waived, abandoned,
96
+ surrendered, licensed or otherwise affected by this document.
97
+
98
+ b. Affirmer offers the Work as-is and makes no representations or warranties
99
+ of any kind concerning the Work, express, implied, statutory or otherwise,
100
+ including without limitation warranties of title, merchantability, fitness
101
+ for a particular purpose, non infringement, or the absence of latent or
102
+ other defects, accuracy, or the present or absence of errors, whether or not
103
+ discoverable, all to the greatest extent permissible under applicable law.
104
+
105
+ c. Affirmer disclaims responsibility for clearing rights of other persons
106
+ that may apply to the Work or any use thereof, including without limitation
107
+ any person's Copyright and Related Rights in the Work. Further, Affirmer
108
+ disclaims responsibility for obtaining any necessary consents, permissions
109
+ or other rights required for any use of the Work.
110
+
111
+ d. Affirmer understands and acknowledges that Creative Commons is not a
112
+ party to this document and has no duty or obligation with respect to this
113
+ CC0 or use of the Work.
114
+
115
+ For more information, please see
116
+ <http://creativecommons.org/publicdomain/zero/1.0/>
data/README.md ADDED
@@ -0,0 +1,65 @@
1
+ # Ruboty::Rurema
2
+
3
+ Search the Ruby Reference Manual via Ruboty.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'ruboty-rurema'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install ruboty-rurema
20
+
21
+ ## Usage
22
+
23
+ Ruboty is aware of `Klass`, `Klass#method`, `Klass.method`, and `Klass.#method`.
24
+
25
+ ```
26
+ you > @ruboty rurema Enumerable
27
+ ruboty > https://docs.ruby-lang.org/ja/latest/class/Enumerable.html
28
+
29
+ you > @ruboty rurema Object#then
30
+ ruboty > https://docs.ruby-lang.org/ja/latest/method/Object/i/then.html
31
+
32
+ you > @ruboty rurema JSON.parse
33
+ ruboty > https://docs.ruby-lang.org/ja/2.6.0/method/JSON/m/parse.html
34
+
35
+ you > @ruboty rurema Math.#sin
36
+ ruboty > https://docs.ruby-lang.org/ja/latest/method/Math/m/sin.html
37
+ ```
38
+
39
+ Ruboty is also aware of free style text. It passes to https://docs.ruby-lang.org/ja/search/ as a query.
40
+
41
+ ```
42
+ you > @ruboty rurema 正規表現
43
+ ruboty > https://docs.ruby-lang.org/ja/2.6.0/doc/spec=2fregexp.html
44
+
45
+ you > @ruboty rurema then
46
+ ruboty > https://docs.ruby-lang.org/ja/2.6.0/method/Object/i/then.html
47
+ ```
48
+
49
+ ## Development
50
+
51
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
52
+
53
+ 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).
54
+
55
+ ## Contributing
56
+
57
+ Bug reports and pull requests are welcome on GitHub at https://github.com/pocke/ruboty-rurema.
58
+
59
+
60
+ ## License
61
+
62
+
63
+ These codes are licensed under CC0, except code that is copied from [bitclust](https://github.com/rurema/bitclust/blob/ba4b07e05ae41694e4930d7be355d32e9f17f396/lib/bitclust/nameutils.rb#L205-L208).
64
+
65
+ [![CC0](http://i.creativecommons.org/p/zero/1.0/88x31.png "CC0")](http://creativecommons.org/publicdomain/zero/1.0/deed.en)
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "ruboty/rurema"
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__)
data/bin/setup ADDED
@@ -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,5 @@
1
+ module Ruboty
2
+ module Rurema
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,108 @@
1
+ require "ruboty/rurema/version"
2
+
3
+ require 'net/https'
4
+ require 'uri'
5
+ require 'json'
6
+ require 'open-uri'
7
+ require 'cgi'
8
+
9
+ module Ruboty
10
+ module Handlers
11
+ class Rurema < Base
12
+ URL_BASE = 'https://docs.ruby-lang.org/ja/latest/'
13
+ SEARCH_API_URL_BASE = 'https://docs.ruby-lang.org/ja/search/api:v1/'
14
+ CLASS_RE = /[A-Z][\w:]+/
15
+
16
+ on(
17
+ /rurema\s+(?<query>.+)/i,
18
+ name: 'rurema',
19
+ description: "Response the Ruby reference manual",
20
+ )
21
+
22
+ def rurema(message)
23
+ query = message.match_data['query']
24
+ url = build_url(query)
25
+ if url && exist_resource?(url)
26
+ message.reply url
27
+ return
28
+ end
29
+
30
+ # Is CGI.escape correct way?
31
+ resp = get SEARCH_API_URL_BASE + query.split.map {|q| "query:#{CGI.escape(q)}"}.join('/')
32
+ if resp.nil? || resp[:entries].empty?
33
+ message.reply "No document for #{query}"
34
+ return
35
+ end
36
+
37
+ url = resp.dig(:entries, 0, :documents, -1, :url)
38
+ # BUG: rurema searcher returns URL with 'https://docs.ruby-lang.org/ja/search/', so we need removing it.
39
+ url.sub!('docs.ruby-lang.org/ja/search/http://', '')
40
+ message.reply url
41
+ end
42
+
43
+ private def build_url(query)
44
+ case
45
+ when klass = class?(query)
46
+ URL_BASE + "class/#{encode_name(klass)}.html"
47
+ when ok = module_method?(query)
48
+ klass = encode_name ok[0]
49
+ method = encode_name ok[1]
50
+ URL_BASE + "method/#{klass}/m/#{method}.html"
51
+ when ok = instance_method?(query)
52
+ klass = encode_name ok[0]
53
+ method = encode_name ok[1]
54
+ URL_BASE + "method/#{klass}/i/#{method}.html"
55
+ when ok = class_method?(query)
56
+ klass = encode_name ok[0]
57
+ method = encode_name ok[1]
58
+ URL_BASE + "method/#{klass}/s/#{method}.html"
59
+ else
60
+ nil
61
+ end
62
+ end
63
+
64
+ private def class?(query)
65
+ query[/\A#{CLASS_RE}\z/]
66
+ end
67
+
68
+ private def module_method?(query)
69
+ m = /\A(?<class>#{CLASS_RE})\.\#(?<method>.+)\z/.match(query)
70
+ [m['class'], m['method']] if m
71
+ end
72
+
73
+ private def instance_method?(query)
74
+ m = /\A(?<class>#{CLASS_RE})\#(?<method>.+)\z/.match(query)
75
+ [m['class'], m['method']] if m
76
+ end
77
+
78
+ private def class_method?(query)
79
+ m = /\A(?<class>#{CLASS_RE})\.(?<method>.+)\z/.match(query)
80
+ [m['class'], m['method']] if m
81
+ end
82
+
83
+ private def exist_resource?(url)
84
+ url = URI.parse(url)
85
+ https = Net::HTTP.new(url.host, 443)
86
+ https.use_ssl = true
87
+ https.start do
88
+ resp = https.head(url.path)
89
+ return resp.code_type == Net::HTTPOK
90
+ end
91
+ end
92
+
93
+ # It is copied and modified from bitclust.
94
+ # The license for the method follows bitclust's one.
95
+ # https://github.com/rurema/bitclust/blob/ba4b07e05ae41694e4930d7be355d32e9f17f396/lib/bitclust/nameutils.rb#L205-L208
96
+ private def encode_name(name)
97
+ name.gsub(/[^A-Za-z0-9_]/n) {|ch| sprintf('=%02x', ch.ord) }
98
+ end
99
+
100
+ private def get(url)
101
+ JSON.parse(URI.open(url).read, symbolize_names: true)
102
+ rescue OpenURI::HTTPError => ex
103
+ p ex
104
+ nil
105
+ end
106
+ end
107
+ end
108
+ end
@@ -0,0 +1,30 @@
1
+ require_relative 'lib/ruboty/rurema/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "ruboty-rurema"
5
+ spec.version = Ruboty::Rurema::VERSION
6
+ spec.authors = ["Masataka Pocke Kuwabara"]
7
+ spec.email = ["kuwabara@pocke.me"]
8
+
9
+ spec.summary = %q{Search the Ruby Reference Manual via Ruboty}
10
+ spec.description = %q{Search the Ruby Reference Manual via Ruboty}
11
+ spec.homepage = "https://github.com/pocke/ruboty-rurema"
12
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
13
+
14
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
15
+
16
+ spec.metadata["homepage_uri"] = spec.homepage
17
+ spec.metadata["source_code_uri"] = spec.homepage
18
+ # spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
19
+
20
+ # Specify which files should be added to the gem when it is released.
21
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
22
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
23
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
24
+ end
25
+ spec.bindir = "exe"
26
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
27
+ spec.require_paths = ["lib"]
28
+
29
+ spec.add_runtime_dependency "ruboty"
30
+ end
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruboty-rurema
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Masataka Pocke Kuwabara
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-08-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: ruboty
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: Search the Ruby Reference Manual via Ruboty
28
+ email:
29
+ - kuwabara@pocke.me
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".gitignore"
35
+ - Gemfile
36
+ - LICENSE
37
+ - README.md
38
+ - Rakefile
39
+ - bin/console
40
+ - bin/setup
41
+ - lib/ruboty/rurema.rb
42
+ - lib/ruboty/rurema/version.rb
43
+ - ruboty-rurema.gemspec
44
+ homepage: https://github.com/pocke/ruboty-rurema
45
+ licenses: []
46
+ metadata:
47
+ allowed_push_host: https://rubygems.org
48
+ homepage_uri: https://github.com/pocke/ruboty-rurema
49
+ source_code_uri: https://github.com/pocke/ruboty-rurema
50
+ post_install_message:
51
+ rdoc_options: []
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: 2.3.0
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ requirements: []
65
+ rubygems_version: 3.0.3
66
+ signing_key:
67
+ specification_version: 4
68
+ summary: Search the Ruby Reference Manual via Ruboty
69
+ test_files: []