Ruby Examples

Sample: Creating a Background Burn

Code

Ruby script to create Background Burn of colorful bag and see results of said Background Burn

require 'rubygems'
require 'json' # Hash.to_json(), JSON.parse()
require 'net/http' # Net::HTTP#post()
require 'cgi' # CGI.escapeHTML()

# Make the request
burn_response = Net::HTTP.new("api.bonanza.com").post("/api/background_burns/", 
        URI.encode_www_form({ 
                url: "https://bonanzapublic.s3.amazonaws.com/images/background_burner/colorful_bag.jpg", 
                key: "your_dev_id", user_id: "sample" 
        }))

# Get the response data back as JSON
burn_response_json = JSON.parse(burn_response.body)

Data

burn_response_json's value (JSON [formatted for legibility] ):

{
               "action" => "create",
              "message" => "Successfully queued",
              "user_id" => "sample",
         "burn_user_id" => 4041305,
         "access_token" => "SG0xmqXblvWH",
                   "id" => 16049115,
                  "url" => "http://www.bonanza.com/background_burns/16049115",
    "position_in_queue" => 0,
             "complete" => false,
     "selected_mask_id" => nil,
     "final_result_url" => nil,
     "source_image_url" => "https://images.bonanzastatic.com/uploads/burnees/1474054071-3311798-0001-2239.jpg",
                "masks" => [],
      "review_complete" => false
}

Sample: Viewing result of Background Burn

Code

Building off the example above, let's see the results for the burn we created

require 'rubygems'
require 'json' # Hash.to_json(), JSON.parse()
require 'net/http' # Net::HTTP#post()
require 'cgi' # CGI.escapeHTML()

# Make the request, uses ActiveRecord's #to_query to translate hash into GET params:
burn_response = Net::HTTP.new("api.bonanza.com").get("/api/background_burns/#{ burn_response_json["id"] }?#{ {key: "your_dev_id", user_id: "sample" }.to_query }")

# Get the response data back as JSON
burn_response_json = JSON.parse(burn_response.body)

Data

burn_response_json's value (JSON [formatted for legibility] ):

{
               "action" => "show",
              "message" => "Found existing burn for user sample with id: 16049115",
              "user_id" => "sample",
         "burn_user_id" => 4041305,
         "access_token" => "SG0xmqXblvWH",
                   "id" => 16049115,
                  "url" => "http://www.bonanza.com/background_burns/16049115",
    "position_in_queue" => 0,
             "complete" => true,
     "selected_mask_id" => nil,
     "final_result_url" => "https://images.bonanzastatic.com/uploads/burns/16049115.jpg?1474054094",
     "source_image_url" => "https://images.bonanzastatic.com/uploads/burnees/1474054071-3311798-0001-2239.jpg",
                "masks" => [
        [0] {
                       "id" => 51445451,
                      "url" => "http://burner.bonanza.com/background_masks/51445451.png?",
            "composite_url" => "http://burner.bonanza.com/background_masks/51445451.png?1474054387&composite=true&no_crop=false&transparent=false",
                "variation" => "flooded",
            "quality_score" => 506
        },
        [1] {
                       "id" => 51445453,
                      "url" => "http://burner.bonanza.com/background_masks/51445453.png?",
            "composite_url" => "http://burner.bonanza.com/background_masks/51445453.png?1474054387&composite=true&no_crop=false&transparent=false",
                "variation" => "adjacent-probable",
            "quality_score" => 495
        },
        [2] {
                       "id" => 51445454,
                      "url" => "http://burner.bonanza.com/background_masks/51445454.png?",
            "composite_url" => "http://burner.bonanza.com/background_masks/51445454.png?1474054387&composite=true&no_crop=false&transparent=false",
                "variation" => "color-probable",
            "quality_score" => 481
        },
        [3] {
                       "id" => 51445456,
                      "url" => "http://burner.bonanza.com/background_masks/51445456.png?",
            "composite_url" => "http://burner.bonanza.com/background_masks/51445456.png?1474054387&composite=true&no_crop=false&transparent=false",
                "variation" => "side_burn-probable",
            "quality_score" => 435
        },
        [4] {
                       "id" => 51445459,
                      "url" => "http://burner.bonanza.com/background_masks/51445459.png?",
            "composite_url" => "http://burner.bonanza.com/background_masks/51445459.png?1474054387&composite=true&no_crop=false&transparent=false",
                "variation" => "bg-probable",
            "quality_score" => 434
        }
    ],
      "review_complete" => false
}