ltc.rb 2.03 KB
Newer Older
1 2 3 4 5 6 7 8 9
#!/usr/bin/env ruby

# Plugin to get litecoin value
class Ltc

	require 'json'
	
	# This method is called when the plugin is first loaded
	def initialize( status, config, output, irc, timer )
10 11 12 13 14
		@status   = status
		@config   = config
		@output   = output
		@irc      = irc
		@timer    = timer
15
		
16 17
		@api_host = 'btc-e.com'
		@api_path = '/api/2/ltc_usd/ticker'
18
		
19
		@last     = 0.0
20 21 22
	end

	# Alias for last
Cool Fire's avatar
Cool Fire committed
23
	def main( _nick, _user, _host, from, _msg, _arguments, con )
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
		uri = URI.parse( "https://#{@api_host}#{@api_path}" )
		
		http = Net::HTTP.new(uri.host, uri.port)
		http.use_ssl = true
		http.verify_mode = OpenSSL::SSL::VERIFY_NONE
		
		request = Net::HTTP::Get.new(uri.request_uri)
		response = http.request(request)
		
		result = JSON.parse( response.body )
		
		# Calculate delta from API 'last'
		diff = result[ 'ticker' ][ 'sell' ].to_f - result[ 'ticker' ][ 'last' ].to_f
		diff = ( diff * 1000 ).round / 1000.0
		if( diff > 0 )
Cool Fire's avatar
Cool Fire committed
39
			diff = "+#{diff}"
40
		else
Cool Fire's avatar
Cool Fire committed
41
			diff = "#{diff}"
42 43 44 45 46 47
		end

		# Calculate delta from last !btc
		ldiff = result[ 'ticker' ][ 'sell' ].to_f - @last
		ldiff = ( ldiff * 1000 ).round / 1000.0
		if( ldiff > 0 )
Cool Fire's avatar
Cool Fire committed
48
			ldiff = "+#{ldiff}"
49
		else
Cool Fire's avatar
Cool Fire committed
50
			ldiff = "#{ldiff}"
51 52 53
		end
		@last = result[ 'ticker' ][ 'sell' ].to_f
		
54
		rounded = "$#{( result[ 'ticker' ][ 'sell' ].to_f * 100 ).round / 100.0}"
55
		
Cool Fire's avatar
Cool Fire committed
56
		line = "Btc-e rate: #{rounded} (#{result[ 'ticker' ][ 'sell' ]}) #{diff} (#{ldiff} since last !ltc)"
57 58 59 60 61 62
		
		if( con )
			@output.c( line + "\n" )
		else
			@irc.message( from, line )
		end
63 64 65 66 67
		
		# Check for chained calling
		if( caller[ 0 ][ /([a-zA-Z0-9]+)(\.rb)/, 1 ] == "btc" )
			return result[ 'ticker' ][ 'sell' ].to_f
		end
68
	end
Cool Fire's avatar
Cool Fire committed
69

70
	# Function to send help about this plugin (Can also be called by the help plugin.)
Cool Fire's avatar
Cool Fire committed
71
	def help( nick, _user, _host, _from, _msg, _arguments, con )
72 73
		help = [
			"Gets current litecoin values from Btc-e",
74
			"  btc          - Get Btc-e exchange rate (buying rate)"
75 76 77 78 79 80 81 82 83 84 85 86
		]
		
		# Print out help
		help.each do |line|
			if( con )
				@output.c( line + "\n" )
			else
				@irc.notice( nick, line )
			end
		end
	end
end