Ruby Example

NOTE: Get an APIKey.

This code will allow you to get a session, fetch account info, upload a file, and search games. From it you should be able to discern how to work with the entire TGC API.

Using HTTParty

 #!/usr/bin/env ruby
 
 # This Ruby client for The Game Crafter is more involved than simply using HTTParty standalone.
 # With this code, you can expand upon the client and easily add your own encapsulated functionality!
 # It also shows some good Ruby design practices if you're still new to the language.
 
 # You'll need at least Ruby 1.9.3 or so and Bundler.
 
 # make it easy to do multi-part requests
 require 'httmultiparty'
 
 module TheGameCrafter
   # wrap up our basic client actions in a class
   class Client
     include HTTMultiParty
 
     base_uri 'https://www.thegamecrafter.com/api'.freeze
 
     # we set these in the initializer...
     attr_accessor :api_key, :username, :password
 
     # these get set after #get_session!
     attr_reader :user_id, :session_id
 
     def initialize(username, password, api_key)
       @username, @password, @api_key = username, password, api_key
     end
 
     # get a session and user id
     def get_session!
       response = post '/session', query: { 'username' => username, 'password' => password, 'api_key_id' => api_key }
 
       @session_id = response['id']
       @user_id = response['user_id']
     end
 
     # get user account info
     def account_info
       get '/user/' + @user_id, query: default_query_params
     end
 
     # upload a file
     # provide an IO that responds to #path. A File is good!
     def upload_file(io, folder_id)
       post '/file', query: { 'name' => File.basename(io.path), 'folder_id' => folder_id, 'file' => io }.merge(default_query_params)
     end
 
     # search for games
     def search_games(query)
       get '/game', query: { 'q' => query }.merge(default_query_params)
     end
 
     private
     # wrappers around HTTParty to handle TGC responses automagically
     def post(*args)
       response = self.class.post *args
       response.parsed_response['result']
     end
 
     def get(*args)
       response = self.class.get *args
       response.parsed_response['result']
     end
 
     # we use this a lot. factor it out.
     def default_query_params
       { 'session_id' => session_id }
     end
   end
 end
 
 client = TheGameCrafter::Client.new('username', 'password', 'api key')
 
 client.get_session!
 
 account_info = client.account_info
 
 p client.upload_file File.open('file.jpg'), account_info['root_folder_id']
 
 p client.search_games "Steampunk"

Using Rest Client

 require 'rest-client'
 require 'json'
 
 url="https://www.thegamecrafter.com/api"
 api_key_id = '' #replace with yours
 username = '' #replace with yours
 password = '' #replace with yours
 root_folder_id = '' #replace with yours
 
 #Get a Session
 params = {:api_key_id => api_key_id, :username => username, :password => password}
 response = RestClient.post("#{url}/session", params)
 puts "HTTP Success? #{response.code == 200}"
 puts "Session response: #{response}"
 session = JSON.parse(response)['result']
 
 #Fetch My Account Info
 params = {:session_id => session['id']}
 response = RestClient.get("#{url}/user/#{session['user_id']}", params)
 puts "Account info response: #{response}"
 user = JSON.parse(response)['result']
 
 #Upload a File
 params = {
   :name => 'example.png',
   :file => File.new('example.png'),
   :folder_id => root_folder_id,
   :session_id => session['id']
 }
 response = RestClient.post("#{url}/file", params)
 puts "HTTP Success? #{response.code == 200}"
 puts "Upload response: #{response}"
 
 #Search Games
 params = {
   :q => 'Steampunk',
   :session_id => session['id'] #optional
 }
 response = RestClient.get("#{url}/game", params)
 puts "HTTP Success? #{response.code == 200}"
 results = JSON.parse(response)['result'] 
 puts "There are #{results.size} results"
^ Back to Top ^