module DesignHuddle class Font attr_accessor :font_family_id, :font_family_name, :font_faces, :font_type def initialize(font_family) @font_family_id = font_family["font_family_id"] @font_family_name = font_family["font_family_name"] @font_faces = font_family["font_faces"] @font_type = font_family["font_type"] self end def fetch DesignHuddle.client.call(path: "/partners/api/fonts/#{@font_family_id}") end def self.list DesignHuddle.client.call(path: "/partners/api/fonts")["data"]["items"].map do |font_family| Font.new(font_family) end end # def self.create(file_path:, file_name: nil) # todo: make this work for each font variation # upload_url Fony.upload_font(file_path: file_path, file_name: file_name) # end def self.assign_font(font_family_name:, regular: nil, bold: nil, italic: nil, boldItalic: nil) DesignHuddle.client.call( method: :post, path: "/partners/api/fonts", payload: { font_type: "partner", font_family_name: font_family_name, font_faces: { Regular: regular, Bold: bold, Italic: italic, BoldItalic: boldItalic } }.to_json, headers: { 'Content-Type': "application/json" } ) end def self.get_font_upload_url(file_name) DesignHuddle.client.call( method: :get, path: "/partners/api/font/upload/url", headers: { params: { file_name: file_name } } ) end def self.upload_font(file_path:, file_name: nil) file = file_path.split("/").last name, extension = file.split(".") name = file_name if file_name if (name.length + extension.length + 1) > 30 # +1 for the `.` shortened_name = name[0..29 - extension.length - 1] warn("Font name #{name}.#{extension} is too long. It will be truncated to 30 characters. (#{shortened_name}.#{extension})") name = shortened_name end name = name + "." + extension upload_url = Font.get_font_upload_url(name)["data"]["upload_url"] puts DesignHuddle.client.call( method: :put, url: upload_url, payload: File.read(file_path), headers: { 'Content-Type': "font/#{extension}" } ) upload_url end end end