Last active
August 29, 2015 14:05
-
-
Save theinventor/bf41fddad9455c1a7f73 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
task :import_invoices_with_api_client => :environment do | |
begin | |
#CustomerName,CustomerPhone,Invoice Date,Invoice Subtotal,Invoice Tax,Invoice Total,Invoice Number | |
path = "#{Rails.root}/tmp/test.csv" | |
subdomain = 'ithelpdesk' | |
api = TroysAPIClient.new(subdomain,'API_KEY') | |
api.base_url = "https://#{subdomain}.repairshopr.com" | |
CSV.foreach(path, headers: true, :encoding => 'windows-1251:utf-8') do |row| | |
puts "here is a row #{row}" | |
date = row['date'] | |
subtotal = row['subtotal'].gsub('$','').gsub(' ','').to_f | |
number = row['number'] | |
invoice = {} | |
invoice[:date] = Time.strptime(date,'%m/%d/%y') | |
invoice[:paid] = true | |
invoice[:date_received] = Time.strptime(date,'%m/%d/%y') | |
invoice[:number] = number | |
invoice[:phone] = phone | |
invoice[:line_items] = [ | |
{item: 'Legacy', name: 'Invoice Line Item', cost: 0.0, price: subtotal, quantity: 1} | |
] | |
api.create_invoice invoice | |
end | |
rescue => ex | |
puts "ERROR #{ex}" | |
end | |
end |
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 TroysAPIClient | |
attr_accessor :subdomain, :api_key, :base_url, :api_version | |
def initialize subdomain,api_key | |
@subdomain = subdomain | |
@api_key = api_key | |
@api_version = "/api/v1" | |
end | |
def base_url | |
@base_url ||= "https://#{subdomain}.repairshopr.com" | |
@base_url ||= "http://#{subdomain}.lvh.me:3000" if Rails.env.development? | |
end | |
def customers | |
response = Faraday.get "#{base_url}/#{@api_version}/customers.json?api_key=#{@api_key}" | |
JSON.parse response.body | |
end | |
def autocomplete query | |
response = Faraday.get "#{base_url}/#{@api_version}/customers/autocomplete.json?api_key=#{@api_key}&query=#{query}" | |
JSON.parse response.body | |
end | |
def invoices | |
response = Faraday.get "#{base_url}/#{@api_version}/invoices.json?api_key=#{@api_key}" | |
JSON.parse response.body | |
end | |
def tickets | |
response = Faraday.get "#{base_url}/#{@api_version}/tickets.json?api_key=#{@api_key}" | |
JSON.parse response.body | |
end | |
def create_customer params | |
setup_connection | |
response = @conn.post "#{api_version}/customers.json?api_key=#{@api_key}", params | |
JSON.parse response.body | |
end | |
def demo_customer | |
new_cust = {} | |
new_cust[:firstname] = 'JonnyAPI' | |
new_cust[:lastname] = 'SmithAPI' | |
new_cust[:phone] = '4256611' | |
new_cust[:email] = 'john+123@repairshopr.com' | |
create_customer new_cust | |
end | |
def create_invoice params | |
setup_connection | |
response = @conn.post "#{@api_version}/invoices.json?api_key=#{@api_key}", params | |
JSON.parse response.body | |
end | |
def create_ticket params | |
setup_connection | |
response = @conn.post "#{@api_version}/tickets.json?api_key=#{@api_key}", params | |
JSON.parse response.body | |
end | |
def create_comment params | |
setup_connection | |
response = @conn.post "#{@api_version}/tickets/#{params[:number]}/comment.json?api_key=#{@api_key}", params | |
JSON.parse response.body | |
end | |
def demo_invoice | |
new_invoice = {} | |
new_invoice[:customer_id] = '120018' | |
new_invoice[:date] = '2013-05-25' | |
new_invoice[:line_items] = [ | |
{item: 'Some Item', name: 'Some big description', cost: 0.0, price: 19.99, quantity: 1} | |
] | |
create_invoice new_invoice | |
end | |
def setup_connection | |
@conn = Faraday.new(:url => "#{base_url}" ) do |faraday| | |
faraday.request :url_encoded # form-encode POST params | |
faraday.response :logger # log requests to STDOUT | |
faraday.adapter Faraday.default_adapter # make requests with Net::HTTP | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment