hatsrank 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1 @@
1
+ load 'hats.rb'
@@ -0,0 +1,32 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'hatsrank/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'hatsrank'
8
+ spec.version = Hatsrank::VERSION
9
+ spec.authors = ['Clarke Brunsdon']
10
+ spec.email = ['clarke@freerunningtechnologies.com']
11
+ spec.summary = %q{Gets information about stuff.}
12
+ spec.license = 'MIT'
13
+
14
+ spec.files = `git ls-files -z`.split("\x0")
15
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
16
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
17
+ spec.require_paths = ['lib']
18
+
19
+ spec.add_dependency 'faraday'
20
+ spec.add_dependency 'money'
21
+ spec.add_dependency 'eu_central_bank'
22
+ spec.add_dependency 'nokogiri'
23
+
24
+ spec.add_development_dependency 'bundler', "~> 1.7"
25
+ spec.add_development_dependency 'rake', "~> 10.0"
26
+
27
+ spec.add_development_dependency 'pry'
28
+ spec.add_development_dependency 'vcr'
29
+ spec.add_development_dependency 'rspec'
30
+ spec.add_development_dependency 'guard'
31
+ spec.add_development_dependency 'guard-rspec'
32
+ end
@@ -0,0 +1,3 @@
1
+
2
+
3
+
@@ -0,0 +1,6 @@
1
+ require 'hatsrank/version'
2
+ require 'hatsrank/marketable'
3
+ require 'hatsrank/listing'
4
+ require 'hatsrank/client'
5
+ require 'hatsrank/item'
6
+ require 'hatsrank/listing_parser'
@@ -0,0 +1,74 @@
1
+ require 'faraday'
2
+ require 'json'
3
+ require 'nokogiri'
4
+ require 'money'
5
+
6
+ module Hatsrank
7
+ class Client
8
+ def unusual_hats
9
+ response = faraday.get 'market/search/render/', query: 'appid:440 unusual', search_descriptions: 0, start: 0, count: 1000
10
+
11
+ body = JSON.parse response.body
12
+
13
+ results_html = body["results_html"]
14
+
15
+ content = Nokogiri::HTML(results_html)
16
+ content.css('.market_listing_row_link').map do |marketable_link|
17
+ Marketable.new self, marketable_link['href']
18
+ end
19
+
20
+ end
21
+
22
+ def listings hat
23
+ response = faraday.get hat.uri + '/render/', query: '', start: 0, count: 100
24
+ body = JSON.parse response.body
25
+
26
+ # the values are the listings
27
+ listings = body["listinginfo"].values.map do |listing|
28
+ l = Listing.new
29
+ l.listing_id = listing['listingid']
30
+ l.price = listing['price']
31
+ l.currency = listing['currencyid']
32
+
33
+ asset_info = listing['asset']
34
+ asset_id = asset_info['id']
35
+ asset_app = asset_info['appid']
36
+ asset_context = asset_info['contextid']
37
+
38
+ asset = body['assets'][asset_app.to_s][asset_context.to_s][asset_id.to_s]
39
+
40
+ item = Item.new
41
+ item.name = asset['name']
42
+
43
+ asset['descriptions'].each do |description|
44
+ d = Description.new
45
+ d.type = description['type']
46
+ d.value = description['value']
47
+ d.color = description['color']
48
+ item.descriptions << d
49
+ end
50
+
51
+ l.item = item
52
+ l
53
+ end
54
+ end
55
+
56
+ private
57
+ def faraday
58
+ @conn = Faraday.new(:url => 'http://steamcommunity.com') do |faraday|
59
+ faraday.request :url_encoded # form-encode POST params
60
+ faraday.response :logger # log requests to STDOUT
61
+ faraday.adapter Faraday.default_adapter # make requests with Net::HTTP
62
+ end
63
+ end
64
+ end
65
+
66
+ class Description
67
+ attr_accessor :type, :value, :color
68
+
69
+ def name
70
+ value.sub 'Effect: ', ''
71
+ end
72
+ end
73
+
74
+ end
@@ -0,0 +1,12 @@
1
+ module Hatsrank
2
+ class Item
3
+ attr_accessor :name, :descriptions
4
+ def initialize
5
+ @descriptions = []
6
+ end
7
+
8
+ def effects
9
+ descriptions.select { |x| x.value.match /^Effect/ }
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,39 @@
1
+ module Hatsrank
2
+ class Listing
3
+ attr_accessor :listing_id, :price, :currency, :item
4
+
5
+ def money
6
+ ::Money.new(price, currency_symbol)
7
+ end
8
+
9
+ def usd
10
+ money.exchange_to(:USD)
11
+ end
12
+
13
+ class UnknownCurrencyException < Exception
14
+ def initialize(currency)
15
+ @currency = currency
16
+ end
17
+ def message
18
+ "Currency #{@currency} not found"
19
+ end
20
+ end
21
+
22
+ private
23
+ def currency_symbol
24
+ case currency.to_i
25
+ when 2001
26
+ 'USD'
27
+ when 2002
28
+ 'GBP'
29
+ when 2003
30
+ 'EUR'
31
+ when 2005
32
+ 'RUB'
33
+ else
34
+ raise UnknownCurrencyException.new(currency)
35
+ end
36
+ end
37
+ end
38
+
39
+ end
@@ -0,0 +1,44 @@
1
+ require 'hatsrank/listing'
2
+ require 'hatsrank/item'
3
+
4
+ module Hatsrank
5
+ class ListingParser
6
+ def initialize listing_hash, asset_hash
7
+ @listing_hash = listing_hash
8
+ @asset_hash = asset_hash
9
+ end
10
+
11
+ def listings
12
+ listings = @listing_hash.values.map do |listing|
13
+
14
+ l = Listing.new
15
+ l.listing_id = listing['listingid']
16
+ l.price = listing['price']
17
+ l.currency = listing['currencyid']
18
+
19
+ asset_info = listing['asset']
20
+ asset_id = asset_info['id']
21
+ asset_app = asset_info['appid']
22
+ asset_context = asset_info['contextid']
23
+
24
+ asset = @asset_hash[asset_app.to_s][asset_context.to_s][asset_id.to_s]
25
+
26
+ item = Item.new
27
+ item.name = asset['name']
28
+
29
+ if asset['descriptions']
30
+ asset['descriptions'].each do |description|
31
+ d = Description.new
32
+ d.type = description['type']
33
+ d.value = description['value']
34
+ d.color = description['color']
35
+ item.descriptions << d
36
+ end
37
+ end
38
+
39
+ l.item = item
40
+ l
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,23 @@
1
+ module Hatsrank
2
+ class Marketable
3
+ attr_reader :uri
4
+
5
+ def initialize(client, uri)
6
+ @uri = uri.sub('http://steamcommunity.com/', '')
7
+ @client = client
8
+ end
9
+
10
+ def listings
11
+ @client.listings self
12
+ end
13
+
14
+ def info
15
+ @client.hat_info self
16
+ end
17
+
18
+ def name
19
+ listings.first.name
20
+ end
21
+
22
+ end
23
+ end
@@ -0,0 +1,3 @@
1
+ module Hatsrank
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,25 @@
1
+ require 'spec_helper'
2
+
3
+ describe Hatsrank::Client do
4
+ let(:client) { Hatsrank::Client.new }
5
+
6
+ describe '#unusual_hats' do
7
+ use_vcr_cassette
8
+
9
+ subject { client.unusual_hats }
10
+ specify { subject.count > 400 }
11
+
12
+ it 'should include the ourdoorsman' do
13
+ subject.first.uri.should == "market/listings/440/Unusual%20Outdoorsman"
14
+ end
15
+ end
16
+
17
+ describe '#listings' do
18
+ use_vcr_cassette
19
+
20
+ let(:hat) { Hatsrank::Marketable.new client, 'http://steamcommunity.com/market/listings/440/Unusual%20Bombing%20Run' }
21
+ subject { client.listings hat }
22
+
23
+ specify { subject.first.item.name.should == "Unusual Bombing Run" }
24
+ end
25
+ end
@@ -0,0 +1,30 @@
1
+ require 'spec_helper'
2
+
3
+ describe Hatsrank::Item do
4
+ let(:item) { Hatsrank::Item.new }
5
+ let(:descriptions) { [] }
6
+ before do
7
+ item.descriptions = descriptions
8
+ end
9
+
10
+ describe '#effects' do
11
+ subject { item.effects }
12
+ context 'no descriptions' do
13
+ specify { should == [] }
14
+ end
15
+ context 'has descriptions' do
16
+ let(:descriptions) { [Hatsrank::Description.new] }
17
+ before do
18
+ descriptions.first.value = description_text
19
+ end
20
+ context 'no effects' do
21
+ let(:description_text) { 'lols no effect' }
22
+ specify { should == [] }
23
+ end
24
+ context 'has an effect' do
25
+ let(:description_text) { 'Effect: FLAMING FIRE FLAMES' }
26
+ specify { should == descriptions }
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,30 @@
1
+ require 'spec_helper'
2
+
3
+ describe Hatsrank::Listing do
4
+ let(:listing) { Hatsrank::Listing.new }
5
+ let(:price) { 5000 }
6
+ before do
7
+ Money.default_bank.add_rate 'GBP', 'USD', 1.1
8
+
9
+ listing.currency = currency
10
+ listing.price = price
11
+ end
12
+
13
+ describe 'usd' do
14
+ use_vcr_cassette
15
+ subject { listing.usd }
16
+
17
+ context 'currency does not exist' do
18
+ let(:currency) { 9999 }
19
+
20
+ it 'throws an exception' do
21
+ expect { listing.usd }.to raise_error { Hatsrank::Listing::UnknownCurrencyException }
22
+ end
23
+ end
24
+
25
+ context 'currency is GBP' do
26
+ let(:currency) { 2002 }
27
+ specify { subject.to_s.should == "55.00" }
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,21 @@
1
+ require 'spec_helper'
2
+
3
+ describe Hatsrank::Marketable do
4
+ let(:url) { 'http://steamcommunity.com/market/listings/440/Unusual%20Bombing%20Run' }
5
+ let(:client) { Hatsrank::Client.new }
6
+
7
+ let(:hat) { Hatsrank::Marketable.new client, url }
8
+
9
+ describe '#listings' do
10
+ let(:hat_list) { 'hats' }
11
+ before do
12
+ client.should_receive(:listings).and_return hat_list
13
+ end
14
+
15
+ it 'calls hats from the client' do
16
+ hat.listings.should == hat_list
17
+ end
18
+
19
+ end
20
+
21
+ end
@@ -0,0 +1,22 @@
1
+ $LOAD_PATH.unshift File.expand_path("lib")
2
+ require 'hatsrank'
3
+ require 'vcr'
4
+
5
+ VCR.config do |c|
6
+ c.cassette_library_dir = 'cassettes'
7
+ c.stub_with :faraday
8
+ end
9
+
10
+ RSpec.configure do |config|
11
+ config.treat_symbols_as_metadata_keys_with_true_values = true
12
+ config.run_all_when_everything_filtered = true
13
+ config.filter_run :focus
14
+
15
+ config.extend VCR::RSpec::Macros
16
+
17
+ # Run specs in random order to surface order dependencies. If you find an
18
+ # order dependency and want to debug it, you can fix the order by providing
19
+ # the seed, which is printed after each run.
20
+ # --seed 1234
21
+ config.order = 'random'
22
+ end
metadata ADDED
@@ -0,0 +1,225 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hatsrank
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Clarke Brunsdon
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-10-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: money
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: eu_central_bank
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: nokogiri
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: bundler
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.7'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.7'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '10.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '10.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: pry
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: vcr
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: rspec
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: guard
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: guard-rspec
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ description:
168
+ email:
169
+ - clarke@freerunningtechnologies.com
170
+ executables: []
171
+ extensions: []
172
+ extra_rdoc_files: []
173
+ files:
174
+ - ".gitignore"
175
+ - ".rspec"
176
+ - Gemfile
177
+ - Guardfile
178
+ - cassettes/Hatsrank_Client/_hat_info.yml
179
+ - cassettes/Hatsrank_Client/_listings.yml
180
+ - cassettes/Hatsrank_Client/_unusual_hats.yml
181
+ - hatsfinder.rb
182
+ - hatsrank.gemspec
183
+ - hatsrank.rb
184
+ - lib/hatsrank.rb
185
+ - lib/hatsrank/client.rb
186
+ - lib/hatsrank/item.rb
187
+ - lib/hatsrank/listing.rb
188
+ - lib/hatsrank/listing_parser.rb
189
+ - lib/hatsrank/marketable.rb
190
+ - lib/hatsrank/version.rb
191
+ - spec/hatsrank/client_spec.rb
192
+ - spec/hatsrank/item_spec.rb
193
+ - spec/hatsrank/listing_spec.rb
194
+ - spec/hatsrank/marketable_spec.rb
195
+ - spec/spec_helper.rb
196
+ homepage:
197
+ licenses:
198
+ - MIT
199
+ metadata: {}
200
+ post_install_message:
201
+ rdoc_options: []
202
+ require_paths:
203
+ - lib
204
+ required_ruby_version: !ruby/object:Gem::Requirement
205
+ requirements:
206
+ - - ">="
207
+ - !ruby/object:Gem::Version
208
+ version: '0'
209
+ required_rubygems_version: !ruby/object:Gem::Requirement
210
+ requirements:
211
+ - - ">="
212
+ - !ruby/object:Gem::Version
213
+ version: '0'
214
+ requirements: []
215
+ rubyforge_project:
216
+ rubygems_version: 2.2.0
217
+ signing_key:
218
+ specification_version: 4
219
+ summary: Gets information about stuff.
220
+ test_files:
221
+ - spec/hatsrank/client_spec.rb
222
+ - spec/hatsrank/item_spec.rb
223
+ - spec/hatsrank/listing_spec.rb
224
+ - spec/hatsrank/marketable_spec.rb
225
+ - spec/spec_helper.rb