Class: Discodactyl::CLI::Format

Inherits:
Object
  • Object
show all
Defined in:
lib/discodactyl/cli/format.rb

Overview

Formatting helpers for various rels

Class Method Summary collapse

Class Method Details

.activities_from_uri(uri, io) ⇒ Object


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/discodactyl/cli/format.rb', line 49

def self.activities_from_uri(uri, io)
  Discodactyl.log.debug("Activity feed lookup for #{uri}") if Discodactyl.log
  begin
    if uri && (feed = Feedzirra::Feed.fetch_and_parse(uri)) && feed.respond_to?(:entries)
      Array(feed.entries).each do |entry|
        include ActionView::Helpers::DateHelper
        silence_warnings do
          io.puts "Status: #{Loofah::Helpers.strip_tags(entry.content)} #{time_ago_in_words(entry.published)} ago"
        end
      end
    else
      Discodactyl.log.debug("Failed activity feed lookup with uri: #{uri.inspect} feed: #{feed.inspect}") if Discodactyl.log
    end
  rescue
    Discodactyl.log.warn($!) if Discodactyl.log
  end
end

.for_rel(rel) ⇒ Object


5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/discodactyl/cli/format.rb', line 5

def self.for_rel(rel)
  case rel
  when 'http://microformats.org/profile/hcard' then
    lambda {|uri, io| Discodactyl::CLI::Format.hcard_from_uri(uri, io) }
  when 'http://schemas.google.com/g/2010#updates-from' then
    lambda {|uri, io| Discodactyl::CLI::Format.activities_from_uri(uri, io) }
  else
    if name = Discodactyl::KNOWN_RELS[rel]
      lambda {|uri, io| io.puts "#{name}: #{uri}" if uri }
    else
      lambda {|uri, io| }
    end
  end
end

.hcard_adr(adr) ⇒ Object


20
21
22
23
24
25
26
27
28
# File 'lib/discodactyl/cli/format.rb', line 20

def self.hcard_adr(adr)
  segments = []
  segments << adr.street_address if adr.properties.include?(:street_address)
  segments << adr.locality if adr.properties.include?(:locality)
  segments << adr.region if adr.properties.include?(:region)
  segments << adr.postal_code if adr.properties.include?(:postal_code)
  segments << adr.country_name if adr.properties.include?(:country_name)
  segments.join(' ')
end

.hcard_from_uri(uri, io) ⇒ Object


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/discodactyl/cli/format.rb', line 30

def self.hcard_from_uri(uri, io)
  begin
    Discodactyl.log.debug("hCard lookup for #{uri}") if Discodactyl.log
    if hcards = Prism.find(uri, :hcard)
      hcards.each do |hcard|
        if hcard
          io << "Name: #{hcard.fn.gsub(/\s+/, ' ')}\n" if hcard.properties.include?(:fn)
          io << "Title: #{hcard.title}\n" if hcard.properties.include?(:title)
          io << "Organization: #{hcard.org}\n" if hcard.properties.include?(:org)
          io << "Address: #{hcard_adr(hcard.adr)}\n" if hcard.properties.include?(:adr)
        end
      end
    end
  rescue RuntimeError => e
    Discodactyl.log.warn(e.message) if Discodactyl.log
    Discodactyl.log.debug(e) if Discodactyl.log
  end
end