Developers

Flat
Ruby Tutorial
User: Andy Meneely
Date: 1/14/2014 2:32 pm
Views: 2069
Rating: 0

Just responding to the Tutorial Bounty on http://news.thegamecrafter.com/post/73249575903/api-tutorial-bounty

Here's my (albeit quick) code snippet for a tutorial in Ruby. I did everything that the Perl tutorial did, but obviously there are many other examples you can use.

I used the rest-client gem, which is pretty simplistic. Sinatra is another common gem used for interacting via REST.

########## ruby code ##########

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"

######## end ruby code ########

Re: Ruby Tutorial
JT
User: JT
Date: 1/17/2014 12:27 pm
Views: 775
Rating: 0

Thanks, I've included this in our next release, and awarded you the crafter points.

PreviousNext