chid 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.
Files changed (44) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +11 -0
  3. data/Gemfile +4 -0
  4. data/Gemfile.lock +67 -0
  5. data/README.md +221 -0
  6. data/Rakefile +17 -0
  7. data/chid.gemspec +32 -0
  8. data/etc/img/stack.png +0 -0
  9. data/lib/chid.rb +100 -0
  10. data/lib/chid/chid_config.rb +127 -0
  11. data/lib/chid/currency_api.rb +69 -0
  12. data/lib/chid/github_api.rb +41 -0
  13. data/lib/chid/main.rb +112 -0
  14. data/lib/chid/news_api.rb +60 -0
  15. data/lib/chid/paginator.rb +57 -0
  16. data/lib/chid/stack_overflow_api.rb +35 -0
  17. data/lib/chid/version.rb +3 -0
  18. data/lib/chid/yandex_translate_api.rb +39 -0
  19. data/tasks/chid/config.rake +5 -0
  20. data/tasks/chid/init.rake +19 -0
  21. data/tasks/chid/start.rake +26 -0
  22. data/tasks/chid/update.rake +14 -0
  23. data/tasks/currency/convert.rake +9 -0
  24. data/tasks/currency/current.rake +11 -0
  25. data/tasks/currency/list.rake +10 -0
  26. data/tasks/github.rake +12 -0
  27. data/tasks/help.rake +8 -0
  28. data/tasks/install/node.rake +18 -0
  29. data/tasks/install/postgres.rake +18 -0
  30. data/tasks/install/rvm.rake +21 -0
  31. data/tasks/install/yadr_dotfiles.rake +26 -0
  32. data/tasks/news.rake +11 -0
  33. data/tasks/run/postgres.rake +12 -0
  34. data/tasks/stack_overflow.rake +20 -0
  35. data/tasks/tmux/config_windows.rake +21 -0
  36. data/tasks/tmux/new_session.rake +10 -0
  37. data/tasks/translate/yandex_list.rake +7 -0
  38. data/tasks/translate/yandex_translate.rake +8 -0
  39. data/tasks/update/os.rake +14 -0
  40. data/tasks/workstation/create.rake +23 -0
  41. data/tasks/workstation/destroy.rake +14 -0
  42. data/tasks/workstation/list.rake +10 -0
  43. data/tasks/workstation/open.rake +29 -0
  44. metadata +158 -0
@@ -0,0 +1,127 @@
1
+ class ChidConfig
2
+
3
+ attr_reader :chid_path, :home_path, :chid_config_path
4
+
5
+ def initialize
6
+ @chid_path = Dir.pwd
7
+ @home_path = File.expand_path("~/")
8
+ @chid_config_path = File.join(home_path, '.chid.config')
9
+ end
10
+
11
+ def configure
12
+ print "\nConfigurating the Chid app...\n"
13
+
14
+ create_chid_alias_on_bashrc
15
+ create_an_empty_chid_config_file
16
+
17
+ print "\nConfiguration done!\n"
18
+ end
19
+
20
+ def username
21
+ on_linux { return %x[echo $USER].strip }
22
+ on_osx { return %x[echo $(logname)].strip }
23
+ end
24
+
25
+ def on_linux
26
+ if platform =~ /Linux/
27
+ yield
28
+ end
29
+ end
30
+
31
+ def on_osx
32
+ if platform =~ /Darwin/
33
+ yield
34
+ end
35
+ end
36
+
37
+ def platform
38
+ %x{echo $(uname -s)}.strip
39
+ end
40
+
41
+ def chid_rake_path
42
+ data = YAML.load_file chid_config_path
43
+ data[:chid][:chid_path]
44
+ end
45
+
46
+ def all_workstations
47
+ data = YAML.load_file chid_config_path
48
+ data[:chid][:workstations]
49
+ end
50
+
51
+ def destroy_workstations(workstations = [])
52
+ data = YAML.load_file chid_config_path
53
+
54
+ workstations.each do |w|
55
+ data[:chid][:workstations].delete_if { |key, value| key == w }
56
+ end
57
+
58
+ File.open(chid_config_path, 'w') do |file|
59
+ YAML.dump(data, file)
60
+ end
61
+ end
62
+
63
+ def create_workstation(name, apps = [])
64
+ data = YAML.load_file chid_config_path
65
+
66
+ data[:chid][:workstations][:"#{name}"] = apps
67
+
68
+ File.open(chid_config_path, 'w') do |file|
69
+ YAML.dump(data, file)
70
+ end
71
+ end
72
+
73
+ private
74
+ def create_chid_alias_on_bashrc
75
+ config_path = zshrc_file_exist? ? zshrc_path : bashrc_path
76
+ print "\nCreating the chid alias on your "
77
+ print "#{config_path}\n".blue
78
+
79
+ File.open(config_path, 'a') do |file|
80
+ file.write "\nalias chid='rake -f #{chid_path}/Rakefile'" unless chid_config_file_exist?
81
+ end
82
+ print "\nTo reload your "
83
+ print "profile ".green
84
+ print "should run: "
85
+ print "source #{config_path}\n".blue
86
+ end
87
+
88
+ def zshrc_file_exist?
89
+ File.exist?(zshrc_path)
90
+ end
91
+
92
+ def zshrc_path
93
+ File.join(home_path, '.zshrc')
94
+ end
95
+
96
+ def bashrc_path
97
+ File.join(home_path, '.bashrc')
98
+ end
99
+
100
+ def chid_config_file_exist?
101
+ File.exist?(chid_config_path)
102
+ end
103
+
104
+ def create_an_empty_chid_config_file
105
+ print "\nCreating or Updating the "
106
+ print "~/.chid.config ".blue
107
+ print "file\n"
108
+
109
+ base_config = {
110
+ chid: {
111
+ chid_path: chid_path,
112
+ workstations: {}
113
+ }
114
+ }
115
+ if chid_config_file_exist?
116
+ data = YAML.load_file chid_config_path
117
+ data[:chid][:chid_path] = data[:chid].fetch(:chid_path, chid_path)
118
+ data[:chid][:workstations] = data[:chid].fetch(:workstations, {})
119
+ base_config = data
120
+ end
121
+
122
+ File.open(chid_config_path, 'w') do |file|
123
+ YAML.dump(base_config, file)
124
+ end
125
+ end
126
+
127
+ end
@@ -0,0 +1,69 @@
1
+ #require 'http'
2
+
3
+ class CurrencyApi
4
+ API_TOKEN = '61d0e853b52d82f00b393fdad228eb47'
5
+
6
+ Quotes = Struct.new(:currency, :amount)
7
+
8
+ # Reference Options: https://currency-api.appspot.com
9
+ SOURCES = {
10
+ ARS: 'Argentina Peso',
11
+ AUD: 'Australia Dollar',
12
+ BTC: 'Bitcoin',
13
+ BRL: 'Brazil Real',
14
+ CAD: 'Canada Dollar',
15
+ CLP: 'Chile Peso',
16
+ CNY: 'China Yuan Renminbi',
17
+ CZK: 'Czech Republic Koruna',
18
+ DKK: 'Denmark Krone',
19
+ EUR: 'Euro Member Countries',
20
+ FJD: 'Fiji Dollar',
21
+ HNL: 'Honduras Lempira',
22
+ HKD: 'Hong Kong Dollar',
23
+ HUF: 'Hungary Forint',
24
+ ISK: 'Iceland Krona',
25
+ INR: 'India Rupee',
26
+ IDR: 'Indonesia Rupiah',
27
+ ILS: 'Israel Shekel',
28
+ JPY: 'Japan Yen',
29
+ KRW: 'Korea (South) Won',
30
+ MYR: 'Malaysia Ringgit',
31
+ MXN: 'Mexico Peso',
32
+ NZD: 'New Zealand Dollar',
33
+ NOK: 'Norway Krone',
34
+ PKR: 'Pakistan Rupee',
35
+ PHP: 'Philippines Peso',
36
+ PLN: 'Poland Zloty',
37
+ RUB: 'Russia Ruble',
38
+ SGD: 'Singapore Dollar',
39
+ ZAR: 'South Africa Rand',
40
+ SEK: 'Sweden Krona',
41
+ CHF: 'Switzerland Franc',
42
+ TWD: 'Taiwan New Dollar',
43
+ THB: 'Thailand Baht',
44
+ TRY: 'Turkey Lira',
45
+ GBP: 'United Kingdom Pound',
46
+ USD: 'United States Dollar',
47
+ VND: 'Viet Nam Dong'
48
+ }
49
+
50
+ def self.convert(options = {})
51
+ amount = options.fetch(:amount, 1).to_f
52
+ from = options.fetch(:from, :USD).to_sym
53
+ to = options.fetch(:to, :BRL).to_sym
54
+
55
+ request = HTTP.get("http://www.apilayer.net/api/live?access_key=#{API_TOKEN}")
56
+ json = JSON.parse request
57
+
58
+ to_amount = json['quotes']["USD#{to}"]
59
+ from_amount = json['quotes']["USD#{from}"]
60
+
61
+ if to == :USD
62
+ amount / from_amount
63
+ else
64
+ to_amount * amount
65
+ end
66
+ end
67
+
68
+ end
69
+
@@ -0,0 +1,41 @@
1
+ # Api Reference: https://developer.github.com/v3/search/#search-repositories
2
+ class GitHubApi
3
+
4
+ Owner = Struct.new(:login)
5
+
6
+ Repository = Struct.new(:name, :owner, :html_url, :clone_url, :language, :stargazers_count, :updated_at) do
7
+ def summary
8
+ last_activity = updated_at.nil? ? 'unkown' : updated_at.strftime('%a %d %b %Y')
9
+ print "\n"
10
+ print "--- Repo name -> #{name} ---"
11
+ print "\n\n"
12
+ print "Author #{owner.login}".gray.bold
13
+ print "\n\n"
14
+ print "Link:\t".gray
15
+ print "#{html_url}".cyan.underline
16
+ print "\n\n"
17
+ print "Link to clone:\t".gray
18
+ print "#{clone_url}".cyan.underline
19
+ print "\n\n"
20
+ print "Write in #{language}".green.bold
21
+ print "\n\n"
22
+ print "Stars #{stargazers_count}".cyan.bold
23
+ print "\n\n"
24
+ print "Last activity #{last_activity}"
25
+ print "\n\n"
26
+ end
27
+ end
28
+
29
+ def self.repositories(search_expression)
30
+ uri = URI("https://api.github.com/search/repositories?q=#{search_expression}&sort=stars&order=desc")
31
+ request = ::HTTP.get(uri)
32
+ json_repositories = JSON.parse request
33
+
34
+ json_repositories['items'].collect do |item|
35
+ updated_at = item['updated_at'].nil? ? nil : Date.parse(item['updated_at'])
36
+ owner = Owner.new(item['owner']['login'])
37
+ Repository.new(item['name'], owner, item['html_url'], item['clone_url'], item['language'],
38
+ item['stargazers_count'], updated_at)
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,112 @@
1
+ class Main
2
+
3
+ ActionWithArgs = Struct.new(:action, :args)
4
+
5
+ private
6
+ attr_reader :chid_config
7
+
8
+ public
9
+ def initialize(chid_config)
10
+ @chid_config = chid_config
11
+ end
12
+
13
+ def init(&execute_action_block)
14
+ puts "Hello #{chid_config.username}".blue
15
+ puts "How can I help you?"
16
+
17
+ run(&execute_action_block)
18
+ end
19
+
20
+ private
21
+ def fn_get_input
22
+ -> () {
23
+ print "> "
24
+ STDIN.gets.strip
25
+ }
26
+ end
27
+
28
+ def run(&execute_action_block)
29
+ input = fn_get_input.()
30
+ if quit_command?(input)
31
+ puts 'Bye Bye'
32
+ return
33
+ end
34
+ get_action(input, &execute_action_block)
35
+ run(&execute_action_block)
36
+ end
37
+
38
+ def quit_command?(input)
39
+ input =~ /^:q/ || input =~ /^bye/ || input =~ /^quit/ || input =~ /^exit/
40
+ end
41
+
42
+ def get_action(input, &execute_action_block)
43
+ actions_with_args = get_actions_with_args(input)
44
+ return not_found_msgs if actions_with_args.empty?
45
+
46
+ get_action_with_args(actions_with_args) do | action_with_args|
47
+ execute_action_block.(action_with_args.action, action_with_args.args)
48
+ end
49
+
50
+ end
51
+
52
+ def get_actions_with_args(input)
53
+ actions_with_args = Chid::REGEX_ACTIONS.collect do |action, regexs|
54
+ action_with_args = nil
55
+
56
+ regex_match(input, regexs) do |captured_args|
57
+ action_with_args = ActionWithArgs.new(action, captured_args)
58
+ end
59
+ action_with_args
60
+ end.compact!
61
+ end
62
+
63
+ def regex_match(input, regexs, &block)
64
+ regexs.each do |regex|
65
+ matched = regex.match input
66
+ if matched
67
+ captured_args = matched.captures
68
+ block.(captured_args)
69
+ return
70
+ end
71
+ end
72
+ end
73
+
74
+ def get_action_with_args(actions_with_args)
75
+ return if actions_with_args.nil?
76
+
77
+ if actions_with_args.count > 1
78
+ action_with_args = choose_multiple_action(actions_with_args)
79
+ else
80
+ action_with_args = actions_with_args.first
81
+ end
82
+
83
+ yield action_with_args if action_with_args
84
+ end
85
+
86
+ def choose_multiple_action(actions_with_args)
87
+ puts "You are trying to execute #{actions_with_args.count} actions at once."
88
+ puts "Please choose:"
89
+ puts "0 - none"
90
+ actions_with_args.each_with_index { |a, i| puts "#{i + 1} - #{a.action}" }
91
+ choose = fn_get_input.().to_i
92
+ if choose == 0
93
+ puts "Ok, canceled"
94
+ return nil
95
+ end
96
+ choose = choose - 1
97
+ actions_with_args[choose]
98
+ end
99
+
100
+ def not_found_msgs()
101
+ msgs = [
102
+ "Sorry, I did not found any action.",
103
+ "You should try another action. That does not exist. Sorry.",
104
+ "Maybe you typed wrongly. Please try again."
105
+ ]
106
+
107
+ msg_number = rand(3) - 1
108
+
109
+ puts msgs[msg_number]
110
+ end
111
+
112
+ end
@@ -0,0 +1,60 @@
1
+ # Api Reference: https://newsapi.org/#apiArticles
2
+ class NewsApi
3
+ API_TOKEN = 'ce81e7aeb6c4467880b2ee5e4e2d8492'
4
+
5
+ Source = Struct.new(:id, :name, :description, :category)
6
+ Article = Struct.new(:source, :author, :title, :description, :url, :publishedAt) do
7
+ def summary
8
+ published_at = publishedAt.nil? ? 'unkown' : publishedAt.strftime("%B %d, %Y")
9
+ print "\n"
10
+ print "--- #{title} ---".blue
11
+ print "\n"
12
+ print " Posted #{published_at} by ".brown
13
+ print "#{author}".green
14
+ print "\n\n"
15
+ print " #{description}"
16
+ print "\n\n"
17
+ print " Link: "
18
+ print "#{url}".cyan.underline
19
+ print "\n"
20
+ end
21
+ end
22
+
23
+ SOURCES = [
24
+ :"reddit-r-all",
25
+ :'google-news',
26
+ :'bbc-news',
27
+ :'cnn',
28
+ :'espn',
29
+ :'mashable',
30
+ :techcrunch
31
+ ]
32
+
33
+ def self.articles
34
+ sources = self.sources()
35
+ sources.collect do |s|
36
+ request = ::HTTP.get("https://newsapi.org/v1/articles?source=#{s.id}&apiKey=#{API_TOKEN}")
37
+ json_news = JSON.parse request
38
+
39
+ json_news[ 'articles' ].collect do |n|
40
+ published_at = n[ 'publishedAt' ].nil? ? nil : Date.parse(n[ 'publishedAt' ])
41
+ Article.new(json_news[ 'source' ], n[ 'author' ], n[ 'title' ],
42
+ n[ 'description' ], n[ 'url' ], published_at)
43
+ end
44
+
45
+ end.flatten
46
+ end
47
+
48
+ def self.sources()
49
+ #request = HTTP.get("https://newsapi.org/v1/sources?language=en")
50
+ #json_sources = JSON.parse request
51
+
52
+ #json_sources[ 'sources' ].collect do |s|
53
+ # Source.new(s[ 'id' ], s[ 'name' ], s[ 'description' ], s[ 'category' ])
54
+ #end
55
+ SOURCES.collect do |s|
56
+ Source.new(s)
57
+ end
58
+ end
59
+
60
+ end
@@ -0,0 +1,57 @@
1
+ class Paginator
2
+
3
+ private
4
+ attr_reader :current_page, :per_page, :list
5
+
6
+ public
7
+ def initialize(list)
8
+ @current_page = 1
9
+ @per_page = 3
10
+ @list = list.freeze
11
+ end
12
+
13
+ def paginate(&block)
14
+ begin_index = (current_page - 1) * per_page
15
+ end_index = (current_page * per_page) - 1
16
+
17
+ return if list.size < begin_index
18
+
19
+ paginated_list = list.slice(begin_index..end_index)
20
+
21
+ paginated_list.each do |object|
22
+ block.(object) if block_given?
23
+ end
24
+
25
+ ask_action(&block)
26
+ end
27
+
28
+ private
29
+ def ask_action(&block)
30
+ return if total_pages <= 1
31
+
32
+ puts "\n#{current_page} of #{total_pages}"
33
+
34
+ puts "\nPrevious(p) Next(n) Quit(q):"
35
+ print "> "
36
+ option = STDIN.gets.strip
37
+
38
+ return if (/^q/.match(option))
39
+ next_page if (/^n/.match(option))
40
+ previous_page if (/^p/.match(option))
41
+
42
+ paginate(&block)
43
+ end
44
+
45
+ def next_page
46
+ @current_page += 1
47
+ end
48
+
49
+ def previous_page
50
+ @current_page -= 1
51
+ end
52
+
53
+ def total_pages
54
+ list.size / per_page
55
+ end
56
+
57
+ end