smite_ruby 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ffc0afcaab87a86ac938f41c576b59b220c64c6e
4
+ data.tar.gz: 77a44fdc4fe805474748ffdd6200d49828d9f65e
5
+ SHA512:
6
+ metadata.gz: 1e8c917914fe5444416b85db26896b1dff98f2642f2c3a6c53d923f3e83e164d3fbd9b1ae9515b529ec5a52ee9e97a824574ef336410aba3d5052a86ca1bc826
7
+ data.tar.gz: e0fd9755561013dc80ade5073c54127f545a27738006fd57f2bf0c318018c0d533ea109f51925b220fb6d24c883091c9fb970ced732e0abc1e00ec542dea45e7
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ *.gem
2
+ .DS_Store
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in proctor.gemspec
4
+ gemspec
data/lib/smite.rb ADDED
@@ -0,0 +1,17 @@
1
+ require 'httparty'
2
+ require 'active_support'
3
+
4
+ module Smite
5
+ end
6
+
7
+ require 'smite/object.rb'
8
+ require 'smite/ability.rb'
9
+ require 'smite/god_rank.rb'
10
+ require 'smite/god.rb'
11
+ require 'smite/item.rb'
12
+ require 'smite/friend.rb'
13
+ require 'smite/data_transform.rb'
14
+ require 'smite/match.rb'
15
+ require 'smite/player.rb'
16
+ require 'smite/game.rb'
17
+ require 'smite/client.rb'
@@ -0,0 +1,7 @@
1
+ module Smite
2
+ class Ability < Smite::Object
3
+ def inspect
4
+ "#<Smite::Ability '#{summary}'>"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,72 @@
1
+ module Smite
2
+ class Client
3
+ attr_reader :dev_id, :auth_key, :session_id
4
+ include HTTParty
5
+ base_uri 'http://api.smitegame.com/smiteapi.svc/'
6
+
7
+ def initialize(dev_id, auth_key)
8
+ @dev_id = dev_id
9
+ @auth_key = auth_key
10
+ create_session
11
+ end
12
+
13
+ # /testsessionjson/{developerId}/{signature}/{session}/{timestamp}
14
+ def test_session
15
+ api_call('testsession', [])
16
+ end
17
+
18
+ # /getdatausedjson/{developerId}/{signature}/{session}/{timestamp}
19
+ def get_data_used
20
+ api_call('getdataused', [])
21
+ end
22
+
23
+ # /createsessionjson/{developerId}/{signature}/{timestamp}
24
+ def create_session
25
+ return @session_id if valid_session?
26
+
27
+ response = api_call('createsession', [], false)
28
+ @session_id = response['session_id']
29
+ end
30
+
31
+ def api_call(method, params = [], session = true)
32
+ request = request_str(method, params, session)
33
+ self.class.get(request)
34
+ end
35
+
36
+ private
37
+
38
+ def signature(method)
39
+ Digest::MD5.hexdigest("#{dev_id}#{method}#{auth_key}#{timestamp}")
40
+ end
41
+
42
+ # current utc timestamp (formatted yyyyMMddHHmmss)
43
+ def timestamp
44
+ Time.now.utc.strftime('%Y%m%d%H%M%S')
45
+ end
46
+
47
+ def valid_session?
48
+ test_session =~ /successful/i
49
+ end
50
+
51
+ def request_str(method, params, session)
52
+ base = base_str(method)
53
+ parm = param_str(params)
54
+ sess = session_str(session)
55
+
56
+ "#{base}#{sess}#{parm}".chomp('/')
57
+ end
58
+
59
+ def base_str(method)
60
+ signature = signature(method)
61
+ "/#{method}json/#{dev_id}/#{signature}"
62
+ end
63
+
64
+ def param_str(params)
65
+ "/#{params.join('/')}"
66
+ end
67
+
68
+ def session_str(session)
69
+ "/#{session_id}#{session ? '/' : ''}#{timestamp}"
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,91 @@
1
+ module Smite
2
+ class DataTransform
3
+
4
+ def self.transform(data)
5
+ new_data = data.dup
6
+ new_data = transform_items(new_data)
7
+ new_data = transform_gods(new_data)
8
+ new_data = transform_abilities(new_data)
9
+ transform_match(new_data)
10
+ end
11
+
12
+ def self.transform_items(data)
13
+ item_fields = data.slice(*item_map_fields)
14
+ return data if item_fields.empty?
15
+
16
+ data['items'] = item_fields.values.map { |id| Smite::Game.item(id.to_i) }
17
+ data.except(*item_filter_fields)
18
+ end
19
+
20
+ def self.transform_gods(data)
21
+ god_fields = data.slice(*god_map_fields)
22
+ return data if god_fields.empty?
23
+
24
+ data['god'] = god_fields.values.map { |id| Smite::Game.god(id.to_i) }[0]
25
+ data.except(*god_filter_fields)
26
+ end
27
+
28
+ def self.transform_abilities(data)
29
+ ability_fields = data.slice(*ability_map_fields)
30
+ return data if ability_fields.empty?
31
+
32
+ data['abilities'] = ability_fields.values.map do |ability_data|
33
+ data_attrs = ability_data.slice('Id', 'Summary', 'URL')
34
+ desc = ability_data['Description']['itemDescription']
35
+
36
+ Ability.new(data_attrs.merge(desc))
37
+ end
38
+
39
+ data.except(*ability_filter_fields)
40
+ end
41
+
42
+ def self.transform_match(data)
43
+ return data unless data['Queue']
44
+
45
+ if data['Queue'] =~ /League/
46
+ data
47
+ else
48
+ data.except(*match_filter_fields)
49
+ end
50
+ end
51
+
52
+ def self.item_map_fields
53
+ %w( ActiveId1 ActiveId2 ItemId1 ItemId2 ItemId3 ItemId4 ItemId5 ItemId6 )
54
+ end
55
+
56
+ def self.item_filter_fields
57
+ item_map_fields + %w(
58
+ Item_1 Item_2 Item_3 Item_4 Item_5 Item_6 Active_1 Active_2 Active_3
59
+ )
60
+ end
61
+
62
+ def self.ability_map_fields
63
+ %w( Ability_1 Ability_2 Ability_3 Ability_4 Ability_5 )
64
+ end
65
+
66
+ def self.ability_filter_fields
67
+ ability_map_fields + %w(
68
+ Ability1 Ability2 Ability3 Ability4 Ability5
69
+ AbilityId1 AbilityId2 AbilityId3 AbilityId4 AbilityId5
70
+ abilityDescription1 abilityDescription2
71
+ abilityDescription3 abilityDescription4
72
+ abilityDescription5
73
+ )
74
+ end
75
+
76
+ def self.match_filter_fields
77
+ %w(
78
+ Ban1 Ban2 Ban3 Ban4 Ban5 Ban6
79
+ Ban1Id Ban2Id Ban3Id Ban4Id Ban5Id Ban6Id
80
+ )
81
+ end
82
+
83
+ def self.god_map_fields
84
+ %w(GodId god_id)
85
+ end
86
+
87
+ def self.god_filter_fields
88
+ god_map_fields + %w(God)
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,11 @@
1
+ module Smite
2
+ class Friend < Smite::Object
3
+ def to_player
4
+ Smite::Game.player(name)
5
+ end
6
+
7
+ def inspect
8
+ "#<Smite::Friend '#{name}'>"
9
+ end
10
+ end
11
+ end
data/lib/smite/game.rb ADDED
@@ -0,0 +1,92 @@
1
+ module Smite
2
+ class Game
3
+ class << self
4
+ attr_reader :client,:lang_code
5
+
6
+ def authenticate!(dev_id, auth_key, lang_code = 1)
7
+ @client = Smite::Client.new(dev_id, auth_key)
8
+ @lang_code = lang_code
9
+ self
10
+ end
11
+
12
+ def item(name_or_id)
13
+ id_to_item[name_or_id] ||
14
+ id_to_item[name_to_itemid[name_or_id]]
15
+ end
16
+
17
+ def god(name_or_id)
18
+ id_to_god[name_or_id] ||
19
+ id_to_god[name_to_godid[name_or_id]]
20
+ end
21
+
22
+ def match(match_id)
23
+ Match.new(match_details(match_id))
24
+ end
25
+
26
+ def player(player_name)
27
+ Player.new(client, player_name)
28
+ end
29
+
30
+ def gods
31
+ id_to_god.values
32
+ end
33
+
34
+ def items
35
+ id_to_item.values
36
+ end
37
+
38
+ private
39
+
40
+ def id_to_item
41
+ return @id_to_item unless @id_to_item.nil?
42
+
43
+ @id_to_item = get_items.map do |item_data|
44
+ [item_data['ItemId'], Item.new(item_data)]
45
+ end
46
+ @id_to_item = Hash[@id_to_item]
47
+ end
48
+
49
+ def id_to_god
50
+ return @id_to_god unless @id_to_god.nil?
51
+
52
+ @id_to_god = get_gods.map do |god_data|
53
+ [god_data['id'], God.new(god_data)]
54
+ end
55
+ @id_to_god = Hash[@id_to_god]
56
+ end
57
+
58
+ def name_to_itemid
59
+ return @name_to_itemid unless @name_to_itemid.nil?
60
+
61
+ @name_to_itemid = id_to_item.map do |id, item|
62
+ [item.device_name, id]
63
+ end
64
+ @name_to_itemid = Hash[@name_to_itemid]
65
+ end
66
+
67
+ def name_to_godid
68
+ return @name_to_godid unless @name_to_godid.nil?
69
+
70
+ @name_to_godid = id_to_god.map do |id, god|
71
+ [god.name, id]
72
+ end
73
+ @name_to_godid = Hash[@name_to_godid]
74
+ end
75
+
76
+ # /getmatchdetails[ResponseFormat]/{developerId}/{signature}/{session}/{timestamp}/{match_id}
77
+ def match_details(match_id)
78
+ client.api_call('getmatchdetails', [match_id])
79
+ end
80
+
81
+ # /getgodsjson/{developerId}/{signature}/{session}/{timestamp}/{languageCode}
82
+ def get_gods
83
+ client.api_call('getgods', [lang_code])
84
+ end
85
+
86
+ # /getitemsjson/{developerId}/{signature}/{session}/{timestamp}/{languagecode}
87
+ def get_items
88
+ client.api_call('getitems', [lang_code])
89
+ end
90
+ end
91
+ end
92
+ end
data/lib/smite/god.rb ADDED
@@ -0,0 +1,11 @@
1
+ module Smite
2
+ class God < Smite::Object
3
+ def initialize(data)
4
+ @data = DataTransform.transform_abilities(data)
5
+ end
6
+
7
+ def inspect
8
+ "#<Smite::God '#{name}'>"
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,28 @@
1
+ module Smite
2
+ class GodRank < Smite::Object
3
+ def initialize(data)
4
+ @data = DataTransform.transform_gods(data)
5
+ end
6
+
7
+ def level
8
+ case rank
9
+ when 0 then 'none'
10
+ when (1..4) then 'gold'
11
+ when (5..9) then 'legendary'
12
+ when 10 then 'diamond'
13
+ end
14
+ end
15
+
16
+ def mastery
17
+ mastered ? 'mastered' : 'unmastered'
18
+ end
19
+
20
+ def mastered?
21
+ rank > 0
22
+ end
23
+
24
+ def inspect
25
+ "#<Smite::GodRank '#{god.name}' Lvl. #{rank} (#{level})>"
26
+ end
27
+ end
28
+ end
data/lib/smite/item.rb ADDED
@@ -0,0 +1,7 @@
1
+ module Smite
2
+ class Item < Smite::Object
3
+ def inspect
4
+ "#<Smite::Item '#{device_name}'>"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,11 @@
1
+ module Smite
2
+ class Match < Smite::Object
3
+ def initialize(data)
4
+ @data = DataTransform.transform(data)
5
+ end
6
+
7
+ def inspect
8
+ "#<Smite::Match '#{queue}' '#{win_status}'>"
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,23 @@
1
+ module Smite
2
+ class Object
3
+ attr_reader :data
4
+
5
+ def initialize(data)
6
+ @data = data
7
+ end
8
+
9
+ def attributes
10
+ @data.keys.map(&ActiveSupport::Inflector.method(:underscore))
11
+ end
12
+
13
+ def method_missing(method)
14
+ key = @data.keys.find do |attribute|
15
+ camel_attr = ActiveSupport::Inflector.underscore(attribute)
16
+ camel_method = ActiveSupport::Inflector.underscore(method.to_s)
17
+ camel_attr == camel_method
18
+ end
19
+
20
+ key ? @data[key] : super
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,58 @@
1
+ module Smite
2
+ class Player < Smite::Object
3
+ attr_reader :client, :player_name
4
+
5
+ def initialize(client, player_name)
6
+ @client = client
7
+ @player_name = player_name
8
+ @data = get_player
9
+ end
10
+
11
+ def friends
12
+ @friends ||= get_friends.reject { |f| f['name'].empty? }.map(&Friend.method(:new))
13
+ end
14
+
15
+ def god_ranks
16
+ @ranks ||= get_god_ranks.map(&GodRank.method(:new))
17
+ end
18
+
19
+ def match_history
20
+ @history ||= get_match_history.map(&Match.method(:new))
21
+ end
22
+
23
+ def achievements
24
+ @achievements ||= get_achievements
25
+ end
26
+
27
+ def inspect
28
+ "#<Smite::Player '#{name}'>"
29
+ end
30
+
31
+ private
32
+
33
+ # /getplayerachievementsjson/{developerId}/{signature}/{session}/{timestamp}/{playerId}
34
+ def get_achievements
35
+ client.api_call('getplayerachievements', [id])
36
+ end
37
+
38
+ # /getfriendsjson/{developerId}/{signature}/{session}/{timestamp}/{player}
39
+ def get_friends
40
+ client.api_call('getfriends', [player_name])
41
+ end
42
+
43
+ # /getgodranksjson/{developerId}/{signature}/{session}/{timestamp}/{player}
44
+ def get_god_ranks
45
+ client.api_call('getgodranks', [player_name])
46
+ end
47
+
48
+ # /getplayerjson/{developerId}/{signature}/{session}/{timestamp}/{player}
49
+ def get_player
50
+ client.api_call('getplayer', [player_name])[0]
51
+ end
52
+
53
+ # /getmatchhistoryjson/{developerId}/{signature}/{session}/{timestamp}/{player}
54
+ def get_match_history
55
+ client.api_call('getmatchhistory', [player_name])
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,17 @@
1
+ $:.push File.expand_path("../lib", __FILE__)
2
+ Gem::Specification.new do |s|
3
+ s.name = 'smite_ruby'
4
+ s.version = '1.0.0'
5
+ s.date = '2016-01-28'
6
+ s.summary = 'Ruby Smite API'
7
+ s.description = 'Ruby Client for consuming the Smite API'
8
+ s.authors = ['NcUltimate']
9
+ s.email = 'jmklemen@gmail.com'
10
+ s.files = `git ls-files`.split($/)
11
+ s.require_paths = ["lib"]
12
+ s.license = 'MIT'
13
+ s.homepage = 'https://rubygems.org/gems/smite-ruby'
14
+
15
+ s.add_runtime_dependency 'httparty'
16
+ s.add_runtime_dependency 'activesupport'
17
+ end
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: smite_ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - NcUltimate
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-01-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: httparty
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: activesupport
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
+ description: Ruby Client for consuming the Smite API
42
+ email: jmklemen@gmail.com
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - .DS_Store
48
+ - .gitignore
49
+ - Gemfile
50
+ - lib/smite.rb
51
+ - lib/smite/ability.rb
52
+ - lib/smite/client.rb
53
+ - lib/smite/data_transform.rb
54
+ - lib/smite/friend.rb
55
+ - lib/smite/game.rb
56
+ - lib/smite/god.rb
57
+ - lib/smite/god_rank.rb
58
+ - lib/smite/item.rb
59
+ - lib/smite/match.rb
60
+ - lib/smite/object.rb
61
+ - lib/smite/player.rb
62
+ - smite_ruby.gemspec
63
+ homepage: https://rubygems.org/gems/smite-ruby
64
+ licenses:
65
+ - MIT
66
+ metadata: {}
67
+ post_install_message:
68
+ rdoc_options: []
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - '>='
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ requirements: []
82
+ rubyforge_project:
83
+ rubygems_version: 2.0.14
84
+ signing_key:
85
+ specification_version: 4
86
+ summary: Ruby Smite API
87
+ test_files: []