class Finale::Client
Constants
- BASE_URL
- REQUEST_LIMIT
Public Class Methods
new(account: nil, throttle_mode: false)
click to toggle source
# File lib/finale/client.rb, line 15 def initialize(account: nil, throttle_mode: false) @cookies = nil @request_count = 0 @throttle_mode = throttle_mode @account = account @login_url = construct_url(:auth) @order_url = construct_url(:order) @shipment_url = construct_url(:shipment) end
Public Instance Methods
get_order(id)
click to toggle source
# File lib/finale/client.rb, line 39 def get_order(id) response = request(verb: :GET, url: "#{@order_url}/#{id}") Order.new(response) end
get_order_from_shipment(shipment)
click to toggle source
# File lib/finale/client.rb, line 64 def get_order_from_shipment(shipment) get_order(shipment.order_id) end
get_orders(filter: {}, order_type_id: nil, last_updated_date: nil)
click to toggle source
# File lib/finale/client.rb, line 44 def get_orders(filter: {}, order_type_id: nil, last_updated_date: nil) filter.merge!(lastUpdatedDate: last_updated_date) if last_updated_date.present? filter.merge!(orderTypeId: [order_type_id, order_type_id]) if order_type_id.present? resp_orders = request(verb: :GET, url: @order_url, filter: filter ) rows = column_major_to_row_major(resp_orders) orders = rows.map { |r| Order.new(r) } orders end
get_shipment(id)
click to toggle source
# File lib/finale/client.rb, line 34 def get_shipment(id) response = request(verb: :GET, url: "#{@shipment_url}/#{id}") Shipment.new(response) end
get_shipments(filter: {}, shipment_type_id: nil, last_updated_date: nil)
click to toggle source
# File lib/finale/client.rb, line 54 def get_shipments(filter: {}, shipment_type_id: nil, last_updated_date: nil) filter.merge!(lastUpdatedDate: last_updated_date) if last_updated_date.present? filter.merge!(shipmentTypeId: [shipment_type_id, shipment_type_id]) if shipment_type_id.present? resp_shipments = request(verb: :GET, url: @shipment_url, filter: filter ) rows = column_major_to_row_major(resp_shipments) shipments = rows.map { |r| Shipment.new(r) } shipments end
get_shipments_from_order(order)
click to toggle source
# File lib/finale/client.rb, line 68 def get_shipments_from_order(order) (order.shipmentUrlList || []).map do |suffix_url| url = "#{BASE_URL}#{suffix_url}" response = request(verb: :GET, url: url) Shipment.new(response) end end
login(username: nil, password: nil)
click to toggle source
# File lib/finale/client.rb, line 25 def login(username: nil, password: nil) payload = { username: username || ENV['FINALE_USERNAME'], password: password || ENV['FINALE_PASSWORD'] } request(verb: :LOGIN, payload: payload) end
Private Instance Methods
column_major_to_row_major(column_major)
click to toggle source
# File lib/finale/client.rb, line 78 def column_major_to_row_major(column_major) row_major = [] keys = column_major.keys values = column_major.values || [[]] num_cols = values.count == 0 ? 0 : values.first.count num_cols.times do rowvals = keys.map { |key| column_major[key].shift } row = Hash[keys.zip(rowvals)] row_major << row end row_major end
construct_url(resource)
click to toggle source
# File lib/finale/client.rb, line 91 def construct_url(resource) "#{BASE_URL}/#{@account}/api/#{resource}" end
handle_throttling()
click to toggle source
# File lib/finale/client.rb, line 121 def handle_throttling @request_count = 0 if @throttle_mode sleep 60 else raise MaxRequests.new(REQUEST_LIMIT) end end
request(verb: nil, url: nil, payload: nil, filter: nil)
click to toggle source
# File lib/finale/client.rb, line 95 def request(verb: nil, url: nil, payload: nil, filter: nil) handle_throttling if @request_count >= REQUEST_LIMIT raise NotLoggedIn.new(verb: verb, url: url) unless verb == :LOGIN || !@cookies.nil? case verb when :LOGIN response = RestClient.post(@login_url, payload) @cookies = response.cookies when :GET params = {} if filter.present? encoded_filter = Base64.encode64(filter.to_json) params.merge!(filter: encoded_filter) end response = RestClient.get(url, cookies: @cookies, params: params) when :POST response = RestClient.post(url, cookies: @cookies) end @request_count += 1 body = JSON.parse(response.body, symbolize_names: true) body end