class Traceparts::Client
Constants
- API_BASE
- WEB_SERVICE_PATH
Public Class Methods
new(api_key)
click to toggle source
# File lib/traceparts/client.rb, line 16 def initialize(api_key) @api_key = api_key @connection = Faraday.new(url: API_BASE) do |faraday| faraday.adapter(Faraday.default_adapter) end end
Public Instance Methods
available_formats(classification_id, part_number, user_email)
click to toggle source
# File lib/traceparts/client.rb, line 59 def available_formats(classification_id, part_number, user_email) json = json_available_formats(classification_id, part_number, user_email) json.map { |format| CadFormat.new(self, format) } end
catalogs()
click to toggle source
# File lib/traceparts/client.rb, line 24 def catalogs json_catalogs['classificationList'].map { |catalog| Catalog.new(self, catalog) } end
categories(classification_id)
click to toggle source
# File lib/traceparts/client.rb, line 28 def categories(classification_id) json_categories(classification_id)['categorieList'].map { |category| Category.new(self, category) } end
download_archive_link(classification_id, part_number, cad_format_id, user_email)
click to toggle source
# File lib/traceparts/client.rb, line 65 def download_archive_link(classification_id, part_number, cad_format_id, user_email) response = get_request('DownloadCADPath', { 'PartNumber' => part_number, 'ClassificationID' => classification_id, 'CADFormatID' => cad_format_id, 'UserEmail' => user_email }) json = JSON.parse(response.body) json[1][0][1] end
page_products(classification_id, category_path = nil, page = 1)
click to toggle source
Returns product family id's for specified page
# File lib/traceparts/client.rb, line 33 def page_products(classification_id, category_path = nil, page = 1) json = json_products(classification_id, category_path, page) products_present?(json) ? bind_products(json) : [] end
part(classification_id, part_number, user_email)
click to toggle source
# File lib/traceparts/client.rb, line 78 def part(classification_id, part_number, user_email) Part.new(self, classification_id, part_number, user_email) end
part_details(classification_id, part_number, user_email)
click to toggle source
# File lib/traceparts/client.rb, line 53 def part_details(classification_id, part_number, user_email) json = json_part_details(classification_id, part_number, user_email) PartDetails.new(json) end
products(catalog_id, category_path = nil)
click to toggle source
Returns all product family id's available
# File lib/traceparts/client.rb, line 40 def products(catalog_id, category_path = nil) page = 1 all_products = [] while (products_for_page = page_products(catalog_id, category_path, page)).any? do all_products.push(*products_for_page) page += 1 end all_products end
register_user(user_email, company, country, first_name, last_name, phone)
click to toggle source
# File lib/traceparts/client.rb, line 96 def register_user(user_email, company, country, first_name, last_name, phone) response = get_request('UserRegistration', { 'UserEmail' => user_email, 'company' => company, 'country' => country, 'fname' => first_name, 'name' => last_name, 'phone' => phone }) json = JSON.parse(response.body) json['registered'] == true end
user(user_email)
click to toggle source
# File lib/traceparts/client.rb, line 82 def user(user_email) User.new(self, user_email) end
user_exists?(user_email)
click to toggle source
# File lib/traceparts/client.rb, line 86 def user_exists?(user_email) response = get_request('CheckLogin', { 'UserEmail' => user_email }) json = JSON.parse(response.body) json['registered'] == true end
Private Instance Methods
bind_products(json)
click to toggle source
# File lib/traceparts/client.rb, line 160 def bind_products(json) json['productList'].map { |product| Product.new(self, product) } end
default_params()
click to toggle source
# File lib/traceparts/client.rb, line 176 def default_params { 'ApiKey' => @api_key, 'Format' => 'json', options: { timeout: 50, open_timeout: 50 } } end
get_request(endpoint, params = {})
click to toggle source
# File lib/traceparts/client.rb, line 168 def get_request(endpoint, params = {}) @connection.get("#{WEB_SERVICE_PATH}/#{endpoint}", default_params.merge(params)) end
json_available_formats(classification_id, part_number, user_email)
click to toggle source
# File lib/traceparts/client.rb, line 150 def json_available_formats(classification_id, part_number, user_email) response = get_request('CADFormatsList', { 'PartNumber' => part_number, 'ClassificationID' => classification_id, 'UserEmail' => user_email }) JSON.parse(response.body) end
json_catalogs()
click to toggle source
# File lib/traceparts/client.rb, line 113 def json_catalogs response = get_request('CatalogsList') JSON.parse(response.body) end
json_categories(classification_id)
click to toggle source
# File lib/traceparts/client.rb, line 119 def json_categories(classification_id) response = get_request('CategoriesList', { 'ClassificationId' => classification_id }) JSON.parse(response.body) end
json_part_details(classification_id, part_number, user_email)
click to toggle source
# File lib/traceparts/client.rb, line 140 def json_part_details(classification_id, part_number, user_email) response = get_request('PartNumberData', { 'PartNumber' => part_number, 'ClassificationID' => classification_id, 'UserEmail' => user_email }) JSON.parse(response.body) end
json_products(catalog_id, category_path, page)
click to toggle source
# File lib/traceparts/client.rb, line 127 def json_products(catalog_id, category_path, page) response = get_request('ProductsList', { 'ClassificationID' => catalog_id, 'Path' => category_path, 'Page' => page }) json = response.body return {} unless json && json.length >= 2 JSON.parse(json) end
post_request(endpoint, params = {})
click to toggle source
# File lib/traceparts/client.rb, line 172 def post_request(endpoint, params = {}) @connection.post(endpoint, default_params.merge(params)) end
products_present?(json)
click to toggle source
# File lib/traceparts/client.rb, line 164 def products_present?(json) !json['productList'].nil? end