class TheFox::Pastebin::Pastebin

Constants

API_DEV_KEY

Public Class Methods

new(options = {}) click to toggle source
# File lib/pastebin/pastebin.rb, line 12
def initialize(options = {})
        if options.nil?
                options = {}
        else
                @options = options
        end
        
        if !@options.has_key?('api_dev_key')
                @options['api_dev_key'] = API_DEV_KEY
        end
        if !@options.has_key?('api_paste_expire_date')
                @options['api_paste_expire_date'] = 'N'
        end
        if !@options.has_key?('api_paste_private')
                @options['api_paste_private'] = '0'
        end
        
        @name_prefix = ''
        if @options.has_key?('name_prefix')
                @name_prefix = @options['name_prefix']
        end
        @name_postfix = ''
        if @options.has_key?('name_postfix')
                @name_postfix = @options['name_postfix']
        end
        
        if @options.has_key?('api_paste_name') && @options['api_paste_name'] != '' && (@name_prefix != '' || @name_postfix != '')
                @options['api_paste_name'] = "#{@name_prefix}#{@options['api_paste_name']}#{@name_postfix}"
        end
end

Public Instance Methods

login(username, password) click to toggle source
# File lib/pastebin/pastebin.rb, line 43
def login(username, password)
        @options['api_user_name'] = username
        @options['api_user_password'] = password
        
        uri = URI.parse('https://pastebin.com/api/api_login.php')
        res = Net::HTTP.post_form(uri, @options)
        
        if res.code.to_i == 200
                if /^[a-f0-9]{32}$/.match(res.body)
                        return res.body
                end
        end
        
        raise res.body
end
paste() click to toggle source
# File lib/pastebin/pastebin.rb, line 59
def paste
        @options['api_option'] = 'paste'
        
        if @options.has_key?('api_paste_code')
                if @options['api_paste_code'] == '-'
                        @options['api_paste_code'] = STDIN.read
                else
                        File.open(@options['api_paste_code']) do |file|
                                @options['api_paste_code'] = file.read
                        end
                end
        else
                @options['api_paste_code'] = STDIN.read
        end
        
        if @options.has_key?('api_paste_code') && @options['api_paste_code']
                uri = URI.parse('https://pastebin.com/api/api_post.php')
                return Net::HTTP.post_form(uri, @options).body
        else
                raise 'no code to paste'
        end
end
print_defaults() click to toggle source
print_skel() click to toggle source
raw(id) click to toggle source
# File lib/pastebin/pastebin.rb, line 82
def raw(id)
        uri = URI.parse("https://pastebin.com/raw.php?i=#{id}")
    Net::HTTP.get_response(uri).body
end