Class: TapeDiscoveryService

Inherits:
Object
  • Object
show all
Defined in:
app/services/tape_discovery_service.rb

Constant Summary collapse

CONTENT_TYPES =
[
  'application/x.atom+xml',
  'application/atom+xml',
  'application/xml',
  'text/xml',
  'application/rss+xml',
  'application/rdf+xml',
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ TapeDiscoveryService

Returns a new instance of TapeDiscoveryService.



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
# File 'app/services/tape_discovery_service.rb', line 12

def initialize(url)
  @url_uri = URI.parse(url)
  url = "#{ @url_uri.scheme or 'http' }://#{ @url_uri.host }#{ @url_uri.path }"
  url << "?#{ @url_uri.query }" if @url_uri.query

  begin
    html = Nokogiri::HTML(open(url))

  rescue RuntimeError
    if url.start_with? 'http://'
      url.gsub!('http://', 'https://')
      html = Nokogiri::HTML(open(url))

    # TODO: add anther option for https://www for naked subdomain
    else
      url  = ''
      html = ''

    end

  rescue SocketError
    ap "Bad server address: #{ url }"
    url  = ''
    html = ''

  rescue Errno::ECONNREFUSED
    ap "Scheme is not supported: #{ url }"
    url  = ''
    html = ''

  end

  @url  = url
  @html = html
end

Instance Method Details

#fetch_iconObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'app/services/tape_discovery_service.rb', line 70

def fetch_icon
  api_url = "http://icons.better-idea.org/allicons.json?pretty=true&url="
  icon_url = ''

  json = Net::HTTP.get(URI(api_url + @url))
  data = JSON.parse(json)
  icon = data['icons'].first

  if icon
    icon_url = icon.fetch('url', '')
  end

  return icon_url
end

#fetch_subscriptionObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/services/tape_discovery_service.rb', line 49

def fetch_subscription
  if @url.empty?
    return nil
  end

  icon_url = fetch_icon()

  ap icon_url

  @subscription = TapeSubscription.new({
    title:            title,
    website_url:      @url,
    website_icon_url: icon_url
  })

  add_feeds()

  return @subscription
end