Class: SwgohComlink

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

Overview

Base class for the gem, a wrapper for Comlink See github.com/swgoh-utils/swgoh-comlink for more info on Comlink

Instance Method Summary collapse

Constructor Details

#initialize(comlink_url, **keys) ⇒ SwgohComlink

Returns a new instance of SwgohComlink.



8
9
10
# File 'lib/swgoh_comlink.rb', line 8

def initialize(comlink_url, **keys)
  @api_requester = ComlinkApiRequest.new(comlink_url, keys)
end

Instance Method Details

#data(version, include_pve_units = true, request_segment = 0, enums = false) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/swgoh_comlink.rb', line 34

def data(version, include_pve_units = true, request_segment = 0, enums = false)
  body = {
    payload: {
      version:,
      includePveUnits: include_pve_units,
      requestSegment: request_segment
    },
    enums:
  }

  body_validation(body, [ { validation: (0..4), error_message: 'Request segment must be between 0 and 4', path: [:payload, :requestSegment] } ])

  parse_post_response('/data', body)
end

#enumsObject



12
13
14
# File 'lib/swgoh_comlink.rb', line 12

def enums
  JSON.parse(@api_requester.get('/enums'))
end

#get_events(enums = false) ⇒ Object



100
101
102
103
104
# File 'lib/swgoh_comlink.rb', line 100

def get_events(enums = false)
  body = { enums: }

  parse_post_response('/getEvents', body)
end

#get_guild_leaderboard(leaderboards, count, enums = false) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/swgoh_comlink.rb', line 128

def get_guild_leaderboard(leaderboards, count, enums = false)
  valid_def_ids = [
    'sith_raid',
    'rancor',
    'aat',
    'kraytdragon',
    'speederbike',
    't01D',
    't02D',
    't03D',
    't04D',
    't05D',
    'TERRITORY_WAR_LEADERBOARD',
    'GUILD:RAIDS:NORMAL_DIFF:RANCOR:DIFF06',
    'GUILD:RAIDS:NORMAL_DIFF:RANCOR:HEROIC80',
    'GUILD:RAIDS:NORMAL_DIFF:AAT:DIFF06',
    'GUILD:RAIDS:NORMAL_DIFF:AAT:HEROIC85',
    'GUILD:RAIDS:NORMAL_DIFF:SITH_RAID:DIFF06',
    'GUILD:RAIDS:NORMAL_DIFF:SITH_RAID:HEROIC85',
    'GUILD:RAIDS:NORMAL_DIFF:KRAYTDRAGON:DIFF01',
    'GUILD:RAIDS:NORMAL_DIFF:ROTJ:SPEEDERBIKE'
  ]

  leaderboards.each do |leaderboard|
    payload = verify_parameters(leaderboard, [:leaderboardType, :defId, :monthOffset])
    body_validation(leaderboard, [
      { validation: [0, 2, 3, 4, 5, 6], error_message: 'leaderboardType must in [0, 2, 3, 4, 5, 6]', path: [:leaderboardType], required: true },
      { validation: valid_def_ids, error_message: 'defId must be certain values, see docs', path: [:defId], required: [2, 4, 5, 6].include?(leaderboard.transform_keys(&:to_sym)[:leaderboardType]) },
      { validation: [0, 1], error_message: 'monthOffset must 0 or 1', path: [:monthOffset] }
    ])
  end

  body = {
    payload: {
      leaderboardId: leaderboards,
      count:
    },
    enums:
  }

  parse_post_response('/getGuildLeaderboard', body)
end

#get_guilds(filter_type, name = nil, search_criteria = nil, count = 10, enums = false) ⇒ Object



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/swgoh_comlink.rb', line 76

def get_guilds(filter_type, name = nil, search_criteria = nil, count = 10, enums = false)
  body = {
    payload: {
      filterType: filter_type,
      count:,
      enums:
    }
  }

  validations = [ { validation: [4, 5], error_message: 'filterType must be 4 or 5', path: [:payload, :filterType], required: true } ]

  if filter_type == 4
    body[:payload][:name] = name
    validations << { error_message: 'Name is required when filterType is 4', path: [:payload, :name], required: true }
  elsif filter_type == 5
    body[:payload][:searchCriteria] = search_criteria && verify_parameters(search_criteria, [:minMemberCount, :maxMemberCount, :includeInviteOnly, :minGuildGalacticPower, :maxGuildGalacticPower, :recentTbParticipatedIn])
    validations << { error_message: 'searchCriteria is required when filterType is 5', path: [:payload, :searchCriteria], required: true }
  end

  body_validation(body, validations)

  parse_post_response('/getGuilds', body)
end

#get_leaderboard(payload, enums = false) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/swgoh_comlink.rb', line 106

def get_leaderboard(payload, enums = false)
  body_validation(payload, [ { validation: [4, 6], error_message: 'leaderboardType must be 4 or 6', path: [:leaderboardType] } ])

  if payload[:leaderboardType] == 4 || payload[:leaderboard_type] == 4
    payload = verify_parameters(payload, [:leaderboardType, :eventInstanceId, :groupId])
    body_validation(payload, [
      { error_message: 'eventInstanceId must be present', path: [:eventInstanceId], required: true },
      { error_message: 'groupId must be present', path: [:groupId], required: true }
    ])
  else
    payload = verify_parameters(payload, [:leaderboardType, :league, :division])
    body_validation(payload, [
      { validation: [20, 40, 60, 80, 100], error_message: 'league must be in [20, 40, 60, 80, 100]', path: [:league], required: true },
      { validation: [5, 10, 15, 20, 25], error_message: 'division must be in [5, 10, 15, 20, 25]', path: [:division], required: true }
    ])
  end

  body = { payload:, enums: }

  parse_post_response('/getLeaderboard', body)
end

#guild(guild_id, include_recent_guild_activity = false, enums = false) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/swgoh_comlink.rb', line 64

def guild(guild_id, include_recent_guild_activity = false, enums = false)
  body = {
    payload: {
      guildId: guild_id,
      includeRecentGuildActivityInfo: include_recent_guild_activity
    },
    enums:
  }

  parse_post_response('/guild', body)
end

#localization(id, unzip = false, enums = false) ⇒ Object



16
17
18
19
20
# File 'lib/swgoh_comlink.rb', line 16

def localization(id, unzip = false, enums = false)
  body = { payload: { id: }, unzip:, enums: }

  parse_post_response('/localization', body)
end

#metadata(client_specs = {}, enums = false) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/swgoh_comlink.rb', line 22

def (client_specs = {}, enums = false)
  body = {}
  unless client_specs.empty?
    body['payload'] = {
      clientSpecs: verify_parameters(client_specs, [:platform, :bundleId, :externalVersion, :internalVersion, :region])
    }
  end
  body['enums'] = enums

  parse_post_response('/metadata', body)
end

#player(player_id, enums = false) ⇒ Object



49
50
51
52
53
# File 'lib/swgoh_comlink.rb', line 49

def player(player_id, enums = false)
  body = { payload: format_player_id_hash(player_id), enums: }

  parse_post_response('/player', body)
end

#player_arena(player_id, enums = false) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/swgoh_comlink.rb', line 55

def player_arena(player_id, enums = false)
  body = {
    payload: format_player_id_hash(player_id),
    enums:
  }

  parse_post_response('/playerArena', body)
end