alch 1.1.1 → 2.0.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f36ac89d85c1648f5362ab9a6897b973fb6895f6
4
- data.tar.gz: e66ce5de6bff1c0d40b9c09b6ff90989666e837d
3
+ metadata.gz: ff2bd271557caedefb37170982b626bd69f5e359
4
+ data.tar.gz: e32809b2594420db925e84257506fe5d07b3e3a2
5
5
  SHA512:
6
- metadata.gz: b597cf3f2a5ee2891fb3a6c0b9a2b4d7db2f28ba11aab4d200c7356073b3bf2e610b9c958303b25dc7c2735d5721264df5246d7d54400d47b2b9183f8479da1c
7
- data.tar.gz: 744b5551e403863d6ef9b3336049d64cf9c2384986ad6ab13bc3f7047068161616a0796f20dacf4ad10ac11370684e7285e63c01bb664bd5d0551e863280a818
6
+ metadata.gz: 3ea9bf387fa650c21ccf3ddf83b7960902ea036939290ab30c46fc95fcf8c0ab712c0e91db2acd74cb509e26fabf300261d7601df4660c0b1cc91c96a2d940f1
7
+ data.tar.gz: aa7f18f3e1346c4b5973097de2831c8e504c634d3edc7f7b0626c7086b785e098c6485c4c9e4983fdb6fad11382a4ee5bed5c5b9e4907c279924e91751b9f92c
data/README.md CHANGED
@@ -1,45 +1,29 @@
1
1
  # Alch.rb
2
- A simplistic Runescape Alchemy API that can help you get alch prices of certain items.
2
+ A simplistic Runescape Alchemy API that can help you get alch prices of certain items in no time at all. The API is well-documented with good examples. Start building your application now!
3
3
 
4
4
  ## Installation
5
- You can get the newest gem of Alch.rb. Just enter the following in your command line:
5
+ You can get the newest gem of `Alch.rb` just by using the following command:
6
6
 
7
- `$ gem install alch`
7
+ `gem install alch`
8
8
 
9
- That's it! You are now able to include the API into your projects.
9
+ That's it! You're now able to include the API into your projects.
10
10
 
11
- If you plan on using alch in a class or file, you have to require it at the top of your Ruby file, like so:
11
+ If you plan on using Alch in your project, you will need to require it at the top of the Ruby file, like so:
12
12
 
13
- ```ruby
14
- require 'alch'
15
- ```
13
+ `require 'alch'`
16
14
 
17
- ### Dependencies
18
- Ruby 2.0 and up
15
+ ## Dependencies
16
+ * `Ruby 2.3`
19
17
 
20
18
  ## Usage Examples
21
- A very basic use case:
19
+ Basic use case:
22
20
 
23
21
  ```ruby
24
22
  require 'alch'
25
23
 
26
24
  # Create ruby and nature rune items
27
25
  item = Item.new(1603)
28
- nature_rune = Item.new(Runes::NATURE_RUNE_ID)
29
-
30
- # output => 'Profit from alching Ruby: -692'
31
- print "Profit from alching #{item.name}: "
32
- puts item.high_alch - (item.price + nature_rune.price)
33
- ```
34
-
35
- Alternatively, you can also get the Item's ID by the Item's name.
36
-
37
- ```ruby
38
- require 'alch'
39
-
40
- # Create ruby and nature rune items
41
- item = Item.new(Item.id_by_name('ruby'))
42
- nature_rune = Item.new(Runes::NATURE_RUNE_ID)
26
+ nature_rune = Item.new(Item.id_by_name('nature rune'))
43
27
 
44
28
  # output => 'Profit from alching Ruby: -692'
45
29
  print "Profit from alching #{item.name}: "
@@ -47,4 +31,4 @@ puts item.high_alch - (item.price + nature_rune.price)
47
31
  ```
48
32
 
49
33
  # Full Documentation
50
- [View the Full Documentation](http://www.rubydoc.info/gems/alch/1.1.1)
34
+ [View the Full Documentation on RubyGems](http://www.rubydoc.info/gems/alch/2.0.0)
@@ -1,8 +1,7 @@
1
- # Require this file if you are to use this in a project
1
+ # Require this file if you wish to use this in a project
2
2
  # Released under the MIT License agreement
3
3
 
4
- # @author RubySometimes (GitHub)
5
- # @version 1.1.1
6
- require_relative('alch/item')
7
- require_relative('alch/osrs')
8
- require_relative('alch/runes')
4
+ # @author Marcello A. Sabino
5
+ # @version 2.0.0
6
+ require_relative 'item'
7
+ require_relative 'osrs'
@@ -0,0 +1,141 @@
1
+ require 'open-uri'
2
+ require 'json'
3
+ require_relative 'osrs'
4
+
5
+ # Items are anything that are in the Grand Exchange,
6
+ # traded, or alched.
7
+ #
8
+ # @author Marcello A. Sabino
9
+ # @since 0.0.2
10
+ class Item
11
+ # @return [Integer] the Item's ID
12
+ attr_reader :id
13
+ # @return [String] the Item's name
14
+ attr_reader :name
15
+ # @return [Integer] the price based on the Grand Exchange
16
+ attr_reader :price
17
+ # @return [Integer] the price based on the store
18
+ attr_reader :store_price
19
+ # @return [Integer] the amount of GP from low alching
20
+ attr_reader :low_alch
21
+ # @return [Integer] the amount of GP from high alching
22
+ attr_reader :high_alch
23
+ # @return [String] the image url of the Item
24
+ attr_reader :image
25
+
26
+ # Creates a new Item object
27
+ #
28
+ # You must give the Item's ID to create an item.
29
+ #
30
+ # @note it takes parameter id and casts to a String type
31
+ # @param id - the ID of the Item (numerical)
32
+ # @return [Item] a new Item object
33
+ # @example Create a new Item
34
+ # 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
63
+ end
64
+
65
+ # Checks if this item's GE price is less than another item's
66
+ # @param [Item] other - the other item to compare to
67
+ # @return [Boolean] true if this item is worth less than the other item.
68
+ # @example Example
69
+ # abyssal_whip = Item.new(Item.id_by_name('abyssal whip'))
70
+ # ruby = Item.new(Item.id_by_name('ruby'))
71
+ # price = abyssal_whip < ruby #=> false
72
+ def <(other)
73
+ price < other.price
74
+ end
75
+
76
+ # Checks if this item's GE price is greater than another item's
77
+ # @param [Item] other - the other item to compare to
78
+ # @return [Boolean] true if this item is worth less than the other item.
79
+ # @example Example
80
+ # abyssal_whip = Item.new(Item.id_by_name('abyssal whip'))
81
+ # ruby = Item.new(Item.id_by_name('ruby'))
82
+ # price = abyssal_whip > ruby #=> true
83
+ def >(other)
84
+ price > other.price
85
+ end
86
+
87
+ # Take another item's price and subtract it from this item
88
+ # @param [Item] other - the other item, which price we will subtract from
89
+ # @return [Integer] self.price - other.price
90
+ # @example Example
91
+ # abyssal_whip = Item.new(Item.id_by_name('abyssal whip'))
92
+ # santa_hat = Item.new(Item.id_by_name('santa hat'))
93
+ # price = abyssal_whip - santa_hat #=> 1788200
94
+ def -(other)
95
+ price - other.price
96
+ end
97
+
98
+ # Take another item's price and add it with this item's price
99
+ # @param [Item] other - the other item, which price we will subtract from
100
+ # @return [Integer] self.price - other.price
101
+ # @example Example
102
+ # abyssal_whip = Item.new(Item.id_by_name('abyssal whip'))
103
+ # santa_hat = Item.new(Item.id_by_name('santa hat'))
104
+ # price = abyssal_whip + santa_hat #=> 1811800
105
+ def +(other)
106
+ price + other.price
107
+ end
108
+
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']
114
+ end
115
+
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'])
120
+ end
121
+
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
126
+
127
+ # @return [String] image url of the Item
128
+ def generate_image
129
+ JSON.parse(open(OSRS::GE_JSON + @id, &:read))['item']['icon_large']
130
+ end
131
+
132
+ # Turns a price, like 1.9m and converts it to an Integer.
133
+ # @param price [String] the price of an Item
134
+ # @return [Integer] the price as an Integer
135
+ def price_to_int(price)
136
+ pf = price.sub(/,/, '').to_f # strip commas
137
+ return (pf *= 1_000_000).to_i if price[-1] == 'm'
138
+ return (pf *= 1_000).to_i if price[-1] == 'k'
139
+ pf.to_i
140
+ end
141
+ end
@@ -1,4 +1,5 @@
1
1
  # Old School Runescape Module
2
+ # @since 0.0.2
2
3
  module OSRS
3
4
  # Links to GE specific data
4
5
  GE_JSON = 'http://services.runescape.com/m=itemdb_oldschool/api/catalogue/detail.json?item='.freeze
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: alch
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marcello A. Sabino
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-10-16 00:00:00.000000000 Z
11
+ date: 2017-10-19 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A simple way to determine alch prices on Oldschool Runescape
14
14
  email: march@marchy.me
@@ -18,9 +18,8 @@ extra_rdoc_files: []
18
18
  files:
19
19
  - README.md
20
20
  - lib/alch.rb
21
- - lib/alch/item.rb
22
- - lib/alch/osrs.rb
23
- - lib/alch/runes.rb
21
+ - lib/item.rb
22
+ - lib/osrs.rb
24
23
  homepage: http://rubygems.org/gems/alch
25
24
  licenses:
26
25
  - MIT
@@ -33,7 +32,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
33
32
  requirements:
34
33
  - - ">="
35
34
  - !ruby/object:Gem::Version
36
- version: '0'
35
+ version: 2.3.0
37
36
  required_rubygems_version: !ruby/object:Gem::Requirement
38
37
  requirements:
39
38
  - - ">="
@@ -1,136 +0,0 @@
1
- require 'json'
2
- require 'net/http'
3
- require_relative 'osrs'
4
- require_relative 'runes'
5
-
6
- # Items are anything that can be in your inventory.
7
- #
8
- # @author Marcello A. Sabino
9
- class Item
10
- # Returns the id of the item
11
- # @return [Integer] the item's id
12
- attr_reader :id
13
- # Returns the item's name
14
- # @return [String] the item's name
15
- attr_reader :name
16
- # Returns the Grand Exchange price of the item
17
- # @return [Integer] the Grand Exchange's price
18
- attr_reader :price
19
- # Returns the store's price of the item
20
- # @return [Integer] the store's price
21
- attr_reader :store_price
22
- # Returns the gold pieces from low alching the item
23
- # @return [Integer] the amount of GP from low alching
24
- attr_reader :low_alch
25
- # Returns the golf pieces from high alching the item
26
- # @return [Integer] the amount of GP from high alching
27
- attr_reader :high_alch
28
- # Returns the image url of the item as a string.
29
- # @return [String] the image url of the item, on Runescape.com
30
- attr_reader :image
31
-
32
- # Creates a new Item object
33
- # @param [Integer] id - the id of the Item
34
- def initialize(id)
35
- @id = id.to_i
36
- @name = generate_name
37
- @price = generate_price
38
- @store_price = generate_store
39
- @low_alch = (@store_price * 0.4).to_i
40
- @high_alch = (@store_price * 0.6).to_i
41
- @image = generate_image
42
- end
43
-
44
- # Class method to get the item's id by name
45
- # @param [String] item_name - the name of the item to search for
46
- # the item_name gets capitalized for search.
47
- # @return [Integer] the item's id
48
- # @raise RunTimeError if the item doesn't exist
49
- def self.id_by_name(item_name)
50
- item_name = item_name.capitalize
51
- uri = URI(OSRS::SHOP_DATA)
52
- json = JSON.parse(Net::HTTP.get(uri))
53
- item_id = 0
54
- json.each { |id, name| item_id = id if name['name'].eql? item_name }
55
- raise "Item (#{item_name}) doesn't exist." if item_id.eql? 0
56
- item_id.to_i
57
- end
58
-
59
- # Checks if this item's GE price is less than another item's
60
- # @param [Item] other - the other item to compare to
61
- # @return [Boolean] true if this item is worth less than the other_item.
62
- def <(other)
63
- price < other.price
64
- end
65
-
66
- # Checks if this item's GE price is greater than another item's
67
- # @param [Item] other - the other item to compare to
68
- # @return [Boolean] true if this item is worth less than the other_item.
69
- def >(other)
70
- price > other.price
71
- end
72
-
73
- # Take another item's price and subtract it from this item
74
- # @param [Item] other - the other item, which price we will subtract from
75
- # @return [Integer] self.price - other.price
76
- def -(other)
77
- price - other.price
78
- end
79
-
80
- # Take another item's price and add it with this item's price
81
- # @param [Item] other - the other item, which price we will subtract from
82
- # @return [Integer] self.price - other.price
83
- def +(other)
84
- price + other.price
85
- end
86
-
87
- private
88
-
89
- # Gets the name of the Item
90
- # @return the name of the Item
91
- def generate_name
92
- uri = URI(OSRS::SHOP_DATA)
93
- json = JSON.parse(Net::HTTP.get(uri))
94
- json[@id.to_s]['name']
95
- end
96
-
97
- # Gets the store price of the Item
98
- # @return [Integer] the store price of the Item
99
- def generate_store
100
- uri = URI(OSRS::SHOP_DATA)
101
- json = JSON.parse(Net::HTTP.get(uri))
102
- json[@id.to_s]['store'].to_i
103
- end
104
-
105
- # Gets the average price of the Item on the GE
106
- # @return [Integer] the average price on the GE
107
- def generate_price
108
- uri = URI(OSRS::GE_JSON + @id.to_s)
109
- json = JSON.parse(Net::HTTP.get(uri))
110
- price_to_int(json['item']['current']['price'])
111
- end
112
-
113
- # Gets the image url of the item.
114
- # @return the image url of the item as a string.
115
- def generate_image
116
- uri = URI(OSRS::GE_JSON + @id.to_s)
117
- json = JSON.parse(Net::HTTP.get(uri))
118
- json['item']['icon_large']
119
- end
120
-
121
- # Turns a price, like 1.9m and converts to an Integer.
122
- # @param price - the price of an item in string form
123
- # @return [Integer] the integer form of a price.
124
- def price_to_int(price)
125
- price_float = clean_price(price.to_s)
126
- price_float *= 1_000_000 if price[-1] == 'm'
127
- price_float *= 1_000 if price[-1] == 'k'
128
- price_float.to_i
129
- end
130
-
131
- # Takes a price as a string, and removes any commas.
132
- # @param [Float] price - the price from the JSON in string form.
133
- def clean_price(price)
134
- price.sub(/,/, '').to_f
135
- end
136
- end
@@ -1,7 +0,0 @@
1
- # Information on runes that are needed to perform alchemy
2
- module Runes
3
- # Nature Rune ID
4
- NATURE_RUNE_ID = 561
5
- # Fire Rune ID
6
- FIRE_RUNE_ID = 554
7
- end