Class: Wikian::Subcommand

Inherits:
Object
  • Object
show all
Defined in:
lib/wikian/subcommand.rb

Overview

class to be inherited by other Wikian classes

Direct Known Subclasses

Contributions, Get, Search

Instance Attribute Summary collapse

Instance Method Summary collapse

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_urlObject

Returns the value of attribute api_url.



8
9
10
# File 'lib/wikian/subcommand.rb', line 8

def api_url
  @api_url
end

#argsObject

Returns the value of attribute args.



8
9
10
# File 'lib/wikian/subcommand.rb', line 8

def args
  @args
end

#configObject

Returns the value of attribute config.



8
9
10
# File 'lib/wikian/subcommand.rb', line 8

def config
  @config
end

#debugObject

Returns the value of attribute debug.



8
9
10
# File 'lib/wikian/subcommand.rb', line 8

def debug
  @debug
end

#output_fileObject

Returns the value of attribute output_file.



8
9
10
# File 'lib/wikian/subcommand.rb', line 8

def output_file
  @output_file
end

#queryObject

Returns the value of attribute query.



8
9
10
# File 'lib/wikian/subcommand.rb', line 8

def query
  @query
end

#resObject

Returns the value of attribute res.



8
9
10
# File 'lib/wikian/subcommand.rb', line 8

def res
  @res
end

#res_bodyObject

Returns the value of attribute res_body.



8
9
10
# File 'lib/wikian/subcommand.rb', line 8

def res_body
  @res_body
end

#titleObject

Returns the value of attribute title.



8
9
10
# File 'lib/wikian/subcommand.rb', line 8

def title
  @title
end

Instance Method Details

#doitObject



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.message
  exit
end

#make_templateObject



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_argsObject

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_fileObject

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.meta['content-type'].split('/').last.sub(/;.*/,'')
end

#write_responseObject

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