Last active
September 11, 2015 17:02
-
-
Save smathy/fff13e92047907cce28f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class HandshakeOrderItem < ActiveRecord::Base | |
establish_connection 'production_hs' | |
require 'open-uri' | |
def self.parameter_for_query_string | |
today_start_of_day = DateTime.now.in_time_zone("Eastern Time (US & Canada)").beginning_of_day# - 86400 | |
parameter = today_start_of_day.strftime("%Y%m%dT%H:%M:%S") | |
puts "started for #{today_start_of_day}" | |
puts "parameter prepared for query string - #{parameter}" | |
parameter | |
end | |
def self.auth_header_value | |
auth_string = "xxxxxxxx:X" | |
auth_string = Base64.encode64 auth_string | |
"Basic "+auth_string | |
end | |
def self.make_url(offset) | |
url = "https://app.handshake.com/api/v2/orders?ctime__gte="+parameter_for_query_string+"&limit=50&offset="+offset.to_s | |
end | |
def self.fill_data_from_handshake_oracle | |
fetch_more = true | |
offset=0 | |
offsetIncrement = 50 | |
while(fetch_more) | |
file = open(make_url(offset), "Authorization" => auth_header_value) | |
data_hash = JSON.parse(file.read) | |
objects = data_hash["objects"] | |
if objects.size == 0 | |
fetch_more = false | |
end | |
offset += offsetIncrement | |
puts "got orders from #{offset-offsetIncrement} to #{offset-1}" | |
objects.each do |object| | |
date = object["cdate"] | |
shipdate = object["startShipDate"] | |
puts ("date class is #{date.class}") | |
salesrep = object["ownerName"] | |
customernum = object["customer"]["id"] | |
chain = object["customer"]["taxID"] | |
items = object["lines"] | |
ordernum = object["objID"] | |
ordertype = object["shippingMethod"] | |
push = object["paymentTerms"] | |
items.each do |item| | |
item_total = item["unitPrice"] | |
itemnumber = item["sku"] | |
qty = item["qty"] | |
order_record = HandshakeOrderItem.where('xdate = ? | |
and salesrep = ? | |
and itemnumber = ? | |
and qty = ? | |
and customernum = ? | |
and chain =? | |
and ordernum = ? | |
and item_total = ? | |
and ordertype = ? | |
and push = ? | |
and shipdate = ?', date, salesrep, itemnumber, qty, customernum, chain, ordernum, item_total, ordertype, push, shipdate) | |
if(order_record.present?) then | |
order = order_record.first | |
order.xdate = date | |
order.salesrep = salesrep | |
order.itemnumber = itemnumber | |
order.qty = qty | |
order.customernum = customernum | |
order.chain = chain | |
order.ordernum = ordernum | |
order.item_total = item_total | |
order.ordertype = ordertype | |
order.push = push | |
order.shipdate = shipdate | |
order.save | |
else | |
new_order = HandshakeOrderItem.new(:xdate => date.to_date, | |
:salesrep => salesrep, | |
:itemnumber => itemnumber, | |
:qty => qty, | |
:customernum => customernum, | |
:chain => chain, | |
:ordernum => ordernum, | |
:item_total => item_total, | |
:ordertype => ordertype, | |
:push => push, | |
:shipdate => shipdate ) | |
new_order.save | |
end | |
end | |
end | |
puts "saved orders from #{offset-offsetIncrement} to #{offset-1}" | |
end | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment