Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/define lookfor command #5

Merged
merged 6 commits into from
Feb 12, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions bin/gaea
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env ruby

require 'gaea'
Gaea::CLI::Lookfor.start(ARGV)
1 change: 1 addition & 0 deletions gaea.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ Gem::Specification.new do |spec|
spec.add_dependency 'thor'
spec.add_dependency 'httparty'
spec.add_dependency 'terminal-table'
spec.add_dependency 'rainbow'
end
2 changes: 2 additions & 0 deletions lib/gaea.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
require 'gaea/version'
require 'gaea/invalid_source'
require 'gaea/option_missing'
require 'gaea/cli'

module Gaea
Expand Down
1 change: 1 addition & 0 deletions lib/gaea/cli.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'thor'
require 'httparty'
require 'rainbow'
require 'terminal-table'
require 'gaea/cli/lookfor'
require 'gaea/lib/stackoverflow'
Expand Down
37 changes: 36 additions & 1 deletion lib/gaea/cli/lookfor.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,41 @@
module Gaea
module CLI
class Lookfor < Thor
class Looksfor < Thor
# Search from multiple sources
#
# Options
#
# keyword - The keyword relate contents you wanna search
# source(require) - One of 3 values: 'stackoverflow', 'gems', 'confreaks'
# year(optional) - The year - will use with source confreaks
#
# Returns the results depend on source and keyword
desc 'lookfor', 'Search from multiple sources'
option :keyword, aliases: '-k', banner: 'The keyword relate to contents you wanna search'
option :source, aliases: '-s', banner: 'The source - 3 default values: stackoverflow, gems and confreaks'
option :year, aliases: '-y', banner: 'The year - only avalaible with source confreaks'
def looksfor
keyword = options[:keyword]

results = case options[:source]
when 'stackoverflow'
raise OptionMissing, 'Oops, man, make sure you have the keyword option in your command' unless keyword
stack = StackOverFlow.new(keyword)
stack.questions
when 'gems'
raise OptionMissing, 'Oops, man, make sure you have the keyword option in your command' unless keyword
rgem = RubyGems.new(keyword)
rgem.gems
when 'confreaks'
year = options[:year]
raise OptionMissing, 'Oops, man, make sure you have the year option in your command' unless year
conf = Confreaks.new(year)
keyword ? conf.events_of_year(keyword) : conf.events_of_year
else
raise InvalidSource, 'Hey man, you wrong the source. Please check again!'
end
puts Rainbow(results).bisque
end
end
end
end
1 change: 1 addition & 0 deletions lib/gaea/invalid_source.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
class InvalidSource < StandardError; end
14 changes: 12 additions & 2 deletions lib/gaea/lib/confreaks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,18 @@ def initialize(year)
# Examples
#
# conf = Confreaks.new('2015')
# conf.events_of_year
# conf.events_of_year or conf.events_of_year
# # =>
# +-----------------------------+------------------------------------------------------+------------+------------+
# | Event | Link | Start date | End date |
# +-----------------------------+------------------------------------------------------+------------+------------+
# | Ruby Kaigi 2015 | http://confreaks.tv/events/rubykaigi2015 | 2015-12-11 | 2015-12-13 |
# | Alter Conf Los Angeles 2015 | http://confreaks.tv/events/alterconf2015-los-angeles | 2015-11-21 | 2015-11-21 |
# | DockerCon EU 2015 | http://confreaks.tv/events/Dockerconeu2015 | 2015-11-15 | 2015-11-17 |
# | Ruby Conference 2015 | http://confreaks.tv/events/rubyconf2015 | 2015-11-15 | 2015-11-17 |
# +-----------------------------+------------------------------------------------------+------------+------------+
#
# Returns Array of events
# Returns the table
def events_of_year(field = nil)
results = field ? events.select!{ |event| event['short_code'].downcase.include? field } : events

Expand All @@ -53,6 +62,7 @@ def parse_events(events)
events.each do |event|
event_rows << [event['display_name'], "http://confreaks.tv/events/#{event['short_code']}", event_date(event['start_at']), event_date(event['end_at'])] if event_of_year?(event['short_code'])
end
return nil if event_rows.empty?
Terminal::Table.new headings: ['Event', 'Link', 'Start date', 'End date'], rows: event_rows
else
nil
Expand Down
16 changes: 8 additions & 8 deletions lib/gaea/lib/rubygems.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,15 @@ def initialize(query)
#
# rgs = RubyGems.new('weather')
# rgs.gems
#
# Returns an Array of JSON of gems that match.
# # =>
# +------+-----------------------------------------------+----------------------------------+-------------+-----------+
# | Name | Info | URL | Authors | Downloads |
# +------+-----------------------------------------------+----------------------------------+-------------+-----------+
# | weer | Display the weather information of your city. | https://github.com/vinhnglx/weer | Vinh Nguyen | 134 |
# +------+-----------------------------------------------+----------------------------------+-------------+-----------+
# Returns the table
def gems
# TODO: need to get more questions - Apply paginates
parse_gems
end

Expand All @@ -53,12 +59,6 @@ def connect(option)
# Examples
#
# parse_gems
# # =>
# +------+-----------------------------------------------+----------------------------------+-------------+-----------+
# | Name | Info | URL | Authors | Downloads |
# +------+-----------------------------------------------+----------------------------------+-------------+-----------+
# | weer | Display the weather information of your city. | https://github.com/vinhnglx/weer | Vinh Nguyen | 134 |
# +------+-----------------------------------------------+----------------------------------+-------------+-----------+
#
# Returns terminal table object
def parse_gems
Expand Down
45 changes: 16 additions & 29 deletions lib/gaea/lib/stackoverflow.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,24 @@ def initialize(keywords)
# stackoverflow = StackOverFlow.new('activerecord nomethod error', true)
# stackoverflow.questions
# # =>
# [
# {
# 'tags': ['ruby-on-rails', 'ruby'],
# 'owner': { 'display_name': 'johndoe', 'link': 'http://stackoverflow.com/users/2398/johndoe'},
# 'answer_count': 14,
# 'accepted_answer_link': 'http://stackoverflow.com/a/21420719/',
# 'title': 'PG::ConnectionBad - could not connect to server: Connection refused',
# 'link': 'http://stackoverflow.com/questions/19828385/pgconnectionbad-could-not-connect-to-server-connection-refused'
# },
# {
# ....
# },
# ]
# +---------------+-----------------------------------------------------------------------------------+-------------------------------------+-------------------------------------+
# | Owner | Title | Question | Accepted Answer |
# +---------------+-----------------------------------------------------------------------------------+-------------------------------------+-------------------------------------+
# | Daniel Cukier | Discover errors in Invalid Record factory girl | http://stackoverflow.com/q/23374576 | http://stackoverflow.com/a/23374747 |
# | Chris Mendla | Rails delete record fails | http://stackoverflow.com/q/35232445 | http://stackoverflow.com/a/35235143 |
# | Jorrin | Manually assigning parent ID with has_many/belongs_to association in custom class | http://stackoverflow.com/q/35193155 | http://stackoverflow.com/a/35201406 |
# | simonmorley | Rails i18n Attributes Not Working via JSON API | http://stackoverflow.com/q/35113584 | http://stackoverflow.com/a/35117092 |
# | NeoP5 | Sonarqube 5.3: Error installing on Oracle 12 - columns missing | http://stackoverflow.com/q/34807593 | http://stackoverflow.com/a/35008747 |
# | kannet | Invalid single-table inheritance type: dog is not a subclass of Pet | http://stackoverflow.com/q/34988853 | http://stackoverflow.com/a/34989090 |
# | Brittany | NoMethodError in Users#show error? | http://stackoverflow.com/q/34980742 | http://stackoverflow.com/a/34980943 |
# | Mac | Python POST binary data | http://stackoverflow.com/q/14365027 | http://stackoverflow.com/a/14448953 |
# | CuriousMind | railstutorial.org, Chapter 6. unknown attribute: password | http://stackoverflow.com/q/12142374 | http://stackoverflow.com/a/12142417 |
# | Brittany | Empty database, user already exists message? | http://stackoverflow.com/q/34862365 | http://stackoverflow.com/a/34862683 |
# +---------------+-----------------------------------------------------------------------------------+-------------------------------------+-------------------------------------+
#
# Returns the JSON list of questions
# Returns the table
def questions
# TODO: need to get more questions - Apply paginates
parse_questions
end

Expand All @@ -70,21 +72,6 @@ def connect(options)
# Examples
#
# parse_questions
# # =>
# +---------------+-----------------------------------------------------------------------------------+-------------------------------------+-------------------------------------+
# | Owner | Title | Question | Accepted Answer |
# +---------------+-----------------------------------------------------------------------------------+-------------------------------------+-------------------------------------+
# | Daniel Cukier | Discover errors in Invalid Record factory girl | http://stackoverflow.com/q/23374576 | http://stackoverflow.com/a/23374747 |
# | Chris Mendla | Rails delete record fails | http://stackoverflow.com/q/35232445 | http://stackoverflow.com/a/35235143 |
# | Jorrin | Manually assigning parent ID with has_many/belongs_to association in custom class | http://stackoverflow.com/q/35193155 | http://stackoverflow.com/a/35201406 |
# | simonmorley | Rails i18n Attributes Not Working via JSON API | http://stackoverflow.com/q/35113584 | http://stackoverflow.com/a/35117092 |
# | NeoP5 | Sonarqube 5.3: Error installing on Oracle 12 - columns missing | http://stackoverflow.com/q/34807593 | http://stackoverflow.com/a/35008747 |
# | kannet | Invalid single-table inheritance type: dog is not a subclass of Pet | http://stackoverflow.com/q/34988853 | http://stackoverflow.com/a/34989090 |
# | Brittany | NoMethodError in Users#show error? | http://stackoverflow.com/q/34980742 | http://stackoverflow.com/a/34980943 |
# | Mac | Python POST binary data | http://stackoverflow.com/q/14365027 | http://stackoverflow.com/a/14448953 |
# | CuriousMind | railstutorial.org, Chapter 6. unknown attribute: password | http://stackoverflow.com/q/12142374 | http://stackoverflow.com/a/12142417 |
# | Brittany | Empty database, user already exists message? | http://stackoverflow.com/q/34862365 | http://stackoverflow.com/a/34862683 |
# +---------------+-----------------------------------------------------------------------------------+-------------------------------------+-------------------------------------+
#
# Returns terminal table object
def parse_questions
Expand Down
1 change: 1 addition & 0 deletions lib/gaea/option_missing.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
class OptionMissing < StandardError; end
8 changes: 8 additions & 0 deletions spec/lib/confreaks_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
RSpec.describe Confreaks do
include_context 'confreaks list'
let(:conf) { Confreaks.new("2015") }
let(:invalid_conf) { Confreaks.new("820982039") }

context '.initialize' do
it 'returns an instance of Confreaks object' do
Expand Down Expand Up @@ -34,5 +35,12 @@
expect(conf.events_of_year).to be_an_instance_of Terminal::Table
end
end

context 'with invalid year' do
it 'returns nil if the year argument invalid' do
expect(invalid_conf.events_of_year).to be_nil
expect(invalid_conf.events_of_year('python')).to be_nil
end
end
end
end