alch 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 617a0a12f9f2da25046bdd402c248e76ef1048e8
4
+ data.tar.gz: ba05835bfe9aa78861c34936d7d35bf79cccfdf6
5
+ SHA512:
6
+ metadata.gz: 61864e4634bf09e9c0b83f7e15bb83e97bd372b00172b8fa4e0d5c4709da55a26864887878bb1d54c0462e0e198fa5443f38287423a6ba0dc49d7eb07beffcb4
7
+ data.tar.gz: 922f4196c930b09fa2bd590904a101da197bcc1f312bfe4169dd27979a5474db0309060365e2ec70b2d8a9e5c1760eac2b1c163c31f6026ee2b9acdb56bca2d6
@@ -0,0 +1,8 @@
1
+ # Require this file if you are to use this in a project
2
+ # Released under the MIT License agreement
3
+
4
+ # @author RubySometimes (GitHub)
5
+ # @version 1.0.5
6
+ require_relative('alch/item')
7
+ require_relative('alch/osrs')
8
+ require_relative('alch/runes')
@@ -0,0 +1,125 @@
1
+ require_relative('osrs')
2
+ require_relative('runes')
3
+ require('json')
4
+ require('net/http')
5
+
6
+ # Items are anything that can be in your inventory.
7
+ #
8
+ # @author RubySometimes (GitHub)
9
+ class Item
10
+ # Attributes
11
+ attr_reader :id
12
+ attr_reader :name
13
+ attr_reader :price
14
+ attr_reader :store_price
15
+ attr_reader :low_alch
16
+ attr_reader :high_alch
17
+ attr_reader :image
18
+
19
+ # Creates a new Item object
20
+ # @param id - the id of the Item
21
+ # @raise RunTimeError if the id is not an integer
22
+ def initialize(id)
23
+ raise 'Parameter id must be an Integer' unless id.instance_of? Integer
24
+ @id = id
25
+ @name = generate_name
26
+ @price = generate_price
27
+ @store_price = generate_store
28
+ @low_alch = (@store_price * 0.4).to_i
29
+ @high_alch = (@store_price * 0.6).to_i
30
+ @image = generate_image
31
+ end
32
+
33
+ # Class method to get the item's id by name
34
+ # @param item_name - the name of the item to search for
35
+ # the item_name gets capitalized for search.
36
+ # @return the item's id
37
+ # @raise RunTimeError if the item doesn't exist
38
+ def self.id_by_name(item_name)
39
+ item_name = item_name.capitalize
40
+ uri = URI(OSRS::SHOP_DATA)
41
+ json = JSON.parse(Net::HTTP.get(uri))
42
+ item_id = 0
43
+ json.each { |id, name| item_id = id if name['name'].eql? item_name }
44
+ raise "Item (#{item_name}) doesn't exist." if item_id.eql? 0
45
+ item_id.to_i
46
+ end
47
+
48
+ # Checks if this item's GE price is less than another item's
49
+ # @param other - the other item to compare to
50
+ # @return true if this item is worth less than the other_item.
51
+ def <(other)
52
+ price < other.price
53
+ end
54
+
55
+ # Checks if this item's GE price is greater than another item's
56
+ # @param other_item - the other item to compare to
57
+ # @return true if this item is worth less than the other_item.
58
+ def >(other)
59
+ price > other.price
60
+ end
61
+
62
+ # Take another item's price and subtract it from this item
63
+ # @param other - the other item, which price we will subtract from
64
+ # @return self.price - other.price
65
+ def -(other)
66
+ price - other.price
67
+ end
68
+
69
+ # Take another item's price and add it with this item's price
70
+ # @param other - the other item, which price we will subtract from
71
+ # @return self.price - other.price
72
+ def +(other)
73
+ price + other.price
74
+ end
75
+
76
+ private
77
+
78
+ # Gets the name of the Item
79
+ # @return the name of the Item
80
+ def generate_name
81
+ uri = URI(OSRS::SHOP_DATA)
82
+ json = JSON.parse(Net::HTTP.get(uri))
83
+ json[@id.to_s]['name']
84
+ end
85
+
86
+ # Gets the store price of the Item
87
+ # @return the store price of the Item
88
+ def generate_store
89
+ uri = URI(OSRS::SHOP_DATA)
90
+ json = JSON.parse(Net::HTTP.get(uri))
91
+ json[@id.to_s]['store'].to_i
92
+ end
93
+
94
+ # Gets the average price of the Item on the GE
95
+ # @return the average price on the GE
96
+ def generate_price
97
+ uri = URI(OSRS::GE_JSON + @id.to_s)
98
+ json = JSON.parse(Net::HTTP.get(uri))
99
+ price_to_int(json['item']['current']['price'])
100
+ end
101
+
102
+ # Gets the image url of the item.
103
+ # @return the image url of the item as a string.
104
+ def generate_image
105
+ uri = URI(OSRS::GE_JSON + @id.to_s)
106
+ json = JSON.parse(Net::HTTP.get(uri))
107
+ json['item']['icon_large']
108
+ end
109
+
110
+ # Turns a price, like 1.9m and converts to an Integer.
111
+ # @param price - the price of an item in string form
112
+ # @return the integer form of a price.
113
+ def price_to_int(price)
114
+ price_float = clean_price(price.to_s)
115
+ price_float *= 1_000_000 if price[-1] == 'm'
116
+ price_float *= 1_000 if price[-1] == 'k'
117
+ price_float.to_i
118
+ end
119
+
120
+ # Takes a price as a string, and removes any commas.
121
+ # @param price - the price from the JSON in string form.
122
+ def clean_price(price)
123
+ price.sub(/,/, '').to_f
124
+ end
125
+ end
@@ -0,0 +1,7 @@
1
+ # Old School Runescape Module
2
+ module OSRS
3
+ # Links to GE specific data
4
+ GE_JSON = 'http://services.runescape.com/m=itemdb_oldschool/api/catalogue/detail.json?item='.freeze
5
+ # Link to Shop Data
6
+ SHOP_DATA = 'http://marchy.me/data/shop.json'.freeze
7
+ end
@@ -0,0 +1,7 @@
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
metadata ADDED
@@ -0,0 +1,47 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: alch
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Marcello A. Sabino
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-10-03 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A simple way to determine alch prices on Oldschool Runescape.
14
+ email: march@marchy.me
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/alch.rb
20
+ - lib/alch/item.rb
21
+ - lib/alch/osrs.rb
22
+ - lib/alch/runes.rb
23
+ homepage: http://rubygems.org/gems/alch
24
+ licenses:
25
+ - MIT
26
+ metadata: {}
27
+ post_install_message:
28
+ rdoc_options: []
29
+ require_paths:
30
+ - lib
31
+ required_ruby_version: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ required_rubygems_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ requirements: []
42
+ rubyforge_project:
43
+ rubygems_version: 2.6.13
44
+ signing_key:
45
+ specification_version: 4
46
+ summary: Grab OSRS Alch Prices
47
+ test_files: []