backpack_tf 0.8.4 → 1.0.0.rc1

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 (88) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -3
  3. data/.rspec +2 -0
  4. data/.travis.yml +3 -0
  5. data/Gemfile +5 -3
  6. data/Gemfile.lock +49 -0
  7. data/README.md +144 -0
  8. data/Rakefile +7 -0
  9. data/backpack_tf.gemspec +2 -2
  10. data/lib/backpack_tf/assets/{attribute_controlled_attached_particles.json → particle_effects.json} +0 -0
  11. data/lib/backpack_tf/client.rb +72 -95
  12. data/lib/backpack_tf/currency/interface.rb +9 -0
  13. data/lib/backpack_tf/currency/response.rb +24 -0
  14. data/lib/backpack_tf/currency.rb +24 -47
  15. data/lib/backpack_tf/helpers.rb +29 -0
  16. data/lib/backpack_tf/interface.rb +29 -0
  17. data/lib/backpack_tf/market_price/interface.rb +9 -0
  18. data/lib/backpack_tf/market_price/response.rb +15 -0
  19. data/lib/backpack_tf/market_price.rb +25 -0
  20. data/lib/backpack_tf/price/interface.rb +19 -0
  21. data/lib/backpack_tf/price/item.rb +85 -0
  22. data/lib/backpack_tf/price/item_price.rb +113 -0
  23. data/lib/backpack_tf/price/particle_effect.rb +27 -0
  24. data/lib/backpack_tf/price/response.rb +38 -0
  25. data/lib/backpack_tf/price.rb +9 -81
  26. data/lib/backpack_tf/price_history/interface.rb +22 -0
  27. data/lib/backpack_tf/price_history/response.rb +15 -0
  28. data/lib/backpack_tf/price_history.rb +25 -0
  29. data/lib/backpack_tf/response.rb +9 -56
  30. data/lib/backpack_tf/special_item/interface.rb +9 -0
  31. data/lib/backpack_tf/special_item/response.rb +16 -0
  32. data/lib/backpack_tf/special_item.rb +11 -21
  33. data/lib/backpack_tf/user/interface.rb +18 -0
  34. data/lib/backpack_tf/user/response.rb +16 -0
  35. data/lib/backpack_tf/user.rb +10 -19
  36. data/lib/backpack_tf/user_listing/interface.rb +18 -0
  37. data/lib/backpack_tf/user_listing/response.rb +15 -0
  38. data/lib/backpack_tf/user_listing.rb +31 -43
  39. data/lib/backpack_tf/version.rb +1 -1
  40. data/lib/backpack_tf.rb +8 -8
  41. data/spec/backpack_tf/client_spec.rb +151 -101
  42. data/spec/backpack_tf/currency/interface_spec.rb +8 -0
  43. data/spec/backpack_tf/currency/response_spec.rb +41 -0
  44. data/spec/backpack_tf/currency_spec.rb +34 -118
  45. data/spec/backpack_tf/helpers_spec.rb +85 -0
  46. data/spec/backpack_tf/interface_spec.rb +56 -0
  47. data/spec/backpack_tf/market_price/interface_spec.rb +8 -0
  48. data/spec/backpack_tf/market_price/response_spec.rb +22 -0
  49. data/spec/backpack_tf/market_price_spec.rb +22 -0
  50. data/spec/backpack_tf/price/interface_spec.rb +27 -0
  51. data/spec/backpack_tf/price/item_price_spec.rb +101 -0
  52. data/spec/backpack_tf/price/item_spec.rb +291 -0
  53. data/spec/backpack_tf/price/particle_effect_spec.rb +23 -0
  54. data/spec/backpack_tf/price/response_spec.rb +87 -0
  55. data/spec/backpack_tf/price_history/interface_spec.rb +38 -0
  56. data/spec/backpack_tf/price_history/response_spec.rb +22 -0
  57. data/spec/backpack_tf/price_history_spec.rb +24 -0
  58. data/spec/backpack_tf/response_spec.rb +22 -108
  59. data/spec/backpack_tf/special_item/interface_spec.rb +8 -0
  60. data/spec/backpack_tf/special_item/response_spec.rb +26 -0
  61. data/spec/backpack_tf/special_item_spec.rb +23 -100
  62. data/spec/backpack_tf/user/interface_spec.rb +20 -0
  63. data/spec/backpack_tf/user/response_spec.rb +27 -0
  64. data/spec/backpack_tf/user_listing/interface_spec.rb +20 -0
  65. data/spec/backpack_tf/user_listing/response_spec.rb +21 -0
  66. data/spec/backpack_tf/user_listing_spec.rb +53 -81
  67. data/spec/backpack_tf/user_spec.rb +16 -83
  68. data/spec/fixtures/item_price_typical.json +6 -0
  69. data/spec/fixtures/item_price_unusual.json +7 -0
  70. data/spec/fixtures/item_typical.json +1 -1
  71. data/spec/fixtures/item_unusual.json +311 -1
  72. data/spec/fixtures/item_with_dual_craftability_tradability.json +59 -0
  73. data/spec/fixtures/market_prices.json +1 -0
  74. data/spec/fixtures/price_history.json +1 -0
  75. data/spec/fixtures/prices.json +80 -1
  76. data/spec/fixtures/user_listing.json +1 -1
  77. data/spec/fixtures/user_listing_individual.json +51 -0
  78. data/spec/spec_helper.rb +12 -52
  79. metadata +61 -16
  80. data/TODO.md +0 -15
  81. data/lib/backpack_tf/item.rb +0 -84
  82. data/lib/backpack_tf/item_price.rb +0 -172
  83. data/readme.md +0 -96
  84. data/spec/backpack_tf/inherited_class_methods_spec.rb +0 -100
  85. data/spec/backpack_tf/item_price_spec.rb +0 -119
  86. data/spec/backpack_tf/item_spec.rb +0 -126
  87. data/spec/backpack_tf/price_spec.rb +0 -184
  88. data/spec/fixtures/item_without_defindex.json +0 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a510923d8b4dc15e3532952f92bed00000910548
4
- data.tar.gz: df6eb944365d8ebe1d718e7154a8fd0842b6719e
3
+ metadata.gz: 8673b991331c19d4f53c025859bce0ea112838c5
4
+ data.tar.gz: 34d35fa2c340a9e239c9525ff26e19768068dd41
5
5
  SHA512:
6
- metadata.gz: 0c2fe92ebc9568cefdb9603a7a432c6e7fc43c1d7569d495af673830ac0e89fd581724cc85b0588420ed45f641bec9047a3db7fbc60f4093536f1e60bf04cabe
7
- data.tar.gz: 5b1dad54cfb43f40b3b81a543b3674046ed70286dd1ea90c19649bb33682c1e3227d838db63335b29ac8d21a34464d8dd8fa4167e59ed1b1348b7936a38494df
6
+ metadata.gz: 12b37a2bf1e298ae661e00faa9f342be27f8e76dde8a853b767801e7b1c650eb45648fbe9de0c2d34246573ae8921500a90266bdcb2f6b3b8d49382b9cb01eae
7
+ data.tar.gz: 08bef1bcf70c9a98bd17f0bdcd23a3c1917816bdd297699b882e7d4d9d5e83160c7d8b6fb9372226eb8f775dedca208faecaba4fa13fde9c92d9bcea408c2b83
data/.gitignore CHANGED
@@ -3,9 +3,7 @@
3
3
 
4
4
  .ruby-version
5
5
  *.gem
6
- Gemfile.lock
7
6
 
7
+ coverage/
8
8
  spec/fixtures/*yml
9
9
  spec/other-library-examples
10
-
11
- scratch/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --order=random
2
+ --no-fail-fast
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - "2.2.3"
data/Gemfile CHANGED
@@ -1,8 +1,10 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- gem 'httparty'
3
+ gem 'httparty', '~> 0.13.5'
4
4
 
5
5
  group :test do
6
- gem 'rspec'
7
- gem 'webmock'
6
+ gem 'rspec', '~> 3.3.0'
7
+ gem 'webmock', '~> 1.21.0'
8
+ gem 'simplecov', '~> 0.11.1', require: false
9
+ gem 'rake', '~> 10.4.2', require: false
8
10
  end
data/Gemfile.lock ADDED
@@ -0,0 +1,49 @@
1
+ GEM
2
+ remote: https://rubygems.org/
3
+ specs:
4
+ addressable (2.3.8)
5
+ crack (0.4.2)
6
+ safe_yaml (~> 1.0.0)
7
+ diff-lcs (1.2.5)
8
+ docile (1.1.5)
9
+ httparty (0.13.5)
10
+ json (~> 1.8)
11
+ multi_xml (>= 0.5.2)
12
+ json (1.8.3)
13
+ multi_xml (0.5.5)
14
+ rake (10.4.2)
15
+ rspec (3.3.0)
16
+ rspec-core (~> 3.3.0)
17
+ rspec-expectations (~> 3.3.0)
18
+ rspec-mocks (~> 3.3.0)
19
+ rspec-core (3.3.2)
20
+ rspec-support (~> 3.3.0)
21
+ rspec-expectations (3.3.1)
22
+ diff-lcs (>= 1.2.0, < 2.0)
23
+ rspec-support (~> 3.3.0)
24
+ rspec-mocks (3.3.2)
25
+ diff-lcs (>= 1.2.0, < 2.0)
26
+ rspec-support (~> 3.3.0)
27
+ rspec-support (3.3.0)
28
+ safe_yaml (1.0.4)
29
+ simplecov (0.11.1)
30
+ docile (~> 1.1.0)
31
+ json (~> 1.8)
32
+ simplecov-html (~> 0.10.0)
33
+ simplecov-html (0.10.0)
34
+ webmock (1.21.0)
35
+ addressable (>= 2.3.6)
36
+ crack (>= 0.3.2)
37
+
38
+ PLATFORMS
39
+ ruby
40
+
41
+ DEPENDENCIES
42
+ httparty (~> 0.13.5)
43
+ rake (~> 10.4.2)
44
+ rspec (~> 3.3.0)
45
+ simplecov (~> 0.11.1)
46
+ webmock (~> 1.21.0)
47
+
48
+ BUNDLED WITH
49
+ 1.10.6
data/README.md ADDED
@@ -0,0 +1,144 @@
1
+ # backpack_tf
2
+
3
+ [![Build Status](https://travis-ci.org/NerdDiffer/backpack_tf.svg?branch=master)](https://travis-ci.org/NerdDiffer/backpack_tf)
4
+
5
+ Backpack.tf is a website for the in-game economies of Team Fortress 2 and
6
+ Dota 2. This gem is a wrapper for the backpack.tf [API](http://backpack.tf/api).
7
+ The goal is to capture the results and turn them into Ruby objects for use in
8
+ your application.
9
+
10
+ ### Contributing
11
+
12
+ If you are interested in contributing, please see any open
13
+ [issues](https://github.com/NerdDiffer/backpack_tf/issues), let me know, and
14
+ branch off of the `development` branch.
15
+
16
+ ### Installation
17
+
18
+ ###### from the command line
19
+
20
+ `$ gem install backpack_tf`
21
+
22
+ ###### from your Gemfile
23
+
24
+ `gem 'backpack_tf'`
25
+
26
+ ### Usage & Examples
27
+
28
+ [Register an API key](http://backpack.tf/developer), pass it in to the client.
29
+
30
+ ``` ruby
31
+ #
32
+ # create a new Client object
33
+ #
34
+ bp = BackpackTF::Client.new(<your_api_key>)
35
+
36
+ #
37
+ # fetch some data
38
+ #
39
+ prices = bp.fetch(:prices)
40
+ price_history = bp.fetch(:price_history, { item: item_name_or_defindex })
41
+ market_prices = bp.fetch(:market_prices)
42
+ currencies = bp.fetch(:currencies)
43
+ special_items = bp.fetch(:special_items)
44
+ users = bp.fetch(:users, { steamids: [array,of,64,bit,steam,ids] })
45
+ listings = bp.fetch(:user_listings, { steamid: 64_bit_steam_id })
46
+
47
+ #
48
+ # assign fetched data to the corresponding module
49
+ #
50
+ BackpackTF::Price.response = prices
51
+ BackpackTF::PriceHistory.response = price_history
52
+ BackpackTF::MarketPrices.response = market_prices
53
+ BackpackTF::Currency.response = currencies
54
+ BackpackTF::SpecialItem.response = special_items
55
+ BackpackTF::User.response = users
56
+ BackpackTF::UserListing.response = listings
57
+ ```
58
+
59
+ ## Interfaces & Responses
60
+
61
+ #### IGetPrices
62
+
63
+ * Get pricing data for all priced items
64
+ * [official doc](http://backpack.tf/api/prices)
65
+
66
+ Information on any particular item, (ie: 'Eviction Notice'), is captured in an
67
+ instance of `BackpackTF::Price::Item`. Furthermore, there may be several prices
68
+ for the same item. For example, an Eviction Notice with the Unique quality has a
69
+ different price than an Eviction Notice with the Strange quality.
70
+
71
+ Each price is an instance of `BackpackTF::Price::ItemPrice`, and is stored in
72
+ the `@prices` hash of that item.
73
+
74
+ ##### A visual representation of this hierarchy
75
+
76
+ * `BackpackTF::Price` module
77
+ * `@items` hash of `BackpackTF::Price::Response`.
78
+ * `BackpackTF::Price::Item` object (ie: 'Beast From Below')
79
+ * `BackpackTF::Price::Item` object (ie: 'Taunt: Rock, Paper Scissors')
80
+ * `BackpackTF::Price::Item` object (ie: 'Eviction Notice')
81
+ * `@prices` hash of a `BackpackTF::Price::Item` object
82
+ * `BackpackTF::Price::ItemPrice` object (ie: price of a Unique item)
83
+ * `BackpackTF::Price::ItemPrice` object (ie: price of a Vintage item)
84
+ * `BackpackTF::Price::ItemPrice` object (ie: price of a Strange item)
85
+
86
+ #### IGetPriceHistory
87
+
88
+ * Get price history for an item
89
+ * [official doc](http://backpack.tf/api/pricehistory)
90
+
91
+ You must pass in the item name or its `defindex` number as value on the `name`
92
+ key when accessing this interface. IE:
93
+
94
+ * `client.fetch(:price_history, item: 829)` or
95
+ * `client.fetch(:price_history, item: 'War Pig')`
96
+
97
+ If you pass in a string as the `item`, then it must match the `item_name`
98
+ property (not the `name` property) as per its definition in the schema. For more
99
+ information, see the
100
+ [TF2 Wiki page on item schema](https://wiki.teamfortress.com/wiki/Item_schema).
101
+
102
+ You can optionally pass the client `quality`, `tradable`, `craftable` or
103
+ `priceindex` keys. See the
104
+ [official doc](http://backpack.tf/api/pricehistory) for more info.
105
+
106
+ #### IGetMarketPrices
107
+
108
+ * Get Steam Community Market price information
109
+ * [official doc](http://backpack.tf/api/market)
110
+
111
+ #### IGetCurrencies
112
+
113
+ * Get internal currency data
114
+ * [official doc](http://backpack.tf/api/currencies)
115
+
116
+ There are currently 4 currencies available through the API.
117
+ Each one is an instance of `BackpackTF::Currency` and is held in the
118
+ `@currencies` hash of the `BackpackTF::Currency::Response` class.
119
+
120
+ #### IGetSpecialItems
121
+
122
+ * Get internal backpack.tf item placeholders
123
+ * [official doc](http://backpack.tf/api/special)
124
+
125
+ This is for items that only exist on backpack.tf. They are not real game items,
126
+ but you will see them returned in a call to `IGetSpecialItems`.
127
+
128
+ #### IGetUsers
129
+
130
+ * Get profile info for a list of 64-bit Steam IDs.
131
+ * Does not require an API key
132
+ * [official doc](http://backpack.tf/api/users)
133
+
134
+ Get some basic information for a list of backpack.tf users. It's basically the
135
+ info that you'd see on their profile page.
136
+ You can request several users at once by sending an array.
137
+
138
+ #### IGetUserListings
139
+
140
+ * Get classified listings for a given user
141
+ * [official doc](http://backpack.tf/api/classifieds)
142
+
143
+ Request all classified listings for one user.
144
+ You must pass in the 64-bit `steamid`.
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ begin
2
+ require 'rspec/core/rake_task'
3
+ RSpec::Core::RakeTask.new(:spec)
4
+ task :default => :spec
5
+ rescue LoadError
6
+ puts 'no rspec available!'
7
+ end
data/backpack_tf.gemspec CHANGED
@@ -2,7 +2,7 @@ require_relative './lib/backpack_tf/version.rb'
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = 'backpack_tf'
5
- s.date = '2015-08-09'
5
+ s.date = '2016-02-06'
6
6
  s.summary = 'a wrapper for the backpack.tf API'
7
7
  s.author = 'Rafael Espinoza'
8
8
  s.email = 'rafael@rafaelespinoza.com'
@@ -13,7 +13,7 @@ Gem::Specification.new do |s|
13
13
  s.require_paths = ['lib']
14
14
  s.files = `git ls-files`.split("\n")
15
15
  s.version = BackpackTF::VERSION
16
- s.required_ruby_version = '>=1.9.3'
16
+ s.required_ruby_version = '>= 2.0.0'
17
17
 
18
18
  s.add_runtime_dependency 'httparty', '~>0.13.5'
19
19
 
@@ -1,118 +1,95 @@
1
1
  module BackpackTF
2
-
2
+ # Access all BackpackTF APIs through here
3
3
  class Client
4
-
5
4
  include HTTParty
6
-
7
- ###########################
8
- # Class Methods
9
- ###########################
10
5
 
11
- # store your API key as an environment variable
12
- # `export <env_var>='<your api key>'`
13
- @@env_var = 'BPTF_API_KEY'
14
- def self.env_var; @@env_var; end
15
-
16
- def self.api_key key = nil
17
- default = key || ENV[@@env_var]
18
-
19
- if default.nil?
20
- msg = "Assign your API key to an environment variable.\n"
21
- msg << "ex: `export #{@@env_var}=value`"
22
- raise KeyError, msg
23
- elsif default.class == String && (default.length != 24 || !!default[/\H/])
24
- msg = "The key should be a hexadecimal number, 24-digits long"
25
- raise ArgumentError, msg
26
- else
27
- default
28
- end
29
- end
30
-
31
- base_uri 'http://backpack.tf/api'
32
- default_timeout 5
33
- default_params(:key => api_key)
34
-
35
- def self.build_url_via action, query_options = {}
36
- case action
37
- when :get_prices, :prices, :price
38
- version = 4
39
- interface_url = "/#{Price.interface}/v#{version}/?"
40
- when :get_currencies, :currencies, :currency
41
- version = 1
42
- interface_url = "/#{Currency.interface}/v#{version}/?"
43
- when :get_special_items, :special_items, :special_item, :specialitem
44
- version = 1
45
- interface_url = "/#{SpecialItem.interface}/v#{version}/?"
46
- when :get_users, :users, :user
47
- version = 3
48
- interface_url = "/#{User.interface}/v#{version}/?"
49
- when :get_user_listings, :user_listings, :user_listing, :userlisting
50
- version = 1
51
- interface_url = "/#{UserListing.interface}/v#{version}/?"
52
- else
53
- raise ArgumentError, 'pass in valid action as a Symbol object'
54
- end
55
-
56
- base_uri + interface_url + extract_query_string(query_options)
6
+ # @param key [String] Your backpack.tf API key
7
+ # @return [Client] a new Client object
8
+ def initialize(key)
9
+ @key = check_key(key)
10
+ httparty_settings
57
11
  end
58
12
 
59
- def self.extract_query_string options = {}
60
- options.each_pair.map do |key, val|
61
- unless val.class == Array
62
- "#{key}=#{val}"
63
- else
64
- "#{key}=#{val.join(',')}"
65
- end
66
- end.join('&')
67
- end
68
-
69
- ###########################
70
- # Instance Methods
71
- ###########################
72
- attr_reader :db
73
-
74
- def initialize
75
- @db = nil
76
- end
77
-
78
- def fetch interface, query_options = {}
13
+ # @param interface [Symbol] API name to fetch, in singular, snake-case.
14
+ # For example:
15
+ # `:currencies`, `:market_prices`, `:prices`, `:price_history`,
16
+ # `:special_items`, `:users`, `:user_listings`
17
+ # @param query_options [Hash] Additional query string key-value pairs
18
+ # @return [Hash] Parsed JSON response from an API, starting from the
19
+ # 'response' key.
20
+ def fetch(interface, query_options = {})
79
21
  get_data(interface, query_options)['response']
80
22
  end
81
23
 
82
- def update class_to_update, data_to_update
83
- send_update_to_master_hash(class_to_update, data_to_update)
84
- refresh_class_hash(class_to_update)
85
- end
86
-
87
24
  private
88
-
89
- def send_update_to_master_hash class_to_update, data_to_update
90
- Response.responses( { class_to_update.to_sym => data_to_update } )
91
- end
92
-
93
- def refresh_class_hash class_to_update
94
- class_to_update.response
95
- end
96
25
 
97
- def get_data action, query_options = {}
26
+ def get_data(action, query_options = {})
98
27
  handle_timeouts do
99
- url = self.class.build_url_via(action, query_options)
28
+ url = build_url_via(action, query_options)
100
29
  self.class.get(url)
101
30
  end
102
31
  end
103
32
 
104
33
  # HTTParty raises an errors after time limit defined by ::default_timeout
105
- # * if it cannot connect to server, then it raises Net::OpenTimeout
106
- # * if it cannot read response from server, then it raises Net::ReadTimeout
107
- # if one of those happen, then an empty hash is returned
34
+ # - if it cannot connect to server, then it raises Net::OpenTimeout
35
+ # - if it cannot read response from server, then it raises Net::ReadTimeout
108
36
  def handle_timeouts
109
- begin
110
- yield
111
- rescue Net::OpenTimeout, Net::ReadTimeout
112
- {}
37
+ yield
38
+ rescue Net::OpenTimeout, Net::ReadTimeout
39
+ {}
40
+ end
41
+
42
+ def httparty_settings
43
+ self.class.base_uri('http://backpack.tf/api')
44
+ self.class.default_timeout(5)
45
+ self.class.default_params(key: @key)
46
+ end
47
+
48
+ def check_key(key = nil)
49
+ # This should not matter when running tests.
50
+ warn('--- WARNING: your key is nil ---') if key.nil?
51
+
52
+ if key.class == String && (key.length != 24 || key[/\H/])
53
+ msg = 'The key should be a hexadecimal number, 24-digits long'
54
+ fail ArgumentError, msg
55
+ else
56
+ key
113
57
  end
114
58
  end
115
59
 
116
- end
60
+ def build_url_via(action, query_options = {})
61
+ base_uri = self.class.base_uri
62
+ interface_url_fragment = select_interface_url_fragment(action)
63
+ base_uri + interface_url_fragment + extract_query_string(query_options)
64
+ end
65
+
66
+ def extract_query_string(options = {})
67
+ options.each_pair.map do |key, val|
68
+ if val.class == Array
69
+ "#{key}=#{val.join(',')}"
70
+ else
71
+ "#{key}=#{val}"
72
+ end
73
+ end.join('&')
74
+ end
117
75
 
76
+ def select_interface_url_fragment(action)
77
+ case action
78
+ when :currencies
79
+ Currency::Interface.url_name_and_version
80
+ when :market_prices
81
+ MarketPrice::Interface.url_name_and_version
82
+ when :prices
83
+ Price::Interface.url_name_and_version
84
+ when :price_history
85
+ PriceHistory::Interface.url_name_and_version
86
+ when :special_items
87
+ SpecialItem::Interface.url_name_and_version
88
+ when :users
89
+ User::Interface.url_name_and_version
90
+ when :user_listings
91
+ UserListing::Interface.url_name_and_version
92
+ end
93
+ end
94
+ end
118
95
  end
@@ -0,0 +1,9 @@
1
+ module BackpackTF
2
+ class Currency
3
+ # Access the IGetCurrencies interface
4
+ class Interface < BackpackTF::Interface
5
+ @name = :IGetCurrencies
6
+ @version = 1
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,24 @@
1
+ module BackpackTF
2
+ class Currency
3
+ # Process reponses from IGetCurrencies
4
+ class Response < BackpackTF::Response
5
+ @response = nil
6
+ @currencies = nil
7
+
8
+ def self.currencies
9
+ hash = @response['currencies'].each_with_object({}) do |(name, attr), h|
10
+ h[name] = BackpackTF::Currency.new(name, attr)
11
+ end
12
+ @currencies = hash
13
+ end
14
+
15
+ def self.name
16
+ @name = @response['name']
17
+ end
18
+
19
+ def self.url
20
+ @url = @response['url']
21
+ end
22
+ end
23
+ end
24
+ end
@@ -1,31 +1,10 @@
1
- module BackpackTF
2
-
3
- # ruby representations of a JSON response to
4
- # `IGetCurrencies`['response']
5
- class Currency < Response
6
-
7
- ###########################
8
- # Class Methods
9
- ###########################
10
-
11
- INTERFACE = :IGetCurrencies
12
- @interface = INTERFACE
13
- @response = nil
14
- @@currencies = nil
15
-
16
- def self.response
17
- @response = superclass.responses[to_sym]
18
- end
1
+ require 'backpack_tf/currency/interface'
2
+ require 'backpack_tf/currency/response'
19
3
 
20
- def self.currencies
21
- return @response if @response.nil?
22
- @@currencies = response[:currencies]
23
- hash_keys_to_sym(@@currencies)
24
- end
25
-
26
- ###########################
27
- # Instance Methods
28
- ###########################
4
+ module BackpackTF
5
+ # Ruby representations of a JSON response to IGetCurrencies
6
+ class Currency
7
+ include Helpers
29
8
 
30
9
  # @return [Fixnum] the quality index of the currency
31
10
  attr_reader :quality
@@ -35,35 +14,33 @@ module BackpackTF
35
14
  attr_reader :single
36
15
  # @return [String] the plural form of noun that is used in the suffix
37
16
  attr_reader :plural
38
- # @return [Fixnum] the number of decimal places the price should be rounded to
17
+ # @return [Fixnum] number of decimal places the price should be rounded to
39
18
  attr_reader :round
40
- # @return [Symbol] either :Craftable or :Non-Craftable to signify currency's craftability
19
+ # @return [Symbol] currency's craftability (:Craftable or :Non-Craftable)
41
20
  attr_reader :craftable
42
- # @return [Symbol] either :Tradable or :Non-Tradable to signify currency's tradability
21
+ # @return [Symbol] currency's tradability (:Tradable or :Non-Tradable)
43
22
  attr_reader :tradable
44
23
  # @return [Fixnum] the definition index of the currency
45
24
  attr_reader :defindex
46
- # TODO: what does the :blanket attribute mean?
47
- # it is set to 0 by default. However, it is set to 1 for :hat.
48
- # :hat also has an extra property & value :blanket_name => 'Random Craft Hat'
49
- # @return [Fixnum]
25
+ # @return [Fixnum] Not sure what this attribute means!
50
26
  attr_reader :blanket
51
27
 
52
- def initialize name, attr
53
- attr = check_attr_keys(attr)
28
+ # @param name [String] name of currency
29
+ # @param attr [Hash] attributes for Currency object
30
+ # @return [Currency] a new Currency object
31
+ def initialize(name, attr)
32
+ processed_attr = hash_keys_to_sym(attr)
54
33
 
55
34
  @name = name.to_s
56
- @quality = attr[:quality]
57
- @priceindex = attr[:priceindex]
58
- @single = attr[:single]
59
- @plural = attr[:plural]
60
- @round = attr[:round]
61
- @craftable = attr[:craftable].to_sym
62
- @tradable = attr[:tradable].to_sym
63
- @defindex = attr[:defindex]
64
- @blanket = attr[:blanket]
35
+ @quality = processed_attr[:quality]
36
+ @priceindex = processed_attr[:priceindex]
37
+ @single = processed_attr[:single]
38
+ @plural = processed_attr[:plural]
39
+ @round = processed_attr[:round]
40
+ @craftable = processed_attr[:craftable].to_sym
41
+ @tradable = processed_attr[:tradable].to_sym
42
+ @defindex = processed_attr[:defindex]
43
+ @blanket = processed_attr[:blanket]
65
44
  end
66
-
67
45
  end
68
-
69
46
  end
@@ -0,0 +1,29 @@
1
+ module BackpackTF
2
+ # Common methods for objects & response-based classes
3
+ module Helpers
4
+ def self.included(base)
5
+ base.extend(ClassMethods)
6
+ end
7
+
8
+ # Accessors/shortcuts for reaching the associated Response class from
9
+ # within a base namespace class.
10
+ module ClassMethods
11
+ def response=(new_data)
12
+ self::Response.response = new_data
13
+ end
14
+
15
+ def response
16
+ self::Response.response
17
+ end
18
+ end
19
+
20
+ private
21
+
22
+ def hash_keys_to_sym(hash)
23
+ hash.each_with_object({}) do |(key, val), new_hash|
24
+ key = key.to_sym if key.class == String
25
+ new_hash[key] = val
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,29 @@
1
+ module BackpackTF
2
+ # Base class for accessing a BackpackTF API
3
+ class Interface
4
+ class << self
5
+ attr_reader :format, :callback, :appid, :name, :version
6
+ end
7
+
8
+ @format = 'json'
9
+ @callback = nil
10
+ @appid = '440'
11
+
12
+ # @param options [Hash]
13
+ # @option options [String] :format The format desired. Defaults to 'json'.
14
+ # @option options [String] :callback JSONP format only, used for function
15
+ # call. Defaults to `nil`.
16
+ # @option options [String] :appid The ID of the game. Defaults to '440'.
17
+ # @return [Hash] default options
18
+ def self.defaults(options = {})
19
+ @format = options[:format] || 'json'
20
+ @callback = options[:callback] || nil
21
+ @appid = options[:appid] || '440'
22
+ end
23
+
24
+ # @return [String] URL fragment, ie: `/IGetPrices/v4/?`
25
+ def self.url_name_and_version
26
+ "/#{name}/v#{version}/?"
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,9 @@
1
+ module BackpackTF
2
+ class MarketPrice
3
+ # Access the IGetMarketPrices interface
4
+ class Interface < BackpackTF::Interface
5
+ @name = :IGetMarketPrices
6
+ @version = 1
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,15 @@
1
+ module BackpackTF
2
+ class MarketPrice
3
+ # Process reponses from IGetMarketPrices
4
+ class Response < BackpackTF::Response
5
+ @response = nil
6
+ @items = {}
7
+
8
+ def self.items
9
+ @items = @response['items'].each_with_object({}) do |(name, attr), acc|
10
+ acc[name] = BackpackTF::MarketPrice.new(name, attr)
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end