Class: Wikian

Inherits:
Object
  • Object
show all
Defined in:
lib/wikian.rb,
lib/wikian/get.rb,
lib/wikian/post.rb,
lib/wikian/search.rb,
lib/wikian/version.rb,
lib/wikian/subcommand.rb,
lib/wikian/contributions.rb

Defined Under Namespace

Classes: ArgumentRequiredError, BadUrlError, Contributions, ExtractWikiError, Get, Post, Search, Subcommand, UnknownSubcommandError, WikiFileError, WikiFileNameError, WikiMergeError, WikianError, WikianGetError, WikianPostError, WikianSubcommandError

Constant Summary collapse

CONFIG_FILE =
'wiki.yml'
RESPONSE_FORMAT =
'json'
VERSION =
"0.2.1"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Wikian

Returns a new instance of Wikian.



28
29
30
# File 'lib/wikian.rb', line 28

def initialize(*args)
  @args = args
end

Instance Attribute Details

#argsObject

Returns the value of attribute args.



23
24
25
# File 'lib/wikian.rb', line 23

def args
  @args
end

#subcommandObject

Returns the value of attribute subcommand.



23
24
25
# File 'lib/wikian.rb', line 23

def subcommand
  @subcommand
end

Class Method Details

.meta_dirObject



63
64
65
# File 'lib/wikian.rb', line 63

def self.meta_dir
  '.wikian'
end

.meta_fileObject

file to store metadata of fetched articles



68
69
70
# File 'lib/wikian.rb', line 68

def self.meta_file
  File.join(meta_dir, 'meta.yml')
end

Instance Method Details

#helpObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/wikian.rb', line 72

def help
  puts <<~eos
    Usage:
      wiki [options] <subcommand>  [url|file]

    Options:
      -a, --append               append the input file
      -c, --captcha ID:MESSAGE   captcha info
      -d, --debug                print debugging messages
      -h, --help                 print this help message
      -m, --message MESSAGE      add a commit message (HIGHLY recommended)
      -p, --prepend              prepend the input file
      -r, --remove-cookie        remove API cookie
      -s, --section NUMBER       section to edit
      -t, --template             create template configuration file
      -v, --version              print version number

    Subcommands:
      c, contributions [N]    get user last N contributions (defaults to #{Contributions::DEFAULT_MAX_CONTRIBUTIONS})
      g, get                  get wikitext from a Wikipedia article
      p, post                 post wikitext to a Wikipedia article
      s, search               search wikitext in Wikipedia

    Examples:
      # create wiki.yml template
      wi g -t

      # download article
      wi get -t
      wi get https://en.wikipedia.org/wiki/Wikipedia:Sandbox

      # upload file
      wi post Wikipedia:Sandbox.en.wikipedia.org.wiki

      # upload file to Spanish Wikipedia
      wi post Wikipedia:Sandbox.es.wikipedia.org.wiki

      # upload file to English Wiktionary
      wi post Wikipedia:Sandbox.en.wiktionary.org.wiki

      # append wikitext to section 2 of the article
      wi post -a -s 2 Wikipedia:Sandbox.en.wikipedia.org.wiki

      # heavy use of the API may require cache validation
      wi post -c 1234:someMessage spider-Man.wiki

    Comments:
      Posted files must follow the convention:
        <article_name>.<site>.wiki
      where <site> is a wikimedia site.
      More info at: https://meta.wikimedia.org/wiki/Our_projects
  eos
  exit
end

#runObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/wikian.rb', line 32

def run
  if args.have?(%w(-h --help))
    help
  elsif args.have?(%w(-v --version))
    version
  end

  @subcommand = args.shift

  raise(UnknownSubcommandError, "Unkown Subcommand") unless %w(c g p s contributions get post search).include?(subcommand)

  if subcommand[0] == 'g'
    api = Wikian::Get.new(args)
    api.doit
    api.extract_wikitext
    api.
  elsif subcommand[0] == 's'
    api = Wikian::Search.new(args)
    api.doit
  elsif subcommand[0] == 'c'
    api = Wikian::Contributions.new(args)
    api.doit
  else
    api = Wikian::Post.new(args)
    api.post
  end

rescue UnknownSubcommandError => e
  puts "#{e.class} #{e.message} in #{__FILE__}"
end

#versionObject



127
128
129
130
# File 'lib/wikian.rb', line 127

def version
  puts "wikian #{VERSION}"
  exit
end