ruby 用于Papa Johns的API的Ruby CLI客户端

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby 用于Papa Johns的API的Ruby CLI客户端相关的知识,希望对你有一定的参考价值。

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

以上是关于ruby 用于Papa Johns的API的Ruby CLI客户端的主要内容,如果未能解决你的问题,请参考以下文章

Ruby注释

Ruby知识概要

使用fpm制作rpm包

Ruby注入初始为哈希

使用innodb_ruby探查Innodb索引结构

Papa的朋友圈(原POJ 2186)Tarjan