wtf_ruby 0.1.2

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: 73becb7f84fda3a2405da82124ef29b69a035e52aaf2bccc67fa21a1b74aa148
4
+ data.tar.gz: 6803b171f4d496b507e244d8b973739f8642ae7a271005bb34844a91e3825a3e
5
+ SHA512:
6
+ metadata.gz: 10c34a2d596302d1ab5c949443cb5cfe56093b6d41412f10bd787c4a6bcaed45fe65f269e15399343e1239807b4bc07ef10f2de4152f8aac025fa1447fb19e22
7
+ data.tar.gz: 3de18210c09b30de304e2dbe3bce6227af4e1cbd4fa2467cdb40b7979c9f946b2064249fad1014e47d3c362a0464add3ccfed71b14b716cd70163ef88360b854
data/bin/wtf_ruby ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require_relative "../lib/wtf_ruby"
4
+
5
+ # http://127.0.0.1:4000/
6
+ sesh = WtfRuby::CommandLineInteface.new
7
+ sesh.run
@@ -0,0 +1,10 @@
1
+ require 'pry'
2
+ require 'nokogiri'
3
+ require 'open-uri'
4
+ require 'colorize'
5
+ require 'fileutils'
6
+
7
+ require_relative "../lib/wtf_ruby/command_line_interface.rb"
8
+ require_relative "../lib/wtf_ruby/scraper.rb"
9
+ require_relative "../lib/wtf_ruby/class.rb"
10
+ require_relative "../lib/wtf_ruby/meth.rb"
@@ -0,0 +1,51 @@
1
+
2
+ require 'open-uri'
3
+
4
+ class WtfRuby::Classy
5
+
6
+ attr_accessor :name, :methods, :content
7
+
8
+ @@all = []
9
+
10
+ def initialize(name)
11
+ @name = name
12
+ @@all << self
13
+ end
14
+
15
+ def self.all
16
+ @@all
17
+ end
18
+
19
+ def self.create_from_collection(list_of_classes)
20
+
21
+ list_of_classes.each do |class_name|
22
+ self.new(class_name)
23
+
24
+ end
25
+ end
26
+
27
+ def self.create_methods_for_all_classes
28
+ self.all.each do |ind_class|
29
+ ind_class.create_methods_for_instance_of_class
30
+ end
31
+ end
32
+
33
+
34
+ def create_methods_for_instance_of_class
35
+ method_names = WtfRuby::Scraper.scrape_methods(@name)
36
+ method_contents = WtfRuby::Scraper.scrape_method_content_from_class_page(@name)
37
+ index_placeholder = 0
38
+ @methods = []
39
+ method_contents.each do |content|
40
+ new_method_of_class = WtfRuby::Meth.new
41
+ new_method_of_class.name = method_names[index_placeholder]
42
+ new_method_of_class.headings = content[:headings].split('click to toggle source')
43
+ new_method_of_class.description = content[:full_description]
44
+ new_method_of_class.mini_description = content[:mini_description]
45
+ new_method_of_class.sample_code = content[:code]
46
+ @methods << new_method_of_class
47
+ index_placeholder += 1
48
+ end
49
+ end
50
+
51
+ end
@@ -0,0 +1,144 @@
1
+
2
+ require 'nokogiri'
3
+ require 'colorize'
4
+ require 'pry'
5
+ require 'open-uri'
6
+ require 'fileutils'
7
+ class WtfRuby::CommandLineInteface
8
+
9
+
10
+
11
+ def run
12
+ make_classes
13
+ width_check
14
+ WtfRuby::Scraper.store_offline
15
+ WtfRuby::Classy.create_methods_for_all_classes
16
+
17
+
18
+ welcome_text
19
+ first_choice = gets.strip
20
+ if validate_first_choice_input(first_choice) == { "valid class" => true, "valid method" => true, "inputs" => 2}
21
+ class_choice = first_choice.split(',')[0].strip
22
+ selected_class = set_class(class_choice)
23
+ method_choice = first_choice.split(',')[1].strip
24
+ display_method(selected_class, method_choice)
25
+ elsif validate_first_choice_input(first_choice) == { "valid class" => true, "valid method" => false, "inputs" => 1}
26
+ selected_class = set_class(first_choice)
27
+ printf_method_list(selected_class)
28
+ puts 'Please enter the method you wish to view:'
29
+ method_choice = gets.strip
30
+ display_method(selected_class, method_choice)
31
+ elsif validate_first_choice_input(first_choice) == { "valid class" => true, "valid method" => false, "inputs" => 2}
32
+ selected_class = set_class(first_choice.split(',')[0].strip)
33
+ printf_method_list(selected_class)
34
+ puts 'Please enter the method you wish to view:'
35
+ method_choice = gets.strip
36
+ display_method(selected_class, method_choice)
37
+ else
38
+ puts "Hmmmm... I couldn't make sense of your input. Let's try this" if first_choice != ''
39
+ printf_class_list
40
+ puts 'Please enter the name of the class for which you wish to view available methods:'
41
+ class_choice= gets.strip
42
+ selected_class = set_class(class_choice)
43
+ printf_method_list(selected_class)
44
+ puts 'Please enter the method you wish to view:'
45
+ method_choice = gets.strip
46
+ display_method(selected_class, method_choice)
47
+
48
+ end
49
+ end
50
+
51
+ #return a hash that identifies whether the user provided class and method are valid.
52
+ def validate_first_choice_input(string)
53
+ inputs = { "valid class" => false, "valid method" => false, "inputs" => 0 }
54
+ if set_class(string)
55
+ inputs["valid class"] = true
56
+ inputs["inputs"] = 1
57
+ elsif string.include?(',')
58
+ array = string.split(',')
59
+ pot_class = array[0].strip
60
+ pot_method = array[1].strip
61
+ inputs["inputs"] = array.count
62
+ if set_class(pot_class)
63
+ inputs["valid class"] = true
64
+ if set_method(set_class(pot_class), pot_method)
65
+ inputs["valid method"] = true
66
+ end
67
+ end
68
+ end
69
+ inputs
70
+ end
71
+
72
+ def width_check
73
+ puts 'This gem displays some pretty big lists. To make sure your terminal window is wide enough expand the view until you have a continuous line on the right side'
74
+ 5.times do
75
+ printf(" %80s \n", "|")
76
+ end
77
+ end
78
+
79
+
80
+ def welcome_text
81
+ puts "Hello, welcome to what_dis_ruby?. If you know the class AND method OR just the class that you wish to look up please enter them as shown below:"
82
+ puts "Classy and Method: Classy, Method"
83
+ puts "Just Classy: Classy"
84
+ puts "Othwerwise, hit enter to see a list of available classes"
85
+ end
86
+
87
+
88
+
89
+ def set_method(class_instance, chosen_method_name)
90
+ class_instance.methods.select { |m| m.name == chosen_method_name}.first
91
+ end
92
+
93
+ def set_class(string)
94
+ WtfRuby::Classy.all.select { |c| c.name == string}.first
95
+ end
96
+
97
+ def display_method(class_instance, chosen_method_name)
98
+ m_to_show = set_method(class_instance, chosen_method_name)
99
+ puts ''
100
+ puts ' Classy: ' + class_instance.name.colorize(:mode => :bold) + ' Method: ' + m_to_show.name.colorize(:color => :red, :mode => :bold)
101
+ printf("\t %s \n", m_to_show.mini_description.colorize(:mode => :italic))
102
+ m_to_show.headings.each do |heading|
103
+ printf("\t#{heading.colorize(:light_blue)}\n")
104
+ end
105
+ m_to_show.sample_code.each do |code|
106
+ line = code.colorize(:color => :white, :background => :green)
107
+ printf("\t %-50s \n", line)
108
+ end
109
+ end
110
+
111
+
112
+ def printf_method_list(selected_class)
113
+ counter = 0
114
+ rows = selected_class.methods.count/3
115
+ rows.ceil.times do
116
+ printf(" %2d.%-25s %2d.%-25s %2d.%-25s \n",
117
+ counter += 1, selected_class.methods[counter - 1].name,
118
+ counter += 1, selected_class.methods[counter - 1].name,
119
+ counter += 1, selected_class.methods[counter - 1].name)
120
+ end
121
+ end
122
+
123
+
124
+
125
+ def make_classes
126
+ class_list = WtfRuby::Scraper.scrape_class
127
+ WtfRuby::Classy.create_from_collection(class_list)
128
+
129
+ end
130
+
131
+
132
+ def printf_class_list
133
+ counter = 0
134
+ rows = WtfRuby::Classy.all.count/4
135
+ rows.ceil.times do
136
+ printf(" %2d.%-15s %2d.%-15s %2d.%-15s %2d.%-15s \n",
137
+ counter += 1, WtfRuby::Classy.all[counter - 1].name,
138
+ counter += 1, WtfRuby::Classy.all[counter - 1].name,
139
+ counter += 1, WtfRuby::Classy.all[counter - 1].name,
140
+ counter += 1, WtfRuby::Classy.all[counter - 1].name)
141
+ end
142
+ end
143
+
144
+ end
@@ -0,0 +1,9 @@
1
+ class WtfRuby::Meth
2
+ #one name, one or more uses, one or more sample_codes
3
+ attr_accessor :name, :headings, :sample_code, :mini_description, :description
4
+
5
+
6
+
7
+
8
+
9
+ end
@@ -0,0 +1,114 @@
1
+ require 'open-uri'
2
+ require 'pry'
3
+ require 'nokogiri'
4
+ require 'fileutils'
5
+
6
+
7
+
8
+ class WtfRuby::Scraper
9
+ @@local_status = false
10
+ @@dir_two = File.dirname("./fixtures/ruby-doc-site")
11
+
12
+ #Sets the path for scraping based on the presence of the fixtures directory
13
+ if File.directory?(@@dir_two)
14
+ @@core_path = "./fixtures/ruby-doc-site"
15
+ @@file_type = ".xml"
16
+ @@local_status = true
17
+ else
18
+ @@core_path = "https://ruby-doc.org/core-2.3.1"
19
+ @@file_type = ".html"
20
+ @@local_status = false
21
+
22
+ end
23
+
24
+
25
+ def self.local_status
26
+ @@local_status
27
+ end
28
+
29
+
30
+
31
+
32
+
33
+
34
+ def self.scrape_class
35
+ class_names = []
36
+ if @@local_status == true
37
+ core_link = "#{@@core_path}/core-2_3_1.xml"
38
+ else
39
+ core_link = @@core_path
40
+ end
41
+
42
+ core_page = Nokogiri::HTML(open(core_link))
43
+ core_page.xpath("//div [@id='class-index']/div[2]/p/a").each do |class_name|
44
+ if !class_name.children.text.include?("::") && !class_name.children.text.include?("Error") && !class_name.children.text.include?("System")
45
+ class_names << class_name.text
46
+
47
+ end
48
+ end
49
+ class_names
50
+ end
51
+
52
+ def self.scrape_methods(class_instance_name)
53
+ # to break up the method sections use //div [@id='at-method'] [@class='method-detail']
54
+ methods = []
55
+ method_link = "#{@@core_path}/#{class_instance_name}#{@@file_type}"
56
+ method_page = Nokogiri::HTML(open(method_link))
57
+ method_page.xpath("//div[@id='method-list-section']/ul/li/a").each do |method_name|
58
+ name = method_name.text.gsub(/[:#]/,'')
59
+ methods << name
60
+ end
61
+ methods
62
+ end
63
+
64
+ def self.scrape_method_content_from_class_page(class_name)
65
+ methods = []
66
+ method_link = @@core_path+"/#{class_name}#{@@file_type}"
67
+ method_page = Nokogiri::HTML(open(method_link))
68
+ method_page.css(".method-detail").each do |section|
69
+ method_hash = {}
70
+ method_hash[:headings] =section.xpath("div [@class='method-heading'] / span").text
71
+ method_hash[:mini_description] = section.xpath("div / p[1]").text.split("\n").join(' ')
72
+ method_hash[:full_description] = section.xpath("div / p").text
73
+ method_hash[:code] = section.css(".ruby").text.split("\n")
74
+ methods << method_hash
75
+ end
76
+ methods
77
+ end
78
+
79
+ def self.store_offline #Only run after making all the classes
80
+ if @@local_status == false
81
+ dir_one = File.dirname("./fixtures")
82
+ unless File.directory?(dir_one)
83
+ FileUtils.mkdir_p(dir_one)
84
+ end
85
+
86
+ dir_two = File.dirname("./fixtures/ruby-doc-site")
87
+ unless File.directory?(dir_two)
88
+ FileUtils.mkdir_p(dir_two)
89
+ end
90
+
91
+ dir_three = File.dirname("./fixtures/ruby-doc-site/why")
92
+ unless File.directory?(dir_three)
93
+ FileUtils.mkdir_p(dir_three)
94
+ end
95
+
96
+ core_file = File.new("./fixtures/ruby-doc-site/core-2_3_1.xml", 'w')
97
+ home = open("https://ruby-doc.org/core-2.3.1")
98
+ doc_home = Nokogiri::HTML(home)
99
+ core_file.write(doc_home)
100
+ core_file.close
101
+
102
+
103
+ WtfRuby::Classy.all.each do |ind_class|
104
+ name = ind_class.name
105
+ t = File.new("./fixtures/ruby-doc-site/#{name}.xml", 'w')
106
+ html = open("https://ruby-doc.org/core-2.3.1/#{name}.html")
107
+ doc = Nokogiri::HTML(html)
108
+ t.write(doc)
109
+ t.close
110
+ end
111
+ end
112
+ end
113
+
114
+ end
data/lib/wtf_ruby.rb ADDED
@@ -0,0 +1,6 @@
1
+
2
+
3
+ module WtfRuby
4
+
5
+ end
6
+ require_relative '../config/environment.rb'
metadata ADDED
@@ -0,0 +1,136 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wtf_ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ platform: ruby
6
+ authors:
7
+ - JerYoMat
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-12-22 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.17'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.17'
27
+ - !ruby/object:Gem::Dependency
28
+ name: pry
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: colorize
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: nokogiri
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: A simple gem to view ruby classes, their methods and sample code within
98
+ the command line.
99
+ email:
100
+ - jeremy.emata@gmail.com
101
+ executables:
102
+ - wtf_ruby
103
+ extensions: []
104
+ extra_rdoc_files: []
105
+ files:
106
+ - bin/wtf_ruby
107
+ - config/environment.rb
108
+ - lib/wtf_ruby.rb
109
+ - lib/wtf_ruby/class.rb
110
+ - lib/wtf_ruby/command_line_interface.rb
111
+ - lib/wtf_ruby/meth.rb
112
+ - lib/wtf_ruby/scraper.rb
113
+ homepage: ''
114
+ licenses:
115
+ - MIT
116
+ metadata: {}
117
+ post_install_message:
118
+ rdoc_options: []
119
+ require_paths:
120
+ - lib
121
+ required_ruby_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ required_rubygems_version: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ requirements: []
132
+ rubygems_version: 3.0.0
133
+ signing_key:
134
+ specification_version: 4
135
+ summary: Get Ruby Doc class descriptions, methods and sample code.
136
+ test_files: []