Class: NewsFetcher::Profile

Inherits:
Object
  • Object
show all
Includes:
SetParams
Defined in:
lib/newsfetcher/profile.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Profile



10
11
12
13
14
# File 'lib/newsfetcher/profile.rb', line 10

def initialize(params={})
  super
  setup_logger
  setup_styles
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



6
7
8
# File 'lib/newsfetcher/profile.rb', line 6

def config
  @config
end

#dirObject

Returns the value of attribute dir.



5
6
7
# File 'lib/newsfetcher/profile.rb', line 5

def dir
  @dir
end

Instance Method Details

#add_subscription(uri:, id: nil, path: nil) ⇒ Object

Raises:



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/newsfetcher/profile.rb', line 72

def add_subscription(uri:, id: nil, path: nil)
  uri = Addressable::URI.parse(uri)
  raise Error, "Bad URI: #{uri}" unless uri.absolute?
  id ||= uri.make_subscription_id
  id = "#{path}/#{id}" if path
  subscription_dir = subscriptions_dir / id
  raise Error, "Subscription already exists in #{subscription_dir}" if subscription_dir.exist?
  subscription_dir.mkpath
  config = @config.make(uri: uri)
  config_file = subscription_dir / ConfigFileName
  config.save(config_file)
  $logger.info { "Saved new subscription to #{config_file}" }
  subscription_dir
end

#all_idsObject



50
51
52
# File 'lib/newsfetcher/profile.rb', line 50

def all_ids
  subscriptions_dir.glob("**/#{ConfigFileName}").map { |p| p.dirname.relative_to(subscriptions_dir).to_s }
end

#config_fileObject



42
43
44
# File 'lib/newsfetcher/profile.rb', line 42

def config_file
  @dir / ConfigFileName
end

#find_subscriptions(ids: nil, status: nil, sort: nil) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/newsfetcher/profile.rb', line 54

def find_subscriptions(ids: nil, status: nil, sort: nil)
  status = [status].flatten.compact if status
  sort ||= :id
  ids = all_ids if ids.nil? || ids.empty?
  subscriptions = ids.map do |id|
    subscription_dir = subscriptions_dir / id
    begin
      subscription_config = @config.load(subscription_dir / ConfigFileName)
    rescue Config::Error => e
      raise "#{id}: #{e}"
    end
    Subscription.new(id: id, dir: subscription_dir, config: subscription_config, styles: @styles)
  end
  subscriptions.
    select { |s| status.nil? || status.include?(s.status) }.
    sort_by { |s| s.send(sort).to_s }
end

#idObject



38
39
40
# File 'lib/newsfetcher/profile.rb', line 38

def id
  @dir.basename.to_s
end

#setup_loggerObject



16
17
18
19
20
21
22
23
# File 'lib/newsfetcher/profile.rb', line 16

def setup_logger
  $logger = Logger.new(STDERR,
    level: @config.log_level,
    formatter: proc { |severity, timestamp, progname, msg|
      "%s %5s: %s\n" % [timestamp.strftime('%FT%T%:z'), severity, msg]
    },
  )
end

#setup_stylesObject

Raises:



25
26
27
28
29
30
31
32
# File 'lib/newsfetcher/profile.rb', line 25

def setup_styles
  raise Error, "dir not set" unless @dir
  @styles = [@config.main_stylesheet, @config.aux_stylesheets].flatten.compact.map do |file|
    file = Path.new(file)
    file = @dir / file if file.relative?
    SassC::Engine.new(file.read, syntax: :scss, style: :compressed).render
  end
end

#subscriptions_dirObject



46
47
48
# File 'lib/newsfetcher/profile.rb', line 46

def subscriptions_dir
  @dir / SubscriptionsDirName
end