Class: WikidataService
- Inherits:
-
Object
- Object
- WikidataService
- Defined in:
- app/services/wikidata_service.rb
Instance Method Summary collapse
- #call ⇒ Object
-
#initialize(account) ⇒ WikidataService
constructor
A new instance of WikidataService.
Constructor Details
#initialize(account) ⇒ WikidataService
Returns a new instance of WikidataService.
6 7 8 |
# File 'app/services/wikidata_service.rb', line 6 def initialize(account) @account = account end |
Instance Method Details
#call ⇒ Object
10 11 12 13 14 15 16 17 18 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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'app/services/wikidata_service.rb', line 10 def call return unless @account.wikidata_id.present? endpoint = 'https://query.wikidata.org/sparql' client = SPARQL::Client.new(endpoint, headers: { 'User-Agent' => 'Fat Free CRM' }) query = <<-SPARQL SELECT ?description ?website ?address ?logo ?twitter ?linkedin ?instagram ?mastodon ?facebook ?bluesky ?blog WHERE { BIND(wd:#{@account.wikidata_id} AS ?item) OPTIONAL { ?item schema:description ?description . FILTER(LANG(?description) = "en") } OPTIONAL { ?item wdt:P856 ?website . } OPTIONAL { ?item wdt:P6375 ?address . } OPTIONAL { ?item wdt:P154 ?logo . } OPTIONAL { ?item p:P2002 ?twitter_statement . ?twitter_statement ps:P2002 ?twitter . FILTER NOT EXISTS { ?twitter_statement pq:P582 ?twitter_end_date } } OPTIONAL { ?item p:P4264 ?linkedin_statement . ?linkedin_statement ps:P4264 ?linkedin . FILTER NOT EXISTS { ?linkedin_statement pq:P582 ?linkedin_end_date } } OPTIONAL { ?item p:P2003 ?instagram_statement . ?instagram_statement ps:P2003 ?instagram . FILTER NOT EXISTS { ?instagram_statement pq:P582 ?instagram_end_date } } OPTIONAL { ?item p:P4033 ?mastodon_statement . ?mastodon_statement ps:P4033 ?mastodon . FILTER NOT EXISTS { ?mastodon_statement pq:P582 ?mastodon_end_date } } OPTIONAL { ?item p:P2013 ?facebook_statement . ?facebook_statement ps:P2013 ?facebook . FILTER NOT EXISTS { ?facebook_statement pq:P582 ?facebook_end_date } } OPTIONAL { ?item p:P12361 ?bluesky_statement . ?bluesky_statement ps:P12361 ?bluesky . FILTER NOT EXISTS { ?bluesky_statement pq:P582 ?bluesky_end_date } } OPTIONAL { ?item p:P1581 ?blog_statement . ?blog_statement ps:P1581 ?blog . FILTER NOT EXISTS { ?blog_statement pq:P582 ?blog_end_date } } } SPARQL result = client.query(query).first return unless result updates = {} updates[:background_info] = result[:description].to_s if @account.background_info.blank? && result[:description] updates[:website] = result[:website].to_s if @account.website.blank? && result[:website] updates[:twitter] = "https://twitter.com/#{result[:twitter]}" if @account.twitter.blank? && result[:twitter] updates[:linkedin] = "https://www.linkedin.com/company/#{result[:linkedin]}" if @account.linkedin.blank? && result[:linkedin] updates[:instagram] = "https://www.instagram.com/#{result[:instagram]}" if @account.instagram.blank? && result[:instagram] if @account.mastodon.blank? && result[:mastodon] m = result[:mastodon].to_s updates[:mastodon] = if m.start_with?("http") m elsif m =~ /^@?([^@]+)@(.+)$/ "https://#{Regexp.last_match(2)}/@#{Regexp.last_match(1)}" else m end end updates[:facebook] = "https://www.facebook.com/#{result[:facebook]}" if @account.facebook.blank? && result[:facebook] updates[:bluesky] = "https://bsky.app/profile/#{result[:bluesky]}" if @account.bluesky.blank? && result[:bluesky] updates[:blog] = result[:blog].to_s if @account.blog.blank? && result[:blog] if updates.any? @account.assign_attributes(updates) @account.save(validate: false) end end |