alch 2.0.0 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (6) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/lib/alch.rb +1 -1
  4. data/lib/item.rb +49 -56
  5. data/lib/osrs.rb +7 -2
  6. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ff2bd271557caedefb37170982b626bd69f5e359
4
- data.tar.gz: e32809b2594420db925e84257506fe5d07b3e3a2
3
+ metadata.gz: 0421dd1fabbd36b971e6828a591e9c554d95fec9
4
+ data.tar.gz: c123074c01000c9baa261464c56ec1f6578fb50e
5
5
  SHA512:
6
- metadata.gz: 3ea9bf387fa650c21ccf3ddf83b7960902ea036939290ab30c46fc95fcf8c0ab712c0e91db2acd74cb509e26fabf300261d7601df4660c0b1cc91c96a2d940f1
7
- data.tar.gz: aa7f18f3e1346c4b5973097de2831c8e504c634d3edc7f7b0626c7086b785e098c6485c4c9e4983fdb6fad11382a4ee5bed5c5b9e4907c279924e91751b9f92c
6
+ metadata.gz: 4ce6b1c8257fc877390039b1c6de64d5a2161be73e63bf296e86bec2986c6812b441d30529873276e4e47b331d3c0fceff29a8b46934539a47d6107097830d7b
7
+ data.tar.gz: bb050fe0c9dace8e2038a4370d1204f18aaa733ab585d4849054902e13de31ff9ae403257eb486c40b2e8e058332a50a1d523e3ebcf9cdd2b03ccfbfa27d0280
data/README.md CHANGED
@@ -31,4 +31,4 @@ puts item.high_alch - (item.price + nature_rune.price)
31
31
  ```
32
32
 
33
33
  # Full Documentation
34
- [View the Full Documentation on RubyGems](http://www.rubydoc.info/gems/alch/2.0.0)
34
+ [View the Full Documentation on RubyGems](http://www.rubydoc.info/gems/alch/2.1.0)
@@ -2,6 +2,6 @@
2
2
  # Released under the MIT License agreement
3
3
 
4
4
  # @author Marcello A. Sabino
5
- # @version 2.0.0
5
+ # @version 2.1.0
6
6
  require_relative 'item'
7
7
  require_relative 'osrs'
@@ -15,7 +15,7 @@ class Item
15
15
  # @return [Integer] the price based on the Grand Exchange
16
16
  attr_reader :price
17
17
  # @return [Integer] the price based on the store
18
- attr_reader :store_price
18
+ attr_reader :store
19
19
  # @return [Integer] the amount of GP from low alching
20
20
  attr_reader :low_alch
21
21
  # @return [Integer] the amount of GP from high alching
@@ -25,49 +25,35 @@ class Item
25
25
 
26
26
  # Creates a new Item object
27
27
  #
28
- # You must give the Item's ID to create an item.
28
+ # You can create a new Item by specifying the Item's name or
29
+ # the Item's id number as an integer.
29
30
  #
30
- # @note it takes parameter id and casts to a String type
31
- # @param id - the ID of the Item (numerical)
31
+ # @note when passing the id number as the parameter, it must be an Integer
32
+ # @note when passing the Item's name as the parameter, it's case INSENSITIVE
33
+ # @param item - either the Item's name as a String or Item's id as an Integer
32
34
  # @return [Item] a new Item object
33
35
  # @example Create a new Item
34
36
  # Item.new(1603) #=> Item
35
- # Item.new(Item.id_by_name('ruby')) #=> Item
36
- def initialize(id)
37
- @id = id.to_s
38
- @name = generate_name
39
- @price = generate_price
40
- @store_price = generate_store
41
- @low_alch = (@store_price * 0.4).to_i
42
- @high_alch = (@store_price * 0.6).to_i
43
- @image = generate_image
44
- end
45
-
46
- # Class method to get the Item's ID by Item's name
47
- # @note The `item_name` gets capitalized for search!
48
- # So this is case insensitive.
49
- # @param item_name [String] the name of the Item to search
50
- # @return [Integer] the Item's ID
51
- # @raise RunTimeError if the Item doesn't exist
52
- # @example Creating an Item object
53
- # Item.new(Item.id_by_name('uncut ruby')) #=> Item
54
- # Item.new(Item.id_by_name('UNCUT rUby')) #=> Item
55
- # Item.new(Item.id_by_name('incit rbuy')) #=> RunTimeError
56
- def self.id_by_name(item_name)
57
- item_name.capitalize!
58
- json = JSON.parse(open(OSRS::SHOP_DATA, &:read))
59
- item_id = 0
60
- json.each { |id, name| item_id = id if name['name'].eql? item_name }
61
- raise "Item (#{item_name}) doesn't exist." if item_id.eql? 0
62
- item_id.to_i
37
+ # Item.new('ruby') #=> Item
38
+ # Item.new('1603') #=> RunTimeError (Item doesn't exist)
39
+ def initialize(item)
40
+ @id = item if item.is_a?(Integer)
41
+ @id = id_by_name(item) if item.is_a?(String)
42
+ @grand_exchange = JSON.parse(open(OSRS::GE_JSON + @id.to_s, &:read)).freeze
43
+ @name = OSRS::STORE_DATA[@id.to_s]['name']
44
+ @price = price_to_int(@grand_exchange['item']['current']['price'].to_s)
45
+ @store = OSRS::STORE_DATA[@id.to_s]['store'].to_i
46
+ @low_alch = (@store * 0.4).to_i
47
+ @high_alch = (@store * 0.6).to_i
48
+ @image = @grand_exchange['item']['icon_large']
63
49
  end
64
50
 
65
51
  # Checks if this item's GE price is less than another item's
66
52
  # @param [Item] other - the other item to compare to
67
53
  # @return [Boolean] true if this item is worth less than the other item.
68
54
  # @example Example
69
- # abyssal_whip = Item.new(Item.id_by_name('abyssal whip'))
70
- # ruby = Item.new(Item.id_by_name('ruby'))
55
+ # abyssal_whip = Item.new('abyssal whip')
56
+ # ruby = Item.new('ruby')
71
57
  # price = abyssal_whip < ruby #=> false
72
58
  def <(other)
73
59
  price < other.price
@@ -77,8 +63,8 @@ class Item
77
63
  # @param [Item] other - the other item to compare to
78
64
  # @return [Boolean] true if this item is worth less than the other item.
79
65
  # @example Example
80
- # abyssal_whip = Item.new(Item.id_by_name('abyssal whip'))
81
- # ruby = Item.new(Item.id_by_name('ruby'))
66
+ # abyssal_whip = Item.new('abyssal whip')
67
+ # ruby = Item.new('ruby')
82
68
  # price = abyssal_whip > ruby #=> true
83
69
  def >(other)
84
70
  price > other.price
@@ -88,8 +74,8 @@ class Item
88
74
  # @param [Item] other - the other item, which price we will subtract from
89
75
  # @return [Integer] self.price - other.price
90
76
  # @example Example
91
- # abyssal_whip = Item.new(Item.id_by_name('abyssal whip'))
92
- # santa_hat = Item.new(Item.id_by_name('santa hat'))
77
+ # abyssal_whip = Item.new('abyssal whip')
78
+ # santa_hat = Item.new('santa hat')
93
79
  # price = abyssal_whip - santa_hat #=> 1788200
94
80
  def -(other)
95
81
  price - other.price
@@ -99,34 +85,41 @@ class Item
99
85
  # @param [Item] other - the other item, which price we will subtract from
100
86
  # @return [Integer] self.price - other.price
101
87
  # @example Example
102
- # abyssal_whip = Item.new(Item.id_by_name('abyssal whip'))
103
- # santa_hat = Item.new(Item.id_by_name('santa hat'))
88
+ # abyssal_whip = Item.new('abyssal whip')
89
+ # santa_hat = Item.new('santa hat')
104
90
  # price = abyssal_whip + santa_hat #=> 1811800
105
91
  def +(other)
106
92
  price + other.price
107
93
  end
108
94
 
109
- private
110
-
111
- # @return [String] the name of the Item
112
- def generate_name
113
- JSON.parse(open(OSRS::SHOP_DATA, &:read))[@id]['name']
95
+ # @example Profit from high alching a Rune Dagger
96
+ # Item.new('rune dagger').high_alch #=> 26
97
+ # @return the total profit from high alching this Item
98
+ def high_profit
99
+ high_alch - (self + Item.new(561))
114
100
  end
115
101
 
116
- # @return [Integer] the Grand Exchange price of the Item
117
- def generate_price
118
- json = JSON.parse(open(OSRS::GE_JSON + @id, &:read))
119
- price_to_int(json['item']['current']['price'])
102
+ # @example Profit from low alching a Rune Dagger
103
+ # Item.new('rune dagger').low_price #=> -1574
104
+ # @return the total profit from low alching this Item
105
+ def low_profit
106
+ low_alch - (self + Item.new(561))
120
107
  end
121
108
 
122
- # @return [Integer] the store price of the Item
123
- def generate_store
124
- JSON.parse(open(OSRS::SHOP_DATA, &:read))[@id]['store'].to_i
125
- end
109
+ private
126
110
 
127
- # @return [String] image url of the Item
128
- def generate_image
129
- JSON.parse(open(OSRS::GE_JSON + @id, &:read))['item']['icon_large']
111
+ # Gets the id of the item based on it's name
112
+ # @param [String] item_name - the name of the Item
113
+ # @return [Integer] Item's id
114
+ # @raise RunTimeError - if the Item doesn't exist in the database
115
+ def id_by_name(item_name)
116
+ item_name.capitalize!.freeze
117
+ item_id = 0
118
+ OSRS::STORE_DATA.each do |id, name|
119
+ item_id = id if name['name'] == item_name
120
+ end
121
+ raise "Item (#{item_name}) doesn't exist." if item_id.eql? 0
122
+ item_id.to_i
130
123
  end
131
124
 
132
125
  # Turns a price, like 1.9m and converts it to an Integer.
@@ -1,8 +1,13 @@
1
+ require 'json'
2
+ require 'open-uri'
3
+
1
4
  # Old School Runescape Module
2
5
  # @since 0.0.2
3
6
  module OSRS
4
7
  # Links to GE specific data
5
8
  GE_JSON = 'http://services.runescape.com/m=itemdb_oldschool/api/catalogue/detail.json?item='.freeze
6
- # Link to Shop Data
7
- SHOP_DATA = 'http://marchy.me/data/shop.json'.freeze
9
+ # Link to Store JSON
10
+ STORE_JSON = 'http://marchy.me/data/shop.json'.freeze
11
+ # Store data
12
+ STORE_DATA = JSON.parse(open(STORE_JSON, &:read)).freeze
8
13
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: alch
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marcello A. Sabino