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
@@ -1,126 +1,42 @@
1
1
  require 'spec_helper'
2
2
 
3
- module BackpackTF
4
- describe Currency do
5
- let(:bp) { Client.new }
6
-
7
- let(:json_obj) {
8
- fixture = file_fixture('currencies.json')
9
- fixture = JSON.parse(fixture)['response']
10
- Response.hash_keys_to_sym(fixture)
11
- }
12
-
13
- let(:more_json) {
14
- fixture = file_fixture('currencies_updated.json')
15
- fixture = JSON.parse(fixture)['response']
16
- Response.hash_keys_to_sym(fixture)
17
- }
18
-
19
- it 'class has these default attributes' do
20
- expect(described_class.interface).to eq :IGetCurrencies
21
- end
22
-
23
- describe '::response' do
24
- before :all do
25
- expect(described_class.response).to be_nil
26
- end
27
-
28
- before :each do
29
- stub_http_response_with('currencies.json')
30
- fetched_currencies = bp.fetch(:currencies)
31
- bp.update(described_class, fetched_currencies)
32
- end
33
-
34
- after :all do
35
- Response.responses(:reset => :confirm)
36
- expect(Response.responses).to be_empty
37
- expect(described_class.response).to be_nil
38
- expect(described_class.currencies).to be_nil
39
- end
40
-
41
- it 'the keys of the response attribute should have these values' do
42
- response = described_class.response
43
- expect(response[:success]).to eq 1
44
- expect(response[:message]).to eq nil
45
- expect(response[:current_time]).to eq 1430784460
46
- expect(response[:name]).to eq 'Team Fortress 2'
47
- expect(response[:url]).to eq 'http://backpack.tf'
48
- end
49
- end
50
-
51
- describe '::currencies' do
52
- before :all do
53
- expect(described_class.response).to be_nil
54
- expect(described_class.currencies).to be_nil
55
- end
56
-
57
- before :each do
58
- Response.responses(:reset => :confirm)
59
- expect(Response.responses).to be_empty
60
-
61
- stub_http_response_with('currencies.json')
62
- fetched_currencies = bp.fetch(:currencies)
63
- bp.update(described_class, fetched_currencies)
64
- end
65
-
66
- after :all do
67
- Response.responses(:reset => :confirm)
68
- expect(Response.responses).to be_empty
69
- described_class.class_eval { @currencies = nil }
70
- end
71
-
72
- it 'returns the fixture and sets to @@currencies variable' do
73
- expect(described_class.currencies).not_to be_nil
74
- end
75
-
76
- it '@@currencies attribute should be a Hash object' do
77
- expect(described_class.currencies).to be_instance_of Hash
78
- end
79
-
80
- it '@@currencies should have these keys' do
81
- expected_keys = [:metal, :keys, :earbuds, :hat]
82
- expect(described_class.currencies.keys).to match_array expected_keys
83
- end
84
- end
85
-
86
- describe '#initialize' do
87
-
88
- before :all do
89
- expect(described_class.response).to be_nil
90
- expect(described_class.currencies).to be_nil
91
- end
92
-
93
- before :each do
94
- bp.update(described_class, json_obj)
95
- expect(described_class.currencies).not_to be_nil
96
- end
97
-
98
- after :all do
99
- Response.responses(:reset => :confirm)
100
- expect(Response.responses).to be_empty
101
- described_class.class_eval { @currencies = nil }
102
- end
3
+ describe BackpackTF::Currency do
4
+ let(:currencies) do
5
+ fixture = file_fixture('currencies_updated.json')
6
+ JSON.parse(fixture)['response']
7
+ end
103
8
 
104
- subject {
105
- described_class.new(:metal, described_class.currencies[:metal])
9
+ describe '#initialize' do
10
+ it 'instance should have these values' do
11
+ processed_currencies = {
12
+ quality: 6,
13
+ priceindex: 0,
14
+ single: 'ref',
15
+ plural: 'ref',
16
+ round: '2',
17
+ craftable: 'Craftable',
18
+ tradable: 'Tradable',
19
+ defindex: 5002,
20
+ blanket: 0
106
21
  }
107
22
 
108
- it 'instance should respond to these methods' do
109
- expect(subject).to respond_to(:quality, :priceindex, :single, :plural, :round, :craftable, :tradable, :defindex, :blanket)
110
- end
111
-
112
- it 'instance should have these values' do
113
- expect(subject.quality).to eq 6
114
- expect(subject.priceindex).to eq 0
115
- expect(subject.single).to eq 'ref'
116
- expect(subject.plural).to eq 'ref'
117
- expect(subject.round).to eq 2
118
- expect(subject.craftable).to eq :Craftable
119
- expect(subject.tradable).to eq :Tradable
120
- expect(subject.defindex).to eq 5002
121
- expect(subject.blanket).to eq 0
122
- end
23
+ allow_any_instance_of(described_class)
24
+ .to receive(:hash_keys_to_sym)
25
+ .and_return(processed_currencies)
26
+
27
+ subject = described_class.new(:metal, currencies['metal'])
28
+
29
+ expect(subject).to have_attributes(
30
+ quality: 6,
31
+ priceindex: 0,
32
+ single: 'ref',
33
+ plural: 'ref',
34
+ round: '2',
35
+ craftable: :Craftable,
36
+ tradable: :Tradable,
37
+ defindex: 5002,
38
+ blanket: 0
39
+ )
123
40
  end
124
-
125
41
  end
126
42
  end
@@ -0,0 +1,85 @@
1
+ require 'spec_helper'
2
+
3
+ describe BackpackTF::Helpers do
4
+ module BackpackTF
5
+ class DummyClass # :nodoc:
6
+ include BackpackTF::Helpers
7
+
8
+ class Response < BackpackTF::Response # :nodoc:
9
+ self.response = { foo: 'bar' }
10
+ end
11
+ end
12
+ end
13
+
14
+ let(:dummy) { BackpackTF::DummyClass.new }
15
+
16
+ # TODO: these tests can be written more succinctly by expecting messages to
17
+ # be called or to have been called on the DummyClass::Response class.
18
+ describe '::response' do
19
+ context 'demonstrating how it normally works' do
20
+ context 'reading' do
21
+ it 'the inner Response class has this value for .response' do
22
+ expected = { foo: 'bar' }
23
+ expect(BackpackTF::DummyClass::Response.response).to eq(expected)
24
+ end
25
+ end
26
+
27
+ context 'writing' do
28
+ before(:each) do
29
+ BackpackTF::DummyClass::Response.response = { bar: 'foo' }
30
+ end
31
+ after(:each) do
32
+ BackpackTF::DummyClass::Response.class_eval do
33
+ self.response = { foo: 'bar' }
34
+ end
35
+ end
36
+ it 'can write .response of its own Response class (compare by value)' do
37
+ expected = { bar: 'foo' }
38
+ expect(BackpackTF::DummyClass::Response.response).to eq(expected)
39
+ end
40
+ end
41
+ end
42
+
43
+ context 'demonstrating how it works with the shortcut' do
44
+ context 'reading' do
45
+ it 'can read .response of its own Response class' do
46
+ expected = BackpackTF::DummyClass::Response.response
47
+ expect(BackpackTF::DummyClass.response).to eq(expected)
48
+ end
49
+ end
50
+
51
+ context 'writing' do
52
+ before(:each) do
53
+ BackpackTF::DummyClass.response = { bar: 'foo' }
54
+ end
55
+ after(:each) do
56
+ BackpackTF::DummyClass.class_eval { self.response = { foo: 'bar' } }
57
+ end
58
+
59
+ it 'can write to its own Response class (compare by value)' do
60
+ expected = { bar: 'foo' }
61
+ expect(BackpackTF::DummyClass.response).to eq(expected)
62
+ end
63
+ it 'can write to its own Response class (compare by reference)' do
64
+ expected = BackpackTF::DummyClass::Response.response
65
+ expect(BackpackTF::DummyClass.response).to eq(expected)
66
+ end
67
+ end
68
+ end
69
+ end
70
+
71
+ describe '#hash_keys_to_sym' do
72
+ it 'changes each key from a String to a Symbol' do
73
+ metal = {
74
+ 'quality' => 6,
75
+ 'single' => 'ref'
76
+ }
77
+ hashed_metal = {
78
+ quality: 6,
79
+ single: 'ref'
80
+ }
81
+ actual = dummy.send(:hash_keys_to_sym, metal)
82
+ expect(actual).to eq hashed_metal
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,56 @@
1
+ require 'spec_helper'
2
+
3
+ describe BackpackTF::Interface do
4
+ describe '::defaults' do
5
+ after(:each) do
6
+ described_class.class_eval do
7
+ @format = 'json'
8
+ @callback = nil
9
+ @appid = '440'
10
+ end
11
+ end
12
+
13
+ it 'has these attributes & values by default' do
14
+ expect(described_class).to have_attributes(
15
+ format: 'json',
16
+ callback: nil,
17
+ appid: '440'
18
+ )
19
+ end
20
+ it 'can modify its values' do
21
+ options = {
22
+ format: 'foo',
23
+ callback: 'bar',
24
+ appid: '2'
25
+ }
26
+ described_class.defaults(options)
27
+ expect(described_class).to have_attributes(
28
+ format: 'foo',
29
+ callback: 'bar',
30
+ appid: '2'
31
+ )
32
+ end
33
+ end
34
+
35
+ describe '::url_name_and_version' do
36
+ before(:each) do
37
+ allow(described_class).to receive(:name).and_return('interface_name')
38
+ allow(described_class).to receive(:version).and_return(1)
39
+ end
40
+ after(:each) do
41
+ allow(described_class).to receive(:name).and_return(nil)
42
+ allow(described_class).to receive(:version).and_return(nil)
43
+ end
44
+
45
+ it 'calls .name and .version' do
46
+ expect(described_class).to receive(:name)
47
+ expect(described_class).to receive(:version)
48
+ described_class.url_name_and_version
49
+ end
50
+ it 'returns part of a url' do
51
+ actual = described_class.url_name_and_version
52
+ expected = '/interface_name/v1/?'
53
+ expect(actual).to eq expected
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,8 @@
1
+ require 'spec_helper'
2
+
3
+ describe BackpackTF::MarketPrice::Interface do
4
+ it 'has these attributes' do
5
+ expect(described_class.name).to eq :IGetMarketPrices
6
+ expect(described_class.version).to eq 1
7
+ end
8
+ end
@@ -0,0 +1,22 @@
1
+ require 'spec_helper'
2
+
3
+ describe BackpackTF::MarketPrice::Response do
4
+ let(:json_response) do
5
+ fixture = file_fixture('market_prices.json')
6
+ JSON.parse(fixture)['response']
7
+ end
8
+
9
+ describe '::items' do
10
+ before(:each) do
11
+ described_class.response = json_response
12
+ end
13
+ after(:each) do
14
+ described_class.response = nil
15
+ end
16
+
17
+ it 'each key points to a BackpackTF::MarketPrice object' do
18
+ items = described_class.items
19
+ expect(items.values).to all be_a BackpackTF::MarketPrice
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,22 @@
1
+ require 'spec_helper'
2
+
3
+ describe BackpackTF::MarketPrice do
4
+ let(:response_attr) do
5
+ {
6
+ 'last_updated' => 1_453_654_816,
7
+ 'quantity' => 52,
8
+ 'value' => 89
9
+ }
10
+ end
11
+
12
+ describe '#initialize' do
13
+ it 'has these attributes' do
14
+ actual = described_class.new('A Color Similar to Slate', response_attr)
15
+ expect(actual).to have_attributes(
16
+ last_updated: 1_453_654_816,
17
+ quantity: 52,
18
+ value: 89
19
+ )
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,27 @@
1
+ require 'spec_helper'
2
+
3
+ describe BackpackTF::Price::Interface do
4
+ it 'has these default attributes' do
5
+ expect(described_class.name).to eq :IGetPrices
6
+ expect(described_class.version).to eq 4
7
+ expect(described_class.raw).to eq nil
8
+ expect(described_class.since).to eq nil
9
+ end
10
+ describe '::defaults' do
11
+ after(:each) do
12
+ described_class.class_eval do
13
+ @raw = nil
14
+ @since = nil
15
+ end
16
+ end
17
+ it 'can modify its values' do
18
+ options = {
19
+ raw: 2,
20
+ since: '1451606400'
21
+ }
22
+ described_class.defaults(options)
23
+ expect(described_class.raw).to eq 2
24
+ expect(described_class.since).to eq '1451606400'
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,101 @@
1
+ require 'spec_helper'
2
+
3
+ describe BackpackTF::Price::ItemPrice do
4
+ let(:item_price) do
5
+ key = 'Unique_Tradable_Craftable'
6
+ described_class.new(key, {})
7
+ end
8
+
9
+ describe '::quality_name_to_index' do
10
+ before(:each) do
11
+ allow(described_class).to receive(:qualities).and_return([:foo, :bar])
12
+ end
13
+
14
+ it 'returns index of the quality' do
15
+ actual = described_class.quality_name_to_index(:bar)
16
+ expect(actual).to eq 1
17
+ end
18
+ end
19
+
20
+ describe '#split_key' do
21
+ it 'splits the string by the underscore' do
22
+ actual = item_price.send(:split_key, 'foo_bar_quux')
23
+ expected = %w(foo bar quux)
24
+ expect(actual).to eq expected
25
+ end
26
+ end
27
+
28
+ describe '#currency_or_nil' do
29
+ it 'returns nil when key is not present' do
30
+ attr = { 'foo' => 'bar' }
31
+ actual = item_price.send(:currency_or_nil, attr)
32
+ expect(actual).to be_nil
33
+ end
34
+
35
+ it 'otherwise returns values as a symbol' do
36
+ attr = { 'currency' => 'foo' }
37
+ actual = item_price.send(:currency_or_nil, attr)
38
+ expect(actual).to eq :foo
39
+ end
40
+ end
41
+
42
+ describe '#pick_particle_effect' do
43
+ it 'calls ParticleEffect.list' do
44
+ expect(BackpackTF::Price::ParticleEffect)
45
+ .to receive(:list)
46
+ .and_return([:foo, :bar])
47
+ .at_most(:twice)
48
+ item_price.send(:pick_particle_effect, '0')
49
+ end
50
+ end
51
+
52
+ describe 'A price for a typical item' do
53
+ item_price_attr = JSON.parse(file_fixture('item_price_typical.json'))
54
+
55
+ subject do
56
+ described_class.new('Unique_Tradable_Craftable', item_price_attr)
57
+ end
58
+
59
+ context 'item-specific tests' do
60
+ it 'should have these values' do
61
+ expect(subject.quality).to eq :Unique
62
+ expect(subject.tradability).to eq :Tradable
63
+ expect(subject.craftability).to eq :Craftable
64
+ expect(subject.currency).to eq :metal
65
+ expect(subject.value).to eq 0.05
66
+ expect(subject.value_high).to be_nil
67
+ expect(subject.value_raw).to be_nil
68
+ expect(subject.value_high_raw).to be_nil
69
+ expect(subject.last_update).to eq 1_336_410_088
70
+ expect(subject.difference).to eq 0
71
+ expect(subject.priceindex).to be_nil
72
+ expect(subject.effect).to be_nil
73
+ end
74
+ end
75
+ end
76
+
77
+ describe "A price for an item with 'Unusual' quality" do
78
+ item_price_attr = JSON.parse(file_fixture('item_price_unusual.json'))
79
+
80
+ subject do
81
+ described_class.new('Unusual_Tradable_Craftable', item_price_attr, 6)
82
+ end
83
+
84
+ context 'item-specific tests' do
85
+ it 'should have these values' do
86
+ expect(subject.quality).to eq :Unusual
87
+ expect(subject.tradability).to eq :Tradable
88
+ expect(subject.craftability).to eq :Craftable
89
+ expect(subject.currency).to eq :keys
90
+ expect(subject.value).to eq 18
91
+ expect(subject.value_high).to eq 22
92
+ expect(subject.value_raw).to be_nil
93
+ expect(subject.value_high_raw).to be_nil
94
+ expect(subject.last_update).to eq 1_418_795_322
95
+ expect(subject.difference).to eq 280
96
+ expect(subject.priceindex).to eq 6
97
+ expect(subject.effect).to eq 'Green Confetti'
98
+ end
99
+ end
100
+ end
101
+ end