hearthstone_api_utils 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3ed7f5db3848aff7630d96b9a85a384289fc371b
4
+ data.tar.gz: caddd5f47dd38aaa535dad2f2beb88b07e4fca15
5
+ SHA512:
6
+ metadata.gz: 93f18ac1327304c45ed79e6d8e87d78b05e4b34daad0a669b371b60b58e4e1d7aeaffe13e7654d23feedf92064982333154ed77d3bd6d27f9f46ba20a0f48857
7
+ data.tar.gz: f3a4080ecedbc397068d8c8eca6798cf08dae6d78b8f4f4fb1836e1fa9095da631ae4eec8a4ae25da0f7049d1f07e636597bc04ec5086f23fdfeba3ee8245519
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ *.swp
data/.rvmrc ADDED
@@ -0,0 +1,62 @@
1
+ #!/usr/bin/env bash
2
+
3
+ # This is an RVM Project .rvmrc file, used to automatically load the ruby
4
+ # development environment upon cd'ing into the directory
5
+
6
+ # First we specify our desired <ruby>[@<gemset>], the @gemset name is optional,
7
+ # Only full ruby name is supported here, for short names use:
8
+ # echo "rvm use 2.1.2@hearthstone_api_utils" > .rvmrc
9
+ environment_id="ruby-2.1.2@hearthstone_api_utils"
10
+
11
+ # Uncomment the following lines if you want to verify rvm version per project
12
+ # rvmrc_rvm_version="1.25.17 (stable)" # 1.10.1 seems like a safe start
13
+ # eval "$(echo ${rvm_version}.${rvmrc_rvm_version} | __rvm_awk -F. '{print "[[ "$1*65536+$2*256+$3" -ge "$4*65536+$5*256+$6" ]]"}' )" || {
14
+ # echo "This .rvmrc file requires at least RVM ${rvmrc_rvm_version}, aborting loading."
15
+ # return 1
16
+ # }
17
+
18
+ # First we attempt to load the desired environment directly from the environment
19
+ # file. This is very fast and efficient compared to running through the entire
20
+ # CLI and selector. If you want feedback on which environment was used then
21
+ # insert the word 'use' after --create as this triggers verbose mode.
22
+ if [[ -d "${rvm_path:-$HOME/.rvm}/environments"
23
+ && -s "${rvm_path:-$HOME/.rvm}/environments/$environment_id" ]]
24
+ then
25
+ \. "${rvm_path:-$HOME/.rvm}/environments/$environment_id"
26
+ for __hook in "${rvm_path:-$HOME/.rvm}/hooks/after_use"*
27
+ do
28
+ if [[ -f "${__hook}" && -x "${__hook}" && -s "${__hook}" ]]
29
+ then \. "${__hook}" || true
30
+ fi
31
+ done
32
+ unset __hook
33
+ if (( ${rvm_use_flag:=1} >= 2 )) # display only when forced
34
+ then
35
+ if [[ $- == *i* ]] # check for interactive shells
36
+ then printf "%b" "Using: $(tput setaf 2 2>/dev/null)$GEM_HOME$(tput sgr0 2>/dev/null)
37
+ " # show the user the ruby and gemset they are using in green
38
+ else printf "%b" "Using: $GEM_HOME
39
+ " # don't use colors in non-interactive shells
40
+ fi
41
+ fi
42
+ else
43
+ # If the environment file has not yet been created, use the RVM CLI to select.
44
+ rvm --create "$environment_id" || {
45
+ echo "Failed to create RVM environment '${environment_id}'."
46
+ return 1
47
+ }
48
+ fi
49
+
50
+ # If you use bundler, this might be useful to you:
51
+ # if [[ -s Gemfile ]] && {
52
+ # ! builtin command -v bundle >/dev/null ||
53
+ # builtin command -v bundle | GREP_OPTIONS="" \grep $rvm_path/bin/bundle >/dev/null
54
+ # }
55
+ # then
56
+ # printf "%b" "The rubygem 'bundler' is not installed. Installing it now.\n"
57
+ # gem install bundler
58
+ # fi
59
+ # if [[ -s Gemfile ]] && builtin command -v bundle >/dev/null
60
+ # then
61
+ # bundle install | GREP_OPTIONS="" \grep -vE '^Using|Your bundle is complete'
62
+ # fi
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in hearthstone_api_utils.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,19 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ hearthstone_api_utils (0.0.1)
5
+ trollop
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ rake (10.3.2)
11
+ trollop (2.0)
12
+
13
+ PLATFORMS
14
+ ruby
15
+
16
+ DEPENDENCIES
17
+ bundler (~> 1.6)
18
+ hearthstone_api_utils!
19
+ rake
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 Alexander Vanadio
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,24 @@
1
+ hearthstone_api_utils
2
+ =====================
3
+
4
+ Denormalize the data from http://hearthstoneapi.com/ and enrich it with image urls
5
+
6
+ ## A Sample MongoDB Document (JSON is also available)
7
+ ```
8
+ {
9
+ "_id" : ObjectId("53755b3e305bd2b2ddac6cd0"),
10
+ "set" : 3,
11
+ "quality" : "Legendary",
12
+ "type" : "Minion",
13
+ "cost" : 9,
14
+ "attack" : 8,
15
+ "health" : 8,
16
+ "faction" : 3,
17
+ "race" : "Dragon",
18
+ "elite" : 1,
19
+ "name" : "Onyxia",
20
+ "description" : "Battlecry: Summon 1/1 Whelps until your side of the battlefield is full.",
21
+ "image_url" : "https://s3-us-west-2.amazonaws.com/hearthstats/cards/onyxia.png",
22
+ "class" : "Neutral"
23
+ }
24
+
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,22 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $: << File.expand_path('../lib', File.dirname(__FILE__))
4
+ require 'hearthstone_api_utils'
5
+ require 'trollop'
6
+
7
+ opts = Trollop::options do
8
+ opt :write_to_file, "Dump the data to JSON for manual MongoDB ingest"
9
+ opt :validate_image_urls, "Ensure that every image url returns a 200", :default => false
10
+ opt :write_to_local_mongo, "Writes the data to your local mongo under the 'hearthstone' database and 'cards' collection"
11
+ end
12
+
13
+ hs_parser = HearthstoneAPI::CardParser.new
14
+ cards = hs_parser.parse opts[:validate_image_urls]
15
+
16
+ if opts[:write_to_file]
17
+ HearthstoneAPI::CardWriter.write_to_json_file cards, 'hearthstone_cards.json'
18
+ end
19
+
20
+ if opts[:write_to_local_mongo]
21
+ HearthstoneAPI::CardWriter.write_to_mongodb cards, 'cards', 'hearthstone'
22
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'hearthstone_api_utils/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "hearthstone_api_utils"
8
+ spec.version = HearthstoneApiUtils::VERSION
9
+ spec.authors = ["Alexander Vanadio"]
10
+ spec.email = ["execdd17@gmail.com"]
11
+ spec.summary = %q{Converts the cards from hearthstoneapi.com to MongoDB objects and enriches with image URL }
12
+ spec.homepage = "https://github.com/execdd17/hearthstone_api_utils"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.6"
21
+ spec.add_development_dependency "rake"
22
+
23
+ spec.add_runtime_dependency 'trollop'
24
+ end
@@ -0,0 +1,119 @@
1
+ require 'json'
2
+
3
+ class HearthstoneAPI; end
4
+
5
+ class HearthstoneAPI::CardWriter
6
+ def self.write_to_json_file cards, filename
7
+ File.open(filename, 'w') do |file|
8
+ file.puts JSON.dump(cards)
9
+ end
10
+ end
11
+
12
+ def self.write_to_mongodb cards, coll_name, db_name
13
+ filename = File.expand_path('../bin', File.dirname(__FILE__)) << '/tmp_hearthstone_cards'
14
+ self.write_to_json_file cards, filename
15
+ cmd = "mongoimport -d #{db_name} -c #{coll_name} --jsonArray #{filename}"
16
+ puts "Running '#{cmd}'"
17
+ `#{cmd}`
18
+ `rm -f "#{filename}"`
19
+ end
20
+ end
21
+
22
+ class HearthstoneAPI::CardParser
23
+ FIND_ALL_URL = 'http://hearthstoneapi.com/cards/findAll'
24
+
25
+ def initialize
26
+ @cards = JSON.parse `curl "#{FIND_ALL_URL}"`
27
+ end
28
+
29
+ def parse validate_image_urls=false
30
+ parsed_cards = @cards.dup
31
+
32
+ parsed_cards.each do |card|
33
+ add_image_url card
34
+ remove_unwanted_attributes card, 'createdAt', 'updatedAt', 'id'
35
+ restore_type_value card
36
+ restore_class_value card
37
+ restore_quality_value card
38
+ restore_race_value card
39
+ end
40
+
41
+ if validate_image_urls
42
+ raise "Some urls are not valid" unless valid_image_urls?(parsed_cards)
43
+ end
44
+
45
+ puts "Parsed #{parsed_cards.size} Cards Successfully\nThe first one is:"
46
+ puts parsed_cards.first
47
+ parsed_cards
48
+ end
49
+
50
+ private
51
+
52
+ def add_image_url card
53
+ formatted_card_name = card["name"].downcase
54
+ formatted_card_name.gsub! /[:.]\s/, '-'
55
+ formatted_card_name.gsub! /!$/, ''
56
+ formatted_card_name.gsub! /\s|'|:/, '-'
57
+
58
+ card_image_base_url = 'https://s3-us-west-2.amazonaws.com/hearthstats/cards/'
59
+ card["image_url"] = card_image_base_url << formatted_card_name << '.png'
60
+ end
61
+
62
+ def remove_unwanted_attributes card, *attrs
63
+ attrs.each { |attr| card.delete attr }
64
+ end
65
+
66
+
67
+ def valid_image_urls? cards
68
+ cards.all? do |card|
69
+ `curl "#{card['image_url']}"`
70
+ end
71
+ end
72
+
73
+ def restore_type_value card
74
+ case card['type']
75
+ when 4 then card['type'] = 'Minion'
76
+ when 5 then card['type'] = 'Spell'
77
+ when 7 then card['type'] = 'Weapon'
78
+ end
79
+ end
80
+
81
+ def restore_class_value card
82
+ case card['classs']
83
+ when 1 then card['class'] = 'Warrior'
84
+ when 2 then card['class'] = 'Paladin'
85
+ when 3 then card['class'] = 'Hunter'
86
+ when 4 then card['class'] = 'Rogue'
87
+ when 5 then card['class'] = 'Priest'
88
+ when 7 then card['class'] = 'Shaman'
89
+ when 8 then card['class'] = 'Mage'
90
+ when 9 then card['class'] = 'Warlock'
91
+ when 11 then card['class'] = 'Druid'
92
+ else
93
+ card['class'] = 'Neutral'
94
+ end
95
+
96
+ card.delete 'classs'
97
+ end
98
+
99
+ def restore_quality_value card
100
+ case card['quality']
101
+ when 0 then card['quality'] = 'Free'
102
+ when 1 then card['quality'] = 'Common'
103
+ when 3 then card['quality'] = 'Rare'
104
+ when 4 then card['quality'] = 'Epic'
105
+ when 5 then card['quality'] = 'Legendary'
106
+ end
107
+ end
108
+
109
+ def restore_race_value card
110
+ case card['race']
111
+ when 14 then card['race'] = 'Murloc'
112
+ when 15 then card['race'] = 'Demon'
113
+ when 20 then card['race'] = 'Beast'
114
+ when 21 then card['race'] = 'Totem'
115
+ when 23 then card['race'] = 'Pirate'
116
+ when 24 then card['race'] = 'Dragon'
117
+ end
118
+ end
119
+ end
@@ -0,0 +1,3 @@
1
+ module HearthstoneApiUtils
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hearthstone_api_utils
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Alexander Vanadio
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-06-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
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: trollop
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
+ description:
56
+ email:
57
+ - execdd17@gmail.com
58
+ executables:
59
+ - parser_and_writer.rb
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - ".rvmrc"
65
+ - Gemfile
66
+ - Gemfile.lock
67
+ - LICENSE
68
+ - README.md
69
+ - Rakefile
70
+ - bin/parser_and_writer.rb
71
+ - hearthstone_api_utils.gemspec
72
+ - lib/hearthstone_api_utils.rb
73
+ - lib/hearthstone_api_utils/version.rb
74
+ homepage: https://github.com/execdd17/hearthstone_api_utils
75
+ licenses:
76
+ - MIT
77
+ metadata: {}
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 2.2.2
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: Converts the cards from hearthstoneapi.com to MongoDB objects and enriches
98
+ with image URL
99
+ test_files: []