Skip to content

Commit

Permalink
Merge pull request #27 from nickel/import
Browse files Browse the repository at this point in the history
Add remote picto import
  • Loading branch information
nickel authored Apr 8, 2024
2 parents 6347046 + 4d77b8c commit 5e1ca7b
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 5 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ gem "stimulus-rails"
gem "tailwindcss-rails"
gem "turbo-rails"

gem "faraday"

group :development, :test do
gem "debug", platforms: %i(mri windows)
end
Expand Down
13 changes: 13 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ GEM
devise (>= 4.9.0)
drb (2.2.1)
erubi (1.12.0)
faraday (2.9.0)
faraday-net_http (>= 2.0, < 3.2)
faraday-net_http (3.1.0)
net-http
ffi (1.16.3)
foreman (0.87.2)
globalid (1.2.1)
Expand Down Expand Up @@ -173,6 +177,8 @@ GEM
minitest (5.22.3)
msgpack (1.7.2)
mutex_m (0.2.0)
net-http (0.4.1)
uri
net-imap (0.4.10)
date
net-protocol
Expand All @@ -185,6 +191,8 @@ GEM
nio4r (2.7.0)
nokogiri (1.16.3-aarch64-linux)
racc (~> 1.4)
nokogiri (1.16.3-arm64-darwin)
racc (~> 1.4)
nokogiri (1.16.3-x86_64-linux)
racc (~> 1.4)
orm_adapter (0.5.0)
Expand Down Expand Up @@ -296,6 +304,8 @@ GEM
stringio (3.1.0)
tailwindcss-rails (2.4.0-aarch64-linux)
railties (>= 6.0.0)
tailwindcss-rails (2.4.0-arm64-darwin)
railties (>= 6.0.0)
tailwindcss-rails (2.4.0-x86_64-linux)
railties (>= 6.0.0)
thor (1.3.1)
Expand All @@ -307,6 +317,7 @@ GEM
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
unicode-display_width (2.5.0)
uri (0.13.0)
warden (1.2.9)
rack (>= 2.0.9)
web-console (4.2.1)
Expand All @@ -325,6 +336,7 @@ GEM

PLATFORMS
aarch64-linux
arm64-darwin-23
x86_64-linux

DEPENDENCIES
Expand All @@ -335,6 +347,7 @@ DEPENDENCIES
debug
devise
devise-i18n
faraday
foreman
image_processing
importmap-rails
Expand Down
6 changes: 4 additions & 2 deletions app/packages/planning/controllers/plans_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,11 @@ def activate
end

def current
@plan = Plan::FindCurrent.call(
Plan::FindCurrent.call(
account_id: current_account.id
).value
).and_then do |plan|
@plan = plan
end
end

def destroy
Expand Down
31 changes: 31 additions & 0 deletions lib/api_clients/arasaac.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

module ApiClients
class Arasaac
BASE_URL = "https://api.arasaac.org"

def all_pictograms
connection.get("/v1/pictograms/all/es").body.map do |picto|
id = picto["_id"]

CustomStruct.new(
id:,
keyword: picto["keywords"].first["keyword"],
data: picto,
image_location: "https://static.arasaac.org/pictograms/#{id}/#{id}_2500.png"
)
end
end

def agent
@agent ||= Faraday.new(url: BASE_URL) do |builder|
builder.response :json
end
end

alias connection agent

delegate :get, :post, :put, :delete, :head, :patch, :options, :trace,
:connect, :headers, :in_parallel, :in_parallel?, to: :agent
end
end
35 changes: 32 additions & 3 deletions lib/tasks/pictos/import.rake
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# frozen_string_literal: true

namespace :pictos do
desc "Import pictos"
task import: :environment do
desc "Local import pictos"
task local_import: :environment do
Dir.foreach(Rails.root.join("vendor/pictos")) do |filename|
next if [".", ".."].include?(filename)

Expand All @@ -15,9 +15,38 @@ namespace :pictos do

next if Picto.exists?(external_id:, external_source:)

Picto::Create.call(keyword:, external_id:, external_source:, active: false, image:, data:)
Picto::Create.call(keyword:, external_id:, external_source:, enabled: false, image:, data:)
rescue StandardError
next
end
end

desc "Remove import pictos"
task remote_import: :environment do
require "open-uri"

pictograms = ApiClients::Arasaac
.new
.all_pictograms

pictograms.each do |picto|
external_id = picto.id
external_source = "arasaac"

next if Picto.exists?(external_id:, external_source:)

image = URI.parse(picto.image_location).open

Picto::Create.call(
external_id:,
external_source:,
image:,
keyword: picto.keyword,
data: picto.data,
enabled: false
)

puts "Picto ##{external_id}: #{picto.keyword}"
end
end
end

0 comments on commit 5e1ca7b

Please sign in to comment.