Module: Restiny

Extended by:
Restiny
Included in:
Restiny
Defined in:
lib/restiny.rb,
lib/restiny/errors.rb,
lib/restiny/version.rb,
lib/restiny/manifest.rb,
lib/restiny/constants.rb

Defined Under Namespace

Modules: Ammunition, ComponentType, Gender, GuardianClass, ItemLocation, Platform, Race, TierType Classes: AuthenticationError, Error, InvalidParamsError, Manifest, NetworkError, RateLimitedError, RequestError, ResponseError

Constant Summary collapse

BUNGIE_URL =
"https://www.bungie.net"
API_BASE_URL =
BUNGIE_URL + "/platform"
VERSION =
"3.1.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



26
27
28
# File 'lib/restiny.rb', line 26

def access_token
  @access_token
end

#api_keyObject

Returns the value of attribute api_key.



26
27
28
# File 'lib/restiny.rb', line 26

def api_key
  @api_key
end

#oauth_client_idObject

Returns the value of attribute oauth_client_id.



26
27
28
# File 'lib/restiny.rb', line 26

def oauth_client_id
  @oauth_client_id
end

#oauth_stateObject

Returns the value of attribute oauth_state.



26
27
28
# File 'lib/restiny.rb', line 26

def oauth_state
  @oauth_state
end

#user_agentObject

Returns the value of attribute user_agent.



26
27
28
# File 'lib/restiny.rb', line 26

def user_agent
  @user_agent
end

Instance Method Details

#api_get(url, params: {}) ⇒ Object

General request methods



151
152
153
# File 'lib/restiny.rb', line 151

def api_get(url, params: {})
  api_connection.get(url, params, token_header).body
end

#api_post(url, params: {}) ⇒ Object



155
156
157
# File 'lib/restiny.rb', line 155

def api_post(url, params: {})
  api_connection.post(url, params, token_header).body
end

#auth_post(url, params) ⇒ Object



159
160
161
# File 'lib/restiny.rb', line 159

def auth_post(url, params)
  auth_connection.post(url, params, "Content-Type" => "application/x-www-form-urlencoded").body
end

#get_authorise_url(redirect_url: nil, state: nil) ⇒ Object

OAuth methods



30
31
32
33
34
35
36
37
38
39
# File 'lib/restiny.rb', line 30

def get_authorise_url(redirect_url: nil, state: nil)
  check_oauth_client_id

  @oauth_state = state || SecureRandom.hex(15)

  params = { response_type: "code", client_id: @oauth_client_id, state: @oauth_state }
  params["redirect_url"] = redirect_url unless redirect_url.nil?

  auth_connection.build_url(BUNGIE_URL + "/en/oauth/authorize/", params).to_s
end

#get_character_profile(character_id:, membership_id:, membership_type:, components:) ⇒ Object



94
95
96
97
98
99
100
101
# File 'lib/restiny.rb', line 94

def get_character_profile(character_id:, membership_id:, membership_type:, components:)
  get_profile(
    membership_id: membership_id,
    membership_type: membership_type,
    components: components,
    type_url: "Character/#{character_id}/"
  )
end

#get_instanced_item_profile(item_id:, membership_id:, membership_type:, components:) ⇒ Object



103
104
105
106
107
108
109
110
# File 'lib/restiny.rb', line 103

def get_instanced_item_profile(item_id:, membership_id:, membership_type:, components:)
  get_profile(
    membership_id: membership_id,
    membership_type: membership_type,
    components: components,
    type_url: "Item/#{item_id}/"
  )
end

#get_manifest(locale: "en", force_download: false) ⇒ Object

Manifest methods



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/restiny.rb', line 52

def get_manifest(locale: "en", force_download: false)
  result = api_get("Destiny2/Manifest/")
  raise Restiny::ResponseError.new("Unable to determine manifest details") if result.nil?

  live_version = result.dig("version")

  @manifests ||= {}
  @manifest_versions ||= {}

  if force_download || @manifests[locale].nil? || @manifest_versions[locale] != live_version
    url = BUNGIE_URL + result.dig("mobileWorldContentPaths", locale)

    zipped_file = Down.download(url)
    database_file_path = zipped_file.path + ".db"

    Zip::File.open(zipped_file) { |file| file.first.extract(database_file_path) }

    @manifests[locale] = Manifest.new(database_file_path, live_version)
    @manifest_versions[locale] = live_version
  end

  @manifests[locale]
rescue Down::Error => error
  raise Restiny::NetworkError.new("Unable to download the manifest file", error.response.code)
rescue Zip::Error => error
  raise Restiny::Error.new("Unable to unzip the manifest file (#{error})")
end

#get_primary_membership(parent_membership_id, use_fallback: true) ⇒ Object



119
120
121
122
123
124
125
126
127
128
# File 'lib/restiny.rb', line 119

def get_primary_membership(parent_membership_id, use_fallback: true)
  result = get_user_memberships_by_id(parent_membership_id)
  return nil if result.nil? || result["primaryMembershipId"].nil?

  result["destinyMemberships"].each do |membership|
    return membership if membership["membershipID"] == result["primaryMembershipId"]
  end

  return result["destinyMemberships"][0] if use_fallback
end

#get_profile(membership_id:, membership_type:, components:, type_url: nil) ⇒ Object

Profile and related methods



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/restiny.rb', line 82

def get_profile(membership_id:, membership_type:, components:, type_url: nil)
  if !components.is_a?(Array) || components.empty?
    raise Restiny::InvalidParamsError.new("Please provide at least one component")
  end

  url = "Destiny2/#{membership_type}/Profile/#{membership_id}/"
  url += type_url if type_url
  url += "?components=#{components.join(",")}"

  api_get(url)
end

#get_user_memberships_by_id(membership_id, membership_type: Platform::ALL) ⇒ Object

User methods.



114
115
116
117
# File 'lib/restiny.rb', line 114

def get_user_memberships_by_id(membership_id, membership_type: Platform::ALL)
  raise Restiny::InvalidParamsError.new("Please provide a membership ID") if membership_id.nil?
  api_get("User/GetMembershipsById/#{membership_id}/#{membership_type}/")
end

#request_access_token(code:, redirect_url: nil) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/restiny.rb', line 41

def request_access_token(code:, redirect_url: nil)
  check_oauth_client_id

  params = { code: code, grant_type: "authorization_code", client_id: @oauth_client_id }
  params["redirect_url"] = redirect_url unless redirect_url.nil?

  auth_post("app/oauth/token/", params)
end

#search_player_by_bungie_name(name, membership_type: Platform::ALL) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/restiny.rb', line 130

def search_player_by_bungie_name(name, membership_type: Platform::ALL)
  display_name, display_name_code = name.split("#")
  if display_name.nil? || display_name_code.nil?
    raise Restiny::InvalidParamsError.new("You must provide a valid Bungie name")
  end

  api_post(
    "Destiny2/SearchDestinyPlayerByBungieName/#{membership_type}/",
    params: {
      displayName: display_name,
      displayNameCode: display_name_code
    }
  )
end

#search_users_by_global_name(name:, page: 0) ⇒ Object



145
146
147
# File 'lib/restiny.rb', line 145

def search_users_by_global_name(name:, page: 0)
  api_post("User/Search/GlobalName/#{page}/", params: { displayNamePrefix: name })
end