Class: Wikian::Subcommand
- Inherits:
-
Object
- Object
- Wikian::Subcommand
- Defined in:
- lib/wikian/subcommand.rb
Overview
class to be inherited by other Wikian classes
Direct Known Subclasses
Instance Attribute Summary collapse
-
#api_url ⇒ Object
Returns the value of attribute api_url.
-
#args ⇒ Object
Returns the value of attribute args.
-
#config ⇒ Object
Returns the value of attribute config.
-
#debug ⇒ Object
Returns the value of attribute debug.
-
#output_file ⇒ Object
Returns the value of attribute output_file.
-
#query ⇒ Object
Returns the value of attribute query.
-
#res ⇒ Object
Returns the value of attribute res.
-
#res_body ⇒ Object
Returns the value of attribute res_body.
-
#title ⇒ Object
Returns the value of attribute title.
Instance Method Summary collapse
- #doit ⇒ Object
-
#initialize(args) ⇒ Subcommand
constructor
A new instance of Subcommand.
- #make_template ⇒ Object
-
#non_opt_args ⇒ Object
return args that dont start with ‘-’.
-
#response_file ⇒ Object
HTTP response file name.
-
#write_response ⇒ Object
write response in to ‘response_file`.
Constructor Details
#initialize(args) ⇒ Subcommand
Returns a new instance of Subcommand.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/wikian/subcommand.rb', line 11 def initialize(args) @args = args if args.have?(%w(-t --template)) puts "Creating template #{Wikian::CONFIG_FILE}" make_template exit end @debug = (args & %w(-d --debug)).length > 0 ? true : false @config = if File.exist?(Wikian::CONFIG_FILE) YAML.load(File.open(Wikian::CONFIG_FILE)) else YAML.load(template) end # some params like 'titles' can contain multiple entries joined by '|'. More info in Wikipedia API docs @params = Hash[config['api'].keys.zip(config['api'].values.map{|arr| arr.join("|")})] rescue InitializeError => e puts "#{e.class} try passing the '-t' option to generate #{Wikian::CONFIG_FILE} in #{__FILE__}" exit end |
Instance Attribute Details
#api_url ⇒ Object
Returns the value of attribute api_url.
8 9 10 |
# File 'lib/wikian/subcommand.rb', line 8 def api_url @api_url end |
#args ⇒ Object
Returns the value of attribute args.
8 9 10 |
# File 'lib/wikian/subcommand.rb', line 8 def args @args end |
#config ⇒ Object
Returns the value of attribute config.
8 9 10 |
# File 'lib/wikian/subcommand.rb', line 8 def config @config end |
#debug ⇒ Object
Returns the value of attribute debug.
8 9 10 |
# File 'lib/wikian/subcommand.rb', line 8 def debug @debug end |
#output_file ⇒ Object
Returns the value of attribute output_file.
8 9 10 |
# File 'lib/wikian/subcommand.rb', line 8 def output_file @output_file end |
#query ⇒ Object
Returns the value of attribute query.
8 9 10 |
# File 'lib/wikian/subcommand.rb', line 8 def query @query end |
#res ⇒ Object
Returns the value of attribute res.
8 9 10 |
# File 'lib/wikian/subcommand.rb', line 8 def res @res end |
#res_body ⇒ Object
Returns the value of attribute res_body.
8 9 10 |
# File 'lib/wikian/subcommand.rb', line 8 def res_body @res_body end |
#title ⇒ Object
Returns the value of attribute title.
8 9 10 |
# File 'lib/wikian/subcommand.rb', line 8 def title @title end |
Instance Method Details
#doit ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/wikian/subcommand.rb', line 65 def doit puts api_url if debug @res=URI.open(api_url, config['meta']['headers']) @res_body = res.read write_response rescue => e puts "#{e.class} in #{__FILE__}", e. exit end |
#make_template ⇒ Object
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/wikian/subcommand.rb', line 36 def make_template if File.exist?(CONFIG_FILE) puts "Overwrite existing '#{CONFIG_FILE}'? [yn]" answer = STDIN.gets.chomp (puts 'Bye'; exit) if answer != 'y' end File.write(CONFIG_FILE, template) exit end |
#non_opt_args ⇒ Object
return args that dont start with ‘-’
48 49 50 |
# File 'lib/wikian/subcommand.rb', line 48 def non_opt_args args.reject {|p| p[0] == '-'} end |
#response_file ⇒ Object
HTTP response file name. Its extension depends on the ‘content-type’ header
53 54 55 |
# File 'lib/wikian/subcommand.rb', line 53 def response_file output_file + '.' + res.['content-type'].split('/').last.sub(/;.*/,'') end |
#write_response ⇒ Object
write response in to ‘response_file`
58 59 60 61 62 63 |
# File 'lib/wikian/subcommand.rb', line 58 def write_response STDERR.puts "Writing to #{response_file}" File.open(response_file, 'w') do |f| f.puts prettify(res_body) end end |