dogecoin.rb 1.81 KB
Newer Older
Cool Fire's avatar
Cool Fire committed
1 2
#!/usr/bin/env ruby

3
# Plugin to get dogecoin value
Cool Fire's avatar
Cool Fire committed
4 5 6
class Dogecoin

	require 'json'
7 8
	require 'bigdecimal'

Cool Fire's avatar
Cool Fire committed
9 10 11 12 13 14 15 16 17 18 19
	# This method is called when the plugin is first loaded
	def initialize( status, config, output, irc, timer )
		@status   = status
		@config   = config
		@output   = output
		@irc      = irc
		@timer    = timer
		
		@api_host = 'pubapi.cryptsy.com'
		@api_path = '/api.php?method=singlemarketdata&marketid=132'
		
20
		@last     = BigDecimal.new( "0.0" )
Cool Fire's avatar
Cool Fire committed
21 22 23
	end

	# Alias for last
Cool Fire's avatar
Cool Fire committed
24
	def main( _nick, _user, _host, from, _msg, _arguments, con )
Cool Fire's avatar
Cool Fire committed
25 26
		uri = URI.parse( "http://#{@api_host}#{@api_path}" )
		
27
		http = Net::HTTP.new( uri.host, uri.port )
Cool Fire's avatar
Cool Fire committed
28
		
29 30
		request = Net::HTTP::Get.new( uri.request_uri )
		response = http.request( request )
Cool Fire's avatar
Cool Fire committed
31 32 33 34 35
		
		result = JSON.parse( response.body )
		

		# Calculate delta from last !btc
36
		ldiff = BigDecimal.new( result[ 'return' ][ 'markets' ][ 'DOGE' ][ 'lasttradeprice' ] ) - @last
Cool Fire's avatar
Cool Fire committed
37
		
Cool Fire's avatar
Cool Fire committed
38
		if( ldiff > 0 )
Cool Fire's avatar
Cool Fire committed
39
			ldiff = "+#{ldiff}"
Cool Fire's avatar
Cool Fire committed
40
		else
Cool Fire's avatar
Cool Fire committed
41
			ldiff = "#{ldiff}"
Cool Fire's avatar
Cool Fire committed
42
		end
43
		@last = BigDecimal.new( result[ 'return' ][ 'markets' ][ 'DOGE' ][ 'lasttradeprice' ] )
Cool Fire's avatar
Cool Fire committed
44
		
45
		rounded = "#{( BigDecimal.new( result[ 'return' ][ 'markets' ][ 'DOGE' ][ 'lasttradeprice' ] ) * 100 ).round / 100.0}"
Cool Fire's avatar
Cool Fire committed
46
		
Cool Fire's avatar
Cool Fire committed
47
		line = "Cryptsy DOGE/BTC rate: #{rounded} (#{result[ 'return' ][ 'markets' ][ 'DOGE' ][ 'lasttradeprice' ]}) (#{ldiff} since last !doge)"
Cool Fire's avatar
Cool Fire committed
48 49 50 51 52 53 54 55 56
		
		if( con )
			@output.c( line + "\n" )
		else
			@irc.message( from, line )
		end
	end
	
	# Function to send help about this plugin (Can also be called by the help plugin.)
Cool Fire's avatar
Cool Fire committed
57
	def help( nick, _user, _host, _from, _msg, _arguments, con )
Cool Fire's avatar
Cool Fire committed
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
		help = [
			"Gets current litecoin values from Btc-e",
			"  dogecoin          - Get DOGE/BTC exchange rate"
		]
		
		# Print out help
		help.each do |line|
			if( con )
				@output.c( line + "\n" )
			else
				@irc.notice( nick, line )
			end
		end
	end
end