Sha256: 77d0668ccfb3fa2212d762497ce4fed6c13850e000256aa5fbe41fb4d1fc2e17

Contents?: true

Size: 1.88 KB

Versions: 31

Compression:

Stored size: 1.88 KB

Contents

require 'optparse'
require 'yaml'

module MediaWiki
  
  class Config
    
    attr_reader :article, :desc, :file, :pw, :summary, :target, :url, :user
    
    def initialize(args, type = "read")
      @summary = "Automated edit via MediaWiki::Gateway"
      @opts = OptionParser.new do |opts|
        opts.banner = "Usage: [options]"
        
        opts.on("-h", "--host HOST", "Use preconfigured HOST in config/hosts.yml") do |host_id|
          yaml = YAML.load_file('config/hosts.yml')
          if yaml.include? host_id
            host = yaml[host_id]
            @url = host['url']
            @pw = host['pw']
            @user = host['user']
          else
            raise "Host #{host_id} not found in config/hosts.yml"
          end
        end

        if type == "upload"
          opts.on("-d", "--description DESCRIPTION", "Description of file to upload") do |desc|
            @desc = desc
          end
          opts.on("-t", "--target-file TARGET-FILE", "Target file name to upload to") do |target|
            @target = target
          end
        else
          opts.on("-a", "--article ARTICLE", "Name of article in Wiki") do |article|
            @article = article
          end
        end
        
        opts.on("-n", "--username USERNAME", "Username for login") do |user|
          @user = user
        end

        opts.on("-p", "--password PASSWORD", "Password for login") do |pw|
          @pw = pw
        end

        if type != "read"
          opts.on("-s", "--summary SUMMARY", "Edit summary for this change") do |summary|
            @summary = summary
          end
        end

        opts.on("-u", "--url URL", "MediaWiki API URL") do |url|
          @url = url
        end
      end
      @opts.parse!
      abort("URL (-u) or valid host (-h) is mandatory.") unless @url
    end

    def abort(error)
      puts "Error: #{error}\n\n#{@opts.to_s}"
      exit
    end

  end
end

Version data entries

31 entries across 31 versions & 1 rubygems

Version Path
mediawiki-gateway-0.6.2 lib/media_wiki/config.rb
mediawiki-gateway-0.6.1 lib/media_wiki/config.rb
mediawiki-gateway-0.6.0 lib/media_wiki/config.rb
mediawiki-gateway-0.5.2 lib/media_wiki/config.rb
mediawiki-gateway-0.5.1 lib/media_wiki/config.rb
mediawiki-gateway-0.5.0 lib/media_wiki/config.rb
mediawiki-gateway-0.4.5 lib/media_wiki/config.rb
mediawiki-gateway-0.4.4 lib/media_wiki/config.rb
mediawiki-gateway-0.4.3 lib/media_wiki/config.rb
mediawiki-gateway-0.4.2 lib/media_wiki/config.rb
mediawiki-gateway-0.4.1 lib/media_wiki/config.rb
mediawiki-gateway-0.3.8 lib/media_wiki/config.rb
mediawiki-gateway-0.3.7 lib/media_wiki/config.rb
mediawiki-gateway-0.3.6 lib/media_wiki/config.rb
mediawiki-gateway-0.3.5 lib/media_wiki/config.rb
mediawiki-gateway-0.3.4 lib/media_wiki/config.rb
mediawiki-gateway-0.3.3 lib/media_wiki/config.rb
mediawiki-gateway-0.3.2 lib/media_wiki/config.rb
mediawiki-gateway-0.3.1 lib/media_wiki/config.rb
mediawiki-gateway-0.3.0 lib/media_wiki/config.rb