ruby 此脚本查询RIOT-API以获取召唤者的最后一个游戏

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby 此脚本查询RIOT-API以获取召唤者的最后一个游戏相关的知识,希望对你有一定的参考价值。

# Usage: ruby last_game.rb <summoner_name> <na/euw/...>

require "net/http"
require "json"
require "date"

# TODO: Insert your own key here!
API_KEY = "<YOUR-RIOT-API-KEY-HERE>"

@summoner_name = ARGV[0]
@region = ARGV[1] || "euw"

class Summoner < Struct.new(:name, :region)
  def summoner_id
    standardized_summoner_name = name.downcase.gsub(" ", "")
    @summoner_id ||= get_from_api("/api/lol/#{region}/v1.4/summoner/by-name/#{standardized_summoner_name}")
      .fetch(standardized_summoner_name)
      .fetch("id")
  end

  def recent_game
    @recent_game ||= get_from_api("/api/lol/#{region}/v1.3/game/by-summoner/#{summoner_id}/recent")["games"].first
  end

  def print_recent_game_info
    infos = {
      date: Time.at(recent_game["createDate"] / 1000)
                .strftime("%Y-%m-%d (%H:%M)"),
      summoner: name,
      outcome: recent_game["stats"]["win"] ? "won" : "lost",
      game_type: recent_game["subType"],
      game_mode: recent_game["gameMode"]
    }

    p "On %{date} %{summoner} %{outcome} his last game, " \
          "which was a %{game_type} (%{game_mode})" % infos
  end

  private

  def get_from_api(path)
    uri = URI(URI.join("https://#{region}.api.pvp.net", path) + "?api_key=#{API_KEY}")

    http = Net::HTTP.new(uri.hostname, uri.port)
    http.use_ssl = true

    response = http.request(Net::HTTP::Get.new(uri, _header = {'Content-Type' =>'application/json'}))

    case response
    when Net::HTTPSuccess
      return JSON.parse(response.body)
    when Net::HTTPNotFound
      raise "Couldn't find the record, maybe you misspelled something?"
    else
      raise "An error occurred while processing #{path}
           Response #{response.code} #{response.message}:
          #{response.body}"
    end
  end
end

summoner = Summoner.new(@summoner_name, @region)
summoner.print_recent_game_info

以上是关于ruby 此脚本查询RIOT-API以获取召唤者的最后一个游戏的主要内容,如果未能解决你的问题,请参考以下文章

ruby Ping一个电子邮件地址以查看它是否存在。此脚本解析MX记录以查找负责向广告发送邮件的SMTP服务器

ruby 快速而肮脏的脚本,以PDF格式从AT&T获取最新账单。

apache_conf 小的ruby脚本在jira中获取jql查询(高级搜索)的结果,并为Cultured Code的Things App创建todo项目。我用

ruby 教程之网络请求数据后导出 excel

ruby 教程之网络请求数据后导出 excel

ruby 教程之网络请求数据后导出 excel