0

I am building a API to a recieve stats for a specific game. Right now I am able to recieve stats once every time I start my server. After looking up 1 Player I and I'm trying to refresh the page to look up another(right now I am using gets.chomp via console to enter the names) I get the following error:

uninitialized constant SiteController::API

class SiteController < ApplicationController
  require_relative '../../lib/api'


  def stats
    api = API.new(
        username: 'someusername',
        password: 'somepassword',
        token: 'sometoken',
    )

    puts "Username: "
    username = gets.chomp
    puts "Platform: "
    platform = gets.chomp

    @allStats = api.getStats(username, platform)
  end

end

api.rb

require 'net/http'
require 'json'

class API
  def initialize(auth)
    @auth = auth
    @Token = getToken['access_token']
  end

  def TOKEN_URL
    'https://antoherlink.com'
  end

  def EXCHANGE_URL
    'https://somelink.com'
  end

  def LOOKUP_URL(username)
    "https://somelink.com{username}"
  end

  def STATS_URL(id)
    "https://somelink.com"
  end

  def httpGet(url, auth)
    uri = URI(url)

    req = Net::HTTP::Get.new(uri)
    req['Authorization'] = auth

    res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
      http.request(req)
    end

    JSON.parse(res.body)
  end

  def httpPost(url, params, auth)
    uri = URI(url)

    req = Net::HTTP::Post.new(uri)
    req.set_form_data(params)
    req['Authorization'] = auth

    res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
      http.request(req)
    end

    JSON.parse(res.body)
  end

  def getToken
    params = {
        grant_type: 'password',
        includePerms: true,
        username: @auth[:username],
        password: @auth[:password]
    }

    httpPost(TOKEN_URL(), params, "basic #{@auth[:token]}")
  end

  def getExchangeCode
    httpGet(EXCHANGE_URL(), "bearer #{getToken['access_token']}")['code']
  end

  def getToken
    params = {
        grant_type: 'exchange_code',
        includePerms: true,
        token_type: 'eg1',
        exchange_code: getExchangeCode
    }

    httpPost(TOKEN_URL(), params, "basic #{@auth[:anothertoken]}")
  end

  def lookup(username)
    httpGet(LOOKUP_URL(username), "bearer #{@Token}")
  end

  def getRawStats(username)
    httpGet(STATS_URL(lookup(username)['id']), "bearer #{@Token}")
  end

  def getStats(username, platform)
    result = decodeRawStats(getRawStats(username), platform)

What did I miss?

2
  • You need to share the content of ../../lib/api.rb. Commented Aug 12, 2018 at 12:32
  • @JamieSchembri updated the question Commented Aug 12, 2018 at 12:41

1 Answer 1

1

Try changing:

class SiteController < ApplicationController
  require_relative '../../lib/api'

  # ...
end

to

require_dependency 'api'

class SiteController < ApplicationController
  # ...
end
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.