Class: SimpleFeed::Providers::Base::Provider

Inherits:
Object
  • Object
show all
Defined in:
lib/simplefeed/providers/base/provider.rb

Direct Known Subclasses

Hash::Provider, Redis::Provider

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#feedObject

Returns the value of attribute feed.



13
14
15
# File 'lib/simplefeed/providers/base/provider.rb', line 13

def feed
  @feed
end

Class Method Details

.class_to_registry(klass) ⇒ Object



15
16
17
# File 'lib/simplefeed/providers/base/provider.rb', line 15

def self.class_to_registry(klass)
  klass.name.split('::').delete_if { |n| %w(SimpleFeed Providers Provider).include?(n) }.compact.last.downcase.to_sym
end

Instance Method Details

#batch(user_ids) ⇒ Object (protected)



62
63
64
65
66
67
68
# File 'lib/simplefeed/providers/base/provider.rb', line 62

def batch(user_ids)
  to_array(user_ids).each_slice(batch_size) do |batch|
    batch.each do |user_id|
      yield(key(user_id))
    end
  end
end

#batch_sizeObject (protected)



50
51
52
# File 'lib/simplefeed/providers/base/provider.rb', line 50

def batch_size
  feed.batch_size
end

#key(user_id) ⇒ Object (protected)



42
43
44
# File 'lib/simplefeed/providers/base/provider.rb', line 42

def key(user_id)
  feed.key(user_id)
end

#reset_last_read_value(user_ids:, at: nil) ⇒ Object (protected)



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/simplefeed/providers/base/provider.rb', line 25

def reset_last_read_value(user_ids:, at: nil)
  at = [Time, DateTime, Date].include?(at.class) ? at : Time.now
  at = at.to_time if at.respond_to?(:to_time)
  at = at.to_f if at.respond_to?(:to_f)

  if self.respond_to?(:reset_last_read)
    reset_last_read(user_ids: user_ids, at: at)
  else
    raise ArgumentError, "Class #{self.class} does not implement #reset_last_read method"
  end
end

#tap(value) ⇒ Object (protected)



37
38
39
40
# File 'lib/simplefeed/providers/base/provider.rb', line 37

def tap(value)
  yield
  value
end

#to_array(user_ids) ⇒ Object (protected)



46
47
48
# File 'lib/simplefeed/providers/base/provider.rb', line 46

def to_array(user_ids)
  user_ids.is_a?(Array) ? user_ids : [user_ids]
end

#with_response(response = nil) {|response| ... } ⇒ Object (protected)

Yields:

  • (response)


70
71
72
73
74
75
76
77
78
79
80
# File 'lib/simplefeed/providers/base/provider.rb', line 70

def with_response(response = nil)
  response ||= SimpleFeed::Response.new
  yield(response)
  if self.respond_to?(:transform_response)
    response.transform do |user_id, result|
      # calling into a subclass
      transform_response(user_id, result)
    end
  end
  response
end

#with_response_batched(user_ids, external_response = nil) ⇒ Object (protected)



54
55
56
57
58
59
60
# File 'lib/simplefeed/providers/base/provider.rb', line 54

def with_response_batched(user_ids, external_response = nil)
  with_response(external_response) do |response|
    batch(user_ids) do |key|
      response.for(key.user_id) { yield(key, response) }
    end
  end
end