Welcome to mirror list, hosted at ThFree Co, Russian Federation.

diaspora_link_service.rb « services « app - github.com/diaspora/diaspora.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1923aa01d409a09c6d9402127b399e1292b6b567 (plain)
1
2
3
4
5
6
7
8
9
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
# frozen_string_literal: true

# Encapsulates logic of processing diaspora:// links
class DiasporaLinkService
  attr_reader :type, :author, :guid

  def initialize(link)
    @link = link.dup
    parse
  end

  def find_or_fetch_entity
    entity_finder.find || fetch_entity
  end

  private

  attr_accessor :link

  def fetch_entity
    DiasporaFederation::Federation::Fetcher.fetch_public(author, type, guid)
    entity_finder.find
  rescue DiasporaFederation::Federation::Fetcher::NotFetchable
    nil
  end

  def entity_finder
    @entity_finder ||= Diaspora::EntityFinder.new(type, guid)
  end

  def normalize
    link.gsub!(%r{^web\+diaspora://}, "diaspora://") ||
      link.gsub!(%r{^//}, "diaspora://") ||
      %r{^diaspora://}.match(link) ||
      self.link = "diaspora://#{link}"
  end

  def parse
    normalize
    match = DiasporaFederation::Federation::DiasporaUrlParser::DIASPORA_URL_REGEX.match(link)
    @author = match[1]
    @type = match[2]
    @guid = match[3]
  end
end