backpack_tf 0.2.1 → 0.5.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.
@@ -2,55 +2,126 @@ require 'spec_helper'
2
2
 
3
3
  module BackpackTF
4
4
  describe 'Prices' do
5
- describe 'Prices class' do
6
5
 
7
- it 'has these default attributes' do
8
- expect(Prices.interface).to eq :IGetPrices
6
+ let(:bp) { Client.new }
7
+
8
+ let(:json_obj) {
9
+ fixture = file_fixture('prices.json')
10
+ fixture = JSON.parse(fixture)['response']
11
+ Response.hash_keys_to_sym(fixture)
12
+ }
13
+
14
+ it 'responds to these methods' do
15
+ expect(Prices).to respond_to :responses, :response, :items, :interface, :hash_keys_to_sym
16
+ end
17
+
18
+ it 'has these default attributes' do
19
+ expect(Prices.interface).to eq :IGetPrices
20
+ end
21
+
22
+ describe '::responses' do
23
+
24
+ before :each do
25
+ stub_http_response_with('prices.json')
26
+ opts = { :app_id => 440, :compress => 1 }
27
+ fetched_prices = bp.fetch(:prices, opts)
28
+ Response.responses(Prices.to_sym => fetched_prices)
9
29
  end
10
30
 
11
- describe 'accessing the IGetPrices interface' do
31
+ context 'access from Response class' do
32
+ it "Prices can be accessed by calling the key, Prices" do
33
+ expect(Response.responses[Prices.to_sym]).to eq json_obj
34
+ end
35
+ end
12
36
 
13
- context 'a successful response' do
14
- it 'returns the fixture and sets to @@items variable' do
15
- stub_http_response_with('prices.json')
16
- opts = { :app_id => 440, :compress => 1 }
17
- Prices.fetch(Client.new.get_data(:get_prices, opts)['response'])
18
- expect(Prices.items).not_to be_nil
19
- end
37
+ context "access from Prices class" do
38
+ it 'can access response information via the class method, ::response' do
39
+ expect(Prices.response).to eq json_obj
40
+ end
41
+ end
20
42
 
21
- it 'the response attribute should have these keys' do
22
- expect(Prices.response.keys).to match_array [:success, :current_time, :raw_usd_value, :usd_currency, :usd_currency_index, :items]
23
- end
43
+ it "is the same as calling Prices.response" do
44
+ expect(Response.responses[Prices.to_sym]).to eq Prices.response
45
+ end
46
+ end
24
47
 
25
- it 'the keys of the response attribute should have these values' do
26
- expect(Prices.response[:success]).to eq 1
27
- expect(Prices.response[:message]).to eq nil
28
- expect(Prices.response[:current_time]).to eq 1430785805
29
- expect(Prices.response[:raw_usd_value]).to eq 0.115
30
- expect(Prices.response[:usd_currency]).to eq 'metal'
31
- expect(Prices.response[:usd_currency_index]).to eq 5002
32
- end
48
+ describe '::response' do
33
49
 
34
- it '@@items attribute should be a Hash object' do
35
- expect(Prices.items).to be_instance_of Hash
36
- end
50
+ before :each do
51
+ stub_http_response_with('prices.json')
52
+ opts = { :app_id => 440, :compress => 1 }
53
+ fetched_prices = bp.fetch(:prices, opts)
54
+ Response.responses(Prices.to_sym => fetched_prices)
55
+ end
37
56
 
38
- it '@@items should have this many keys' do
39
- expect(Prices.items.keys.length).to be 1661
40
- end
57
+ it 'the response attribute should have these keys' do
58
+ expect(Prices.response.keys).to match_array [:success, :current_time, :raw_usd_value, :usd_currency, :usd_currency_index, :items]
59
+ end
41
60
 
42
- it '@@items should have these keys' do
43
- expect(Prices.items).to have_key('Kritzkrieg')
44
- end
61
+ it 'the keys of the response attribute should have these values' do
62
+ expect(Prices.response[:success]).to eq 1
63
+ expect(Prices.response[:message]).to eq nil
64
+ expect(Prices.response[:current_time]).to eq 1430785805
65
+ expect(Prices.response[:raw_usd_value]).to eq 0.115
66
+ expect(Prices.response[:usd_currency]).to eq 'metal'
67
+ expect(Prices.response[:usd_currency_index]).to eq 5002
68
+ end
45
69
 
70
+ end
71
+
72
+ describe '::items' do
73
+
74
+ before :each do
75
+ Response.responses(:reset => :confirm)
76
+ expect(Response.responses).to be_empty
77
+
78
+ stub_http_response_with('prices.json')
79
+ opts = { :app_id => 440, :compress => 1 }
80
+ fetched_prices = bp.fetch(:prices, opts)
81
+ Response.responses(Prices => fetched_prices)
82
+ end
83
+
84
+ it 'returns the fixture and sets to @@items variable' do
85
+ expect(Prices.items).not_to be_nil
86
+ end
87
+
88
+ context '@@items attribute' do
89
+ it 'should be a Hash object' do
90
+ expect(Prices.items).to be_instance_of Hash
91
+ end
92
+ xit 'should have this many keys' do
93
+ expect(Prices.items.keys.length).to be 1661
46
94
  end
95
+ it 'each key should be a String' do
96
+ expect(Prices.items.keys).to all be_a String
97
+ end
98
+ it 'each value should be an Item' do
99
+ expect(Prices.items.values).to all be_an Item
100
+ end
101
+ end
102
+
103
+ context 'using keys to generate Item objects' do
104
+ let(:random_key) { Prices.items.keys.sample }
105
+ let(:item) { Prices.items[random_key] }
47
106
 
107
+ it 'generates an Item object' do
108
+ expect(item).to be_instance_of Item
109
+ end
110
+ it 'the Item object responds to these methods' do
111
+ expect(item).to respond_to :item_name, :defindex, :prices
112
+ end
113
+ it 'uses the key to assign a value to the @item_name property of the Item' do
114
+ expect(random_key).to eq item.item_name
115
+ end
116
+ it 'passes itself to be stored in the @prices attribute of the Item object' do
117
+ expect(Prices.items[random_key].prices).to eq item.prices
118
+ end
48
119
  end
49
120
 
50
121
  end
51
122
 
52
123
  describe 'instances of Prices' do
53
- it 'for now, there are not meant to be instances of this class' do
124
+ it 'raises an error when trying to instantiate this class' do
54
125
  expect{Prices.new}.to raise_error RuntimeError
55
126
  end
56
127
  end
@@ -2,35 +2,110 @@ require 'spec_helper'
2
2
 
3
3
  module BackpackTF
4
4
 
5
- class Dummy
6
- include BackpackTF::Response
7
- end
5
+ describe Response do
6
+ let (:bp) { Client.new }
7
+
8
+ let(:json_obj) {
9
+ fixture = file_fixture('currencies.json')
10
+ fixture = JSON.parse(fixture)['response']
11
+ Response.hash_keys_to_sym(fixture)
12
+ }
8
13
 
9
- shared_examples 'Response' do
10
- let(:dummy) { Dummy.new }
14
+ let(:more_json) {
15
+ fixture = file_fixture('currencies_updated.json')
16
+ fixture = JSON.parse(fixture)['response']
17
+ Response.hash_keys_to_sym(fixture)
18
+ }
11
19
 
12
- context 'the class' do
13
- it 'responds to these inherited class methods' do
14
- expect(Dummy).to respond_to(:interface, :fetch, :response, :hash_keys_to_sym)
20
+ describe '::interface' do
21
+ it 'should be nil' do
22
+ expect(Response.interface).to be_nil
15
23
  end
24
+ end
16
25
 
17
- describe '::hash_keys_to_sym' do
18
- it 'changes the type of each key from String to Symbol' do
19
- metal = {'quality'=>6, 'priceindex'=>0, 'single'=>'ref', 'plural'=>'ref', 'round'=>2, 'blanket'=>0, 'craftable'=>'Craftable', 'tradable'=>'Tradable', 'defindex'=>5002}
20
- hashed_metal = {:quality=>6, :priceindex=>0, :single=>'ref', :plural=>'ref', :round=>2, :blanket=>0, :craftable=>'Craftable', :tradable=>'Tradable', :defindex=>5002}
21
- expect(Dummy.hash_keys_to_sym(metal)).to eq hashed_metal
22
- end
26
+ describe '::to_sym' do
27
+ it 'should return the name of the class, as a Symbol object' do
28
+ expect(Response.to_sym).to eq :'BackpackTF::Response'
23
29
  end
24
30
  end
25
31
 
26
- end
32
+ describe '::responses' do
33
+ let (:faked_class_sym) { :'BackpackTF::FakedClass' }
27
34
 
28
- describe Prices do
29
- it_behaves_like 'Response'
30
- end
35
+ it 'returns a Hash object' do
36
+ expect(Response.responses).to be_instance_of Hash
37
+ end
38
+
39
+ context 'expected input' do
40
+ it 'has these keys' do
41
+ stub_http_response_with('currencies.json')
42
+ expect(bp.fetch(:currencies).keys).to eq ['success', 'currencies', 'name', 'url', 'current_time']
43
+ end
44
+ end
45
+
46
+ context 'reset' do
47
+ before :each do
48
+ Response.responses(faked_class_sym => json_obj)
49
+ expect(Response.responses[faked_class_sym]).to eq json_obj
50
+ end
51
+ it 'can be emptied by passing in `:reset => :confirm`' do
52
+ Response.responses(:reset => :confirm)
53
+ expect(Response.responses).to be_empty
54
+ end
55
+ end
56
+
57
+ context 'reading' do
58
+
59
+ before :each do
60
+ Response.responses(:reset => :confirm)
61
+ expect(Response.responses).to be_empty
62
+ end
63
+
64
+ it 'returns the value of the key' do
65
+ res = { faked_class_sym => json_obj }
66
+ Response.responses(res)
67
+ expect(Response.responses[faked_class_sym]).to eq json_obj
68
+ end
69
+ it 'returns nil when key has no value' do
70
+ Response.responses(:foo)
71
+ expect(Response.responses[:foo]).to be_nil
72
+ end
73
+ end
74
+
75
+ context 'updating' do
76
+
77
+ before :each do
78
+ Response.responses(:reset => :confirm)
79
+ expect(Response.responses).to be_empty
80
+ end
81
+
82
+ it "updates a key's value when the key already exists" do
83
+ entry1 = { faked_class_sym => json_obj }
84
+ Response.responses(entry1)
85
+ expect(Response.responses[faked_class_sym]).to eq json_obj
86
+ entry2 = { faked_class_sym => more_json }
87
+ Response.responses(entry2)
88
+ expect(Response.responses[faked_class_sym]).to eq more_json
89
+ end
90
+ it 'creates a new key & value if key does not exist' do
91
+ res = { faked_class_sym => json_obj }
92
+ expect(Response.responses(res)[faked_class_sym]).to eq json_obj
93
+ end
94
+ it 'is not nil when you reference entire hash object' do
95
+ Response.responses( { faked_class_sym => json_obj } )
96
+ expect(Response.responses).not_to be_nil
97
+ end
98
+ end
99
+ end
100
+
101
+ describe '::hash_keys_to_sym' do
102
+ it 'changes the type of each key from String to Symbol' do
103
+ metal = {'quality'=>6, 'priceindex'=>0, 'single'=>'ref', 'plural'=>'ref', 'round'=>2, 'blanket'=>0, 'craftable'=>'Craftable', 'tradable'=>'Tradable', 'defindex'=>5002}
104
+ hashed_metal = {:quality=>6, :priceindex=>0, :single=>'ref', :plural=>'ref', :round=>2, :blanket=>0, :craftable=>'Craftable', :tradable=>'Tradable', :defindex=>5002}
105
+ expect(Response.hash_keys_to_sym(metal)).to eq hashed_metal
106
+ end
107
+ end
31
108
 
32
- describe Currencies do
33
- it_behaves_like 'Response'
34
109
  end
35
110
 
36
111
  end
@@ -0,0 +1 @@
1
+ {"response":{"success":1,"currencies":{"metal":{"quality":6,"priceindex":0,"single":"ref","plural":"ref","round":2,"blanket":0,"craftable":"Craftable","tradable":"Tradable","defindex":5002},"hat":{"quality":6,"priceindex":0,"single":"hat","plural":"hats","round":1,"blanket":1,"blanket_name":"Random Craft Hat","craftable":"Craftable","tradable":"Tradable","defindex":-2},"keys":{"quality":6,"priceindex":0,"single":"key","plural":"keys","round":2,"blanket":0,"craftable":"Craftable","tradable":"Tradable","defindex":5021},"earbuds":{"quality":6,"priceindex":0,"single":"bud","plural":"buds","round":2,"blanket":0,"craftable":"Craftable","tradable":"Tradable","defindex":143}},"name":"Team Fortress 2","url":"http:\/\/backpack.tf","current_time":1430988840}}
File without changes
@@ -0,0 +1 @@
1
+ {"defindex":[988],"prices":{"5":{"Tradable":{"Craftable":{"6":{"currency":"keys","value":18,"last_update":1418795322,"difference":280,"value_high":22},"7":{"currency":"keys","value":15,"last_update":1427240637,"difference":-16.9385},"8":{"currency":"keys","value":67,"last_update":1412688562,"difference":580.22},"9":{"currency":"keys","value":38,"last_update":1426592869,"difference":-188.815,"value_high":40},"10":{"currency":"keys","value":54,"last_update":1430651464,"difference":-73.98,"value_high":56},"11":{"currency":"keys","value":26,"last_update":1430514572,"difference":-25.739130434783},"13":{"currency":"keys","difference":1501.5,"last_update":1389942856,"value":214,"value_high":215},"14":{"currency":"keys","value":203,"last_update":1405544923,"difference":443.14066326531},"19":{"currency":"keys","value":70,"last_update":1412655977,"difference":609.231},"29":{"currency":"keys","value":18,"last_update":1424595094,"difference":-82.782,"value_high":23},"30":{"currency":"keys","value":14,"last_update":1426634788,"difference":42.9125,"value_high":19},"31":{"item_name":"Barnstormer","quality":5,"craftable":"Craftable","tradable":"Tradable","priceindex":31,"defindex":[988],"value":23,"currency":"keys","value_high":26,"value_old":{"currency":"usd","value":74.4,"value_high":89.28,"last_update":1380366823,"difference":-23.34},"last_update":1401407345,"difference":-135.48506632653},"32":{"currency":"keys","value":12,"last_update":1412860765,"difference":-21.65,"value_high":16},"33":{"currency":"keys","value":19,"last_update":1426560513,"difference":-104.18575,"value_high":24},"34":{"currency":"keys","value":18,"last_update":1406904862,"difference":-9.6526530612245},"35":{"currency":"keys","value":15,"last_update":1426592774,"difference":-162.40333333333,"value_high":19},"36":{"currency":"keys","value":15,"last_update":1420313482,"difference":-28,"value_high":16},"56":{"currency":"keys","value":32,"last_update":1404952347,"difference":-134.91653061224,"value_high":36},"57":{"currency":"keys","difference":-59.636,"last_update":1400197661,"value":21,"value_high":22},"58":{"currency":"keys","value":14,"last_update":1426723046,"difference":-8.5825,"value_high":20},"59":{"currency":"keys","value":19,"last_update":1408972311,"difference":140.315},"60":{"currency":"keys","value":15,"last_update":1430018292,"difference":280.5,"value_high":18},"61":{"currency":"keys","value":15,"last_update":1426593954,"difference":-17.165,"value_high":19},"62":{"currency":"keys","value":13,"last_update":1426592741,"difference":-145.9025,"value_high":18},"63":{"currency":"keys","value":30,"last_update":1421524482,"difference":434.445,"value_high":36},"64":{"currency":"keys","value":36,"last_update":1423376185,"difference":576.06875,"value_high":40},"65":{"currency":"keys","value":17,"last_update":1411656644,"difference":29.257,"value_high":24},"66":{"currency":"usd","value":51.98,"value_high":76.45,"last_update":1384529093,"difference":-99.15},"67":{"currency":"usd","value":79.32,"value_high":95.18,"last_update":1383575955,"difference":-97.776},"68":{"currency":"keys","value":43,"last_update":1406475390,"difference":398.259,"value_high":54},"69":{"currency":"usd","value":232.39,"last_update":1369898821,"difference":0},"70":{"currency":"keys","value":27,"last_update":1418969616,"difference":446.25,"value_high":37},"71":{"currency":"keys","value":60,"last_update":1426512316,"difference":-685.5},"72":{"currency":"keys","value":47,"last_update":1427566642,"difference":-8.6283749999999,"value_high":57},"89":{"currency":"keys","value":32,"last_update":1423834006,"difference":543.96,"value_high":40},"90":{"currency":"keys","value":51,"last_update":1426240269,"difference":878.9}}}},"6":{"Tradable":{"Craftable":[{"currency":"metal","value":1.66,"last_update":1429795315,"difference":0.055,"value_high":1.77}]}}}}
@@ -0,0 +1 @@
1
+ {"defindex":[5650],"prices":{"6":{"Tradable":{"Craftable":{"66":{"currency":"metal","value":0.05,"last_update":1426433111,"difference":0.04}}}}}}
@@ -0,0 +1 @@
1
+ {"defindex":[],"prices":{"6":{"Tradable":{"Craftable":[{"currency":"metal","value":0.66,"last_update":1393622359,"difference":-0.17}]}}}}
data/spec/spec_helper.rb CHANGED
@@ -11,7 +11,8 @@ end
11
11
  # taken from httparty's spec dir
12
12
  # https://github.com/jnunemaker/httparty/blob/master/spec/spec_helper.rb
13
13
  def file_fixture(filename)
14
- File.open( File.join( File.dirname(__FILE__), "fixtures", "#{filename}")).read
14
+ filename = File.join( File.dirname(__FILE__), "fixtures", "#{filename}")
15
+ File.open(filename).read
15
16
  end
16
17
 
17
18
  # taken from httparty's spec dir
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: backpack_tf
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rafael Espinoza
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-05-01 00:00:00.000000000 Z
11
+ date: 2015-05-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty
@@ -70,7 +70,7 @@ dependencies:
70
70
  - - ">="
71
71
  - !ruby/object:Gem::Version
72
72
  version: 1.21.0
73
- description: a wrapper for the backpack.tf API
73
+ description: API client, accessor methods for backpack.tf
74
74
  email: rafael@rafaelespinoza.com
75
75
  executables: []
76
76
  extensions: []
@@ -78,24 +78,31 @@ extra_rdoc_files: []
78
78
  files:
79
79
  - ".gitignore"
80
80
  - Gemfile
81
- - Gemfile.lock
82
81
  - TODO.md
83
82
  - backpack_tf.gemspec
84
83
  - lib/backpack_tf.rb
85
84
  - lib/backpack_tf/client.rb
86
85
  - lib/backpack_tf/currencies.rb
86
+ - lib/backpack_tf/finder.rb
87
87
  - lib/backpack_tf/item.rb
88
88
  - lib/backpack_tf/item_price.rb
89
89
  - lib/backpack_tf/prices.rb
90
90
  - lib/backpack_tf/response.rb
91
+ - lib/backpack_tf/version.rb
91
92
  - readme.md
92
93
  - spec/backpack_tf/client_spec.rb
93
94
  - spec/backpack_tf/currencies_spec.rb
95
+ - spec/backpack_tf/finder_spec.rb
96
+ - spec/backpack_tf/item_price_spec.rb
94
97
  - spec/backpack_tf/item_spec.rb
95
98
  - spec/backpack_tf/prices_spec.rb
96
99
  - spec/backpack_tf/response_spec.rb
97
100
  - spec/fixtures/currencies.json
98
- - spec/fixtures/item.json
101
+ - spec/fixtures/currencies_updated.json
102
+ - spec/fixtures/item_typical.json
103
+ - spec/fixtures/item_unusual.json
104
+ - spec/fixtures/item_with_unconventional_structure.json
105
+ - spec/fixtures/item_without_defindex.json
99
106
  - spec/fixtures/prices.json
100
107
  - spec/spec_helper.rb
101
108
  homepage: https://github.com/NerdDiffer/backpack_tf
@@ -110,7 +117,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
110
117
  requirements:
111
118
  - - ">="
112
119
  - !ruby/object:Gem::Version
113
- version: '0'
120
+ version: 1.9.3
114
121
  required_rubygems_version: !ruby/object:Gem::Requirement
115
122
  requirements:
116
123
  - - ">="
@@ -118,9 +125,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
118
125
  version: '0'
119
126
  requirements: []
120
127
  rubyforge_project:
121
- rubygems_version: 2.2.2
128
+ rubygems_version: 2.4.5
122
129
  signing_key:
123
130
  specification_version: 4
124
131
  summary: a wrapper for the backpack.tf API
125
132
  test_files: []
126
- has_rdoc:
data/Gemfile.lock DELETED
@@ -1,41 +0,0 @@
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
- httparty (0.13.3)
9
- json (~> 1.8)
10
- multi_xml (>= 0.5.2)
11
- json (1.8.2)
12
- multi_xml (0.5.5)
13
- redis (3.2.1)
14
- rspec (3.2.0)
15
- rspec-core (~> 3.2.0)
16
- rspec-expectations (~> 3.2.0)
17
- rspec-mocks (~> 3.2.0)
18
- rspec-core (3.2.3)
19
- rspec-support (~> 3.2.0)
20
- rspec-expectations (3.2.1)
21
- diff-lcs (>= 1.2.0, < 2.0)
22
- rspec-support (~> 3.2.0)
23
- rspec-mocks (3.2.1)
24
- diff-lcs (>= 1.2.0, < 2.0)
25
- rspec-support (~> 3.2.0)
26
- rspec-support (3.2.2)
27
- safe_yaml (1.0.4)
28
- vcr (2.9.3)
29
- webmock (1.21.0)
30
- addressable (>= 2.3.6)
31
- crack (>= 0.3.2)
32
-
33
- PLATFORMS
34
- ruby
35
-
36
- DEPENDENCIES
37
- httparty
38
- redis
39
- rspec
40
- vcr
41
- webmock