Ruby Example Wrapper

require 'json'
require 'base64'

 # Let's imagine that your web server received a POST request with this body:

request_body =
"[
  {
    \"meta\": {
     \"account\": \"AccountExample\",
      \"event\": \"track\"
    },
    \"payload\": {
      \"id\": 342656641079967767,
      \"id_str\": \"342656641079967767\",
      \"asset\": \"359551XXXXX6317\",
      \"recorded_at\": \"2012-08-03T14:25:25Z\",
      \"received_at\": \"2012-08-03T14:26:28Z\",
      \"loc\": [
        2.36687,
        48.78354
      ],
      \"fields\": {
        \"GPS_SPEED\": {
          \"b64_value\": \"AAAAKg==\"
        }
      }
    }
  }
]"


def deserialize_json(json)
  return JSON.parse(json)
end

 # Decode an integer field provided in base64
def decode_b64_integer(b64_integer)
  return Base64.decode64(b64_integer).unpack('l>').first; # Handles bitsign conversion.
end

def extract_gps_speed(event)
  for field, data in event['payload']['fields']
    if field == 'GPS_SPEED'
      b64_value = data['b64_value']
      decoded_value = decode_b64_integer(b64_value)
      puts "Decoded value: #{decoded_value}"
      km_h = decoded_value.to_f / 1000 * 1.852
      puts "Speed: #{km_h} / h"
    end
  end
end

events = deserialize_json(request_body)
for event in events do
  if event['meta']['event'] == 'track'
    extract_gps_speed(event)
  end
end

require 'json'
require 'base64'

 # Let's imagine that your web server received a POST request with this body:
request_body =
"[
  {
    \"meta\": {
     \"account\": \"AccountExample\",
      \"event\": \"track\"
    },
    \"payload\": {
      \"id\": 342656641079967767,
      \"id_str\": \"342656641079967767",
      \"asset\": \"359551XXXXX6317\",
      \"recorded_at\": \"2012-08-03T14:25:25Z\",
      \"received_at\": \"2012-08-03T14:26:28Z\",
      \"loc\": [
        2.36687,
        48.78354
      ],
      \"fields\": {
        \"GPS_SPEED\": {
          \"b64_value\": \"AAAAKg==\"
        }
      }
    }
  }
]"

events = JSON.parse(request_body)

events.each do |event|
  next if event['meta']['event'] != 'track'

  event['payload']['fields'].each do |field, data|
    if field == 'GPS_SPEED'
      b64_value = data['b64_value']
      decoded_value = Base64.decode64(b64_value).unpack('l>').first.to_i(2)
      puts "Decoded value: #{decoded_value}"
      km_h = decoded_value.to_f / 1000 * 1.852
      puts "Speed: #{km_h} / h"
    end
  end
end