class Item
Items are anything that are in the Grand Exchange, traded, or alched.
@author Marcello A. Sabino @since 0.0.2
Attributes
@return [Integer] the amount of GP from high alching
@return [Integer] the Item's ID
@return [String] the image url of the Item
@return [Integer] the amount of GP from low alching
@return [String] the Item's name
@return [Integer] the price based on the Grand Exchange
@return [Integer] the price based on the store
Public Class Methods
Creates a new Item
object
You can create a new Item
by specifying the Item's name or the Item's id number as an integer.
@note when passing the id number as the parameter, it must be an Integer @note when passing the Item's name as the parameter, it's case INSENSITIVE @param item - either the Item's name as a String or Item's id as an Integer @return [Item] a new Item
object @example Create a new Item
Item.new(1603) #=> Item Item.new('ruby') #=> Item Item.new('1603') #=> RunTimeError (Item doesn't exist)
# File lib/item.rb, line 39 def initialize(item) @id = item if item.is_a?(Integer) @id = id_by_name(item) if item.is_a?(String) @grand_exchange = JSON.parse(open(OSRS::GE_JSON + @id.to_s, &:read)).freeze @name = OSRS::STORE_DATA[@id.to_s]['name'] @price = price_to_int(@grand_exchange['item']['current']['price'].to_s) @store = OSRS::STORE_DATA[@id.to_s]['store'].to_i @low_alch = (@store * 0.4).to_i @high_alch = (@store * 0.6).to_i @image = @grand_exchange['item']['icon_large'] end
Public Instance Methods
Take another item's price and add it with this item's price @param [Item] other - the other item, which price we will subtract from @return [Integer] self.price - other.price @example Example
abyssal_whip = Item.new('abyssal whip') santa_hat = Item.new('santa hat') price = abyssal_whip + santa_hat #=> 1811800
# File lib/item.rb, line 91 def +(other) price + other.price end
Take another item's price and subtract it from this item @param [Item] other - the other item, which price we will subtract from @return [Integer] self.price - other.price @example Example
abyssal_whip = Item.new('abyssal whip') santa_hat = Item.new('santa hat') price = abyssal_whip - santa_hat #=> 1788200
# File lib/item.rb, line 80 def -(other) price - other.price end
Checks if this item's GE price is less than another item's @param [Item] other - the other item to compare to @return [Boolean] true if this item is worth less than the other item. @example Example
abyssal_whip = Item.new('abyssal whip') ruby = Item.new('ruby') price = abyssal_whip < ruby #=> false
# File lib/item.rb, line 58 def <(other) price < other.price end
Checks if this item's GE price is greater than another item's @param [Item] other - the other item to compare to @return [Boolean] true if this item is worth less than the other item. @example Example
abyssal_whip = Item.new('abyssal whip') ruby = Item.new('ruby') price = abyssal_whip > ruby #=> true
# File lib/item.rb, line 69 def >(other) price > other.price end
@example Profit from high alching a Rune Dagger
Item.new('rune dagger').high_profit #=> 26
@return the total profit from high alching this Item
# File lib/item.rb, line 98 def high_profit high_alch - (self + Item.new(561)) end
@example Profit from low alching a Rune Dagger
Item.new('rune dagger').low_profit #=> -1574
@return the total profit from low alching this Item
# File lib/item.rb, line 105 def low_profit low_alch - (self + Item.new(561)) end
Private Instance Methods
Gets the id of the item based on it's name @param [String] item_name - the name of the Item
@return [Integer] Item's id @raise RunTimeError - if the Item
doesn't exist in the database
# File lib/item.rb, line 115 def id_by_name(item_name) item_name.capitalize!.freeze item_id = 0 OSRS::STORE_DATA.each do |id, name| item_id = id if name['name'] == item_name end raise "Item (#{item_name}) doesn't exist." if item_id.eql? 0 item_id.to_i end
Turns a price, like 1.9m and converts it to an Integer. @param price [String] the price of an Item
@return [Integer] the price as an Integer
# File lib/item.rb, line 128 def price_to_int(price) pf = price.sub(/,/, '').to_f # strip commas return (pf *= 1_000_000).to_i if price[-1] == 'm' return (pf *= 1_000).to_i if price[-1] == 'k' pf.to_i end