guilding-swiftly 0.0.1

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 (4) hide show
  1. checksums.yaml +7 -0
  2. data/lib/Price.rb +27 -0
  3. data/lib/guilding-swiftly.rb +41 -0
  4. metadata +45 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8af255b78137eec02d0dcf1aed1b46a5bee999d1
4
+ data.tar.gz: 85bd7bb17179540a92da5159fd2df9337bdfaae5
5
+ SHA512:
6
+ metadata.gz: 17989ec1a32e8c974d6befd8848f3f03bb7ff5afd2fb51f458965db0c974482c162b4ffb755f35f277838d9d54ee26f269b616bb9a96c09ab2ab321390196e9e
7
+ data.tar.gz: d48468d92efa925b47e0458581bd982d74eda64421619dd95ef875bb631d922b3898b12dc743388d7fedc78e5889cab08fecb3a4750bc6d078b21f4b7cefe772
data/lib/Price.rb ADDED
@@ -0,0 +1,27 @@
1
+ class Price < Struct.new(:low, :high)
2
+
3
+ def +(price)
4
+ Price.new(self.low + price.low, self.high + price.high)
5
+ end
6
+
7
+ def *(num)
8
+ Price.new(self.low * num, self.high * num)
9
+ end
10
+
11
+ def to_s
12
+ "#{Price.pretty_print self.low} - #{Price.pretty_print self.high}"
13
+ end
14
+
15
+ def Price.none
16
+ Price.new(0, 0)
17
+ end
18
+
19
+ def self.pretty_print(total_copper)
20
+ copper = total_copper % 100
21
+ silver = (total_copper / 100) % 100
22
+ gold = total_copper / (100 * 100)
23
+ "#{gold}g #{silver}s #{copper}c"
24
+ end
25
+
26
+ end
27
+
@@ -0,0 +1,41 @@
1
+ require 'net/http'
2
+ require 'uri'
3
+ require 'json'
4
+ require 'rack'
5
+ require_relative 'Price'
6
+
7
+ BASE_URI = 'https://api.guildwars2.com/v2/'
8
+
9
+ module GW2API
10
+
11
+ def self.get(path, query_hash = {})
12
+ query_string = query_hash.length > 0 ? '?' + Rack::Utils.build_query(query_hash) : ''
13
+ uri = URI::join(BASE_URI, path) + query_string
14
+ response = Net::HTTP.get_response(uri)
15
+ JSON.parse(response.body)
16
+ end
17
+
18
+ def self.get_mats_value(api_key)
19
+ bank_slots = get(
20
+ 'account/materials',
21
+ {:access_token => api_key}
22
+ ).select { |bank_slot| bank_slot['count'] > 0 }
23
+
24
+ bank_slots.each_with_index.map do |bank_slot, i|
25
+ STDOUT.write "#{i}/#{bank_slots.length} (#{(i.fdiv(bank_slots.length) * 100).round}%)\r"
26
+ get_value(bank_slot['id']) * bank_slot['count']
27
+ end
28
+ end
29
+
30
+ def self.get_value(item_id)
31
+ if !item_id then
32
+ puts "Missing entry."
33
+ return Price.none
34
+ end
35
+
36
+ results = get("commerce/prices/#{item_id}")
37
+ results['id'] ? Price.new(results["buys"]["unit_price"], results["sells"]["unit_price"]) : Price.none
38
+ end
39
+
40
+ end
41
+
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: guilding-swiftly
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Sam Collard
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-10-04 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Helper methods for the Guild Wars 2 API.
14
+ email: swift@swifteagle.co.uk
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/Price.rb
20
+ - lib/guilding-swiftly.rb
21
+ homepage: https://github.com/CheshireSwift/guilding-swiftly
22
+ licenses:
23
+ - MIT
24
+ metadata: {}
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubyforge_project:
41
+ rubygems_version: 2.4.5
42
+ signing_key:
43
+ specification_version: 4
44
+ summary: Helper methods for the Guild Wars 2 API.
45
+ test_files: []