Class: FigmaClient
- Inherits:
-
Object
- Object
- FigmaClient
- Defined in:
- lib/admiral-tools-figma/helper/figma/figma_client/figma_client.rb
Constant Summary collapse
- TOKEN_HEADER_NAME =
'X-FIGMA-TOKEN'
- CONTENT_TYPE_HEADER_NAME =
'Content-Type'
- CONTENT_TYPE_HEADER_VALUE =
'application/json'
- BASE_URL_DEFAULT =
'https://api.figma.com/v1'
Instance Method Summary collapse
- #get_components(file_key:) ⇒ Object
- #get_full_image_components(file_key:, image_format:, scales:, name_filter_regex:, desc_filter_regex: nil, frame_filter_regex: nil, page_filter_regex: nil, request_batch: nil, svg_include_id: nil, svg_simplify_stroke: nil, use_absolute_bounds: nil) ⇒ Object
- #get_full_styles(file_key:, request_batch:) ⇒ Object
- #get_images(file_key:, node_ids:, image_format:, scale:, use_absolute_bounds:, svg_include_id:, svg_simplify_stroke:) ⇒ Object
- #get_nodes(file_key:, node_ids:, depth: 1) ⇒ Object
- #get_styles(file_key:) ⇒ Object
-
#initialize(access_token:, base_url: nil) ⇒ FigmaClient
constructor
A new instance of FigmaClient.
Constructor Details
#initialize(access_token:, base_url: nil) ⇒ FigmaClient
Returns a new instance of FigmaClient.
14 15 16 17 |
# File 'lib/admiral-tools-figma/helper/figma/figma_client/figma_client.rb', line 14 def initialize(access_token:, base_url: nil) @access_token = access_token @base_url = base_url || BASE_URL_DEFAULT end |
Instance Method Details
#get_components(file_key:) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/admiral-tools-figma/helper/figma/figma_client/figma_client.rb', line 83 def get_components(file_key:) uri = URI.parse("#{@base_url}/files/#{file_key}/components") req = create_request(uri) http = create_http_client(uri) res = http.start do |h| h.request(req) end raise "HTTP Status Code: #{res.code}\n#{res.body}" unless res.is_a?(Net::HTTPSuccess) json_data = JSON.parse(res.body) FigmaComponentsStylesResult.from_hash(json_data) end |
#get_full_image_components(file_key:, image_format:, scales:, name_filter_regex:, desc_filter_regex: nil, frame_filter_regex: nil, page_filter_regex: nil, request_batch: nil, svg_include_id: nil, svg_simplify_stroke: nil, use_absolute_bounds: nil) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/admiral-tools-figma/helper/figma/figma_client/figma_client.rb', line 19 def get_full_image_components( file_key:, image_format:, scales:, name_filter_regex:, desc_filter_regex: nil, frame_filter_regex: nil, page_filter_regex: nil, request_batch: nil, svg_include_id: nil, svg_simplify_stroke: nil, use_absolute_bounds: nil ) converter = FigmaToDomainConverter.new components_result = get_components(file_key: file_key) figma_components = components_result&.&.components || [] scales = [1.0] if scales.nil? || scales.empty? unless name_filter_regex.nil? figma_components = figma_components.select { |c| c.name[Regexp.new(name_filter_regex)] } end unless desc_filter_regex.nil? figma_components = figma_components.select { |c| c.description[Regexp.new(desc_filter_regex)] } end unless frame_filter_regex.nil? figma_components = figma_components.select { |c| c.containing_frame&.name[Regexp.new(frame_filter_regex)] } end unless page_filter_regex.nil? figma_components = figma_components.select { |c| c.containing_frame&.page_name[Regexp.new(page_filter_regex)] } end node_ids = figma_components.map(&:node_id) node_id_batches = node_ids.each_slice(request_batch || 1_000_000).to_a image_links_hash = {} scales.each do |scale| images_all = {} node_id_batches.each do |batch_node_ids| images = get_images( file_key: file_key, node_ids: batch_node_ids, image_format: image_format, scale: scale, svg_include_id: svg_include_id, svg_simplify_stroke: svg_simplify_stroke, use_absolute_bounds: use_absolute_bounds )&.images || {} images_all.merge!(images) end image_links_hash[scale] = images_all end components = converter.fulfill_components( figma_components: figma_components, image_links_hash: image_links_hash, image_format: image_format ) ComponentsList.new(components: components) end |
#get_full_styles(file_key:, request_batch:) ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/admiral-tools-figma/helper/figma/figma_client/figma_client.rb', line 123 def get_full_styles(file_key:, request_batch:) converter = FigmaToDomainConverter.new styles_result = get_styles(file_key: file_key) node_ids = (styles_result&.&.styles || []).map(&:node_id) node_id_batches = node_ids.each_slice(request_batch || 1_000_000).to_a nodes_result = FigmaNodesResult.new(name: nil, nodes: {}) node_id_batches.each do |batch_node_ids| result = get_nodes(file_key: file_key, node_ids: batch_node_ids, depth: 1) nodes_result.name = result.name nodes_result.nodes.merge!(result.nodes) end converter.fulfill_styles(figma_nodes_result: nodes_result, figma_styles_result: styles_result) end |
#get_images(file_key:, node_ids:, image_format:, scale:, use_absolute_bounds:, svg_include_id:, svg_simplify_stroke:) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/admiral-tools-figma/helper/figma/figma_client/figma_client.rb', line 99 def get_images(file_key:, node_ids:, image_format:, scale:, use_absolute_bounds:, svg_include_id:, svg_simplify_stroke:) uri = URI.parse("#{@base_url}/images/#{file_key}") params = {} params['ids'] = node_ids.join(',') params['format'] = image_format unless image_format.nil? params['scale'] = scale unless scale.nil? params['use_absolute_bounds'] = use_absolute_bounds unless use_absolute_bounds.nil? params['svg_include_id'] = svg_include_id unless svg_include_id.nil? params['svg_simplify_stroke'] = svg_simplify_stroke unless svg_simplify_stroke.nil? add_query_parameters(uri: uri, params: params) req = create_request(uri) http = create_http_client(uri) res = http.start do |h| h.request(req) end raise "HTTP Status Code: #{res.code}\n#{res.body}" unless res.is_a?(Net::HTTPSuccess) json_data = JSON.parse(res.body) FigmaImagesResult.from_hash(json_data) end |
#get_nodes(file_key:, node_ids:, depth: 1) ⇒ Object
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/admiral-tools-figma/helper/figma/figma_client/figma_client.rb', line 156 def get_nodes(file_key:, node_ids:, depth: 1) uri = URI.parse("#{@base_url}/files/#{file_key}/nodes") node_ids_string = node_ids.join(',') params = { "ids": node_ids_string, "depth": depth } add_query_parameters(uri: uri, params: params) req = create_request(uri) http = create_http_client(uri) res = http.start do |h| h.request(req) end raise "HTTP Status Code: #{res.code}\n#{res.body}" unless res.is_a?(Net::HTTPSuccess) json_data = JSON.parse(res.body) FigmaNodesResult.from_hash(json_data) end |
#get_styles(file_key:) ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/admiral-tools-figma/helper/figma/figma_client/figma_client.rb', line 140 def get_styles(file_key:) uri = URI.parse("#{@base_url}/files/#{file_key}/styles") req = create_request(uri) http = create_http_client(uri) res = http.start do |h| h.request(req) end raise "HTTP Status Code: #{res.code}\n#{res.body}" unless res.is_a?(Net::HTTPSuccess) json_data = JSON.parse(res.body) FigmaComponentsStylesResult.from_hash(json_data) end |