Class: PlayerSetHistory::Importer

Inherits:
Object
  • Object
show all
Defined in:
lib/player_set_history/importer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, token, game_id = "22406") ⇒ Importer

Returns a new instance of Importer.



4
5
6
7
8
# File 'lib/player_set_history/importer.rb', line 4

def initialize(url,token, game_id = "22406")
  @url = url
  @token = token
  @game_id = game_id
end

Instance Attribute Details

#tokenObject

Returns the value of attribute token.



2
3
4
# File 'lib/player_set_history/importer.rb', line 2

def token
  @token
end

#urlObject

Returns the value of attribute url.



2
3
4
# File 'lib/player_set_history/importer.rb', line 2

def url
  @url
end

Instance Method Details

#import_sets_from_sgg(slug_str, pid_str) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/player_set_history/importer.rb', line 74

def import_sets_from_sgg(slug_str, pid_str)
  q = set_query
  variables = {
    slug: slug_str,
    pid: pid_str
  }
  result = HTTParty.post(
    @url,
    headers: { 
      'Content-Type'  => 'application/json', 
      'Authorization' => "Bearer #{@token}" 
    },
    body: { 
      query: q, 
      variables: variables 
    }.to_json
  )
  
  event_hash =  JSON.parse(result.response.body)["data"]["user"]
  if event_hash["events"]["nodes"] == nil 
    puts "This player does not play this game"
  else
    PlayerSetHistory::Set.create_sets_from_player(event_hash, self)
  end
end

#import_user_from_sgg(slug_str) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/player_set_history/importer.rb', line 56

def import_user_from_sgg(slug_str)
  q = user_query
  variables = {slug: slug_str}
  result = HTTParty.post(
    @url,
    headers: { 
      'Content-Type'  => 'application/json', 
      'Authorization' => "Bearer #{@token}" 
    },
    body: { 
      query: q, 
      variables: variables 
    }.to_json
  )
  
  return JSON.parse(result.response.body)["data"]["user"]
end

#set_queryObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/player_set_history/importer.rb', line 32

def set_query()
  return %{
    query Users($slug: String, $pid: ID) {
      user(slug: $slug) {
        discriminator
        events(query: { page: 1, perPage: 100, filter: { videogameId: #{@game_id} } }) {
          nodes {
            tournament{
              name
              startAt
            }
            sets(filters: { playerIds: [$pid] }) {
              nodes {
                id
                displayScore
              }
            }
          }
        }
      }
    }
  }
end

#user_queryObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/player_set_history/importer.rb', line 10

def user_query()
  return %{
    query Users($slug: String) {
       user(slug: $slug) {
        genderPronoun
        discriminator
        location {
         state
         country
        }
        authorizations(types: [TWITCH, TWITTER, DISCORD]) {url}
      
        player {
         id
         gamerTag
         prefix
          }
         }
        }
  }
end