Sha256: fb0c5c783b2a32fe2421f94413af05dafb681d011d482c033d86b56fda9b4f9c

Contents?: true

Size: 1.49 KB

Versions: 6

Compression:

Stored size: 1.49 KB

Contents

# frozen_string_literal: true

require 'net/http'
require 'json'

module Warframe
  module REST
    # A request to send to the Warframe Stat API.
    class Request
      # @return [Warframe::REST::Client]
      attr_reader :client

      # @return [String]
      attr_reader :path

      # Instantiate a Request
      #
      # @param client [Warframe::REST::Client]
      # @param path [String]
      # @param klass [Warframe::Models]
      # @return [Warframe:REST:Request]
      def initialize(client, path, klass)
        @client = client
        @path = client.base_url + path + "?language=#{@client.language}"
        @klass = klass
      end

      # Sends an HTTP request with the attached parameters and headers.
      # Will either return the Model, or collection of Models.
      # @return [Warframe::Models, Array<[Warframe::Models]>]
      def send
        uri = URI(path)
        req = Net::HTTP::Get.new(uri)
        return_parsed get_response uri, req
      end

      private

      # Returns the parsed JSON response in the form of a [Warframe::Models] or an array of [Warframe::Models]
      # @param resp [Net::HTTP.get]
      # @return [Warframe::Models, Array<[Warframe::Models]>]
      def return_parsed(resp)
        parsed = JSON.parse(resp)
        @klass.new parsed
      end

      def get_response(uri, req)
        Net::HTTP.get(uri) do |http|
          http.use_ssl = true
          http.request req
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
warframe-0.2.1 lib/warframe/rest/request.rb
warframe-0.2.0 lib/warframe/rest/request.rb
warframe-0.1.3 lib/warframe/rest/request.rb
warframe-0.1.2 lib/warframe/rest/request.rb
warframe-0.1.1 lib/warframe/rest/request.rb
warframe-0.1.0 lib/warframe/rest/request.rb