johnhamelink
3/28/2015 - 9:23 PM

Ruby CLI Client for Papa Johns' API

Ruby CLI Client for Papa Johns' API

require 'rubygems'
require 'bundler/setup'

require 'faraday'
require 'pry'
require 'json'

@conn = Faraday.new(url: 'https://api.papajohns.co.uk') do |faraday|
  faraday.adapter Faraday.default_adapter
end

def login(email, password)
  login = @conn.post do |req|
    req.url '/Login.asmx/DoLogin'
    req.headers['Content-Type'] = 'application/json;charset=UTF-8'
    req.headers['Accept'] = 'application/json, text/plain, */*'
    req.headers['User-Agent'] = 'Mozilla/5.0 (Linux; Android 5.0.1; Nexus 5 Build/LRX22C) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/37.0.0.0 Mobile Safari/537.36'
    req.headers['Accept-Language'] = 'en-GB,en-US;q=0.8'
    req.headers['X-Requested-With'] = 'uk.co.papajohns.ppjqg'
    req.body = JSON.generate({ 'loginRequest' => {
        email: email,
        password: password,
        deviceid: 'b4c40cefa8e46ddd',
        devicetype: 'Android / Nexus 5 / 5.0.1'
      }
    })
  end


  login = JSON.parse login.body
  login = login['d']['authentication']
  @token = login['token']
end

def get_orders(token)
  orders = @conn.post do |req|
    req.url '/Order.asmx/GetOrders'
    req.headers['Content-Type'] = 'application/json;charset=UTF-8'
    req.headers['Accept'] = 'application/json, text/plain, */*'
    req.headers['User-Agent'] = 'Mozilla/5.0 (Linux; Android 5.0.1; Nexus 5 Build/LRX22C) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/37.0.0.0 Mobile Safari/537.36'
    req.headers['Accept-Language'] = 'en-GB,en-US;q=0.8'
    req.headers['X-Requested-With'] = 'uk.co.papajohns.ppjqg'
    req.body = JSON.generate({ 'authenticationRequest' => {
        deviceid: 'b4c40cefa8e46ddd',
        token: token
      }
    })
  end

  #  [{"OrderId"=>9300756,
  #    "OrderDateTime"=>"2015-03-28 19:09:11",
  #    "StatusLevel"=>8,
  #    "ShipmentMethod"=>2,
  #    "StoreId"=>479,
  #    "OrderItems"=>
  #     [{"ProductId"=>452,
  #       "ProductName"=>"Hawaiian BBQ (Extra Extra Large/Authentic Thin Crust)",
  #       "ItemGuid"=>"75259e86-5a9a-4603-b174-89d93d6e78a4",
  #       "ItemId"=>0}]}]

  orders = JSON.parse orders.body
  return orders['d']['orders']
end

def shipment_method(shipment_id)
  return [
    'unknown',
    'Collection',
    'Delivery'
  ][shipment_id]
end

def order_status(status_id)
  return [
    "Thank you for your order, we're making your pizza",
    "Thank you for your order, we're making your pizza",
    "Thank you for your order, we're making your pizza",
    "Thank you for your order, we're making your pizza",
    "Thank you for your order, we're making your pizza",
    "Now we're cookin'",
    "It's on the road",
    "Thank you for your order, we're making your pizza",
    'Tuck In',
    "Sorry, we could not process your order",
  ][status_id]
end


login(ENV['email'], ENV['password'])
orders = get_orders(@token)

puts "Orders:"

orders.each do |order|
  puts " => Order ##{order['OrderId']}:"
  puts "    - Shipment: #{shipment_method(order['ShipmentMethod'])}"
  puts "    - Order Status: #{order_status(order['StatusLevel'])}"
  puts "    - Product Items:"
  order['OrderItems'].each do |item|
    puts "      - #{item['ProductName']}"
  end
end