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

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

Direct Known Subclasses

Redis::Provider

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#feedObject

Returns the value of attribute feed.



17
18
19
# File 'lib/simplefeed/providers/base/provider.rb', line 17

def feed
  @feed
end

Class Method Details

.class_to_registry(klass) ⇒ Object



19
20
21
# File 'lib/simplefeed/providers/base/provider.rb', line 19

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)



66
67
68
69
70
71
72
# File 'lib/simplefeed/providers/base/provider.rb', line 66

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)



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

def batch_size
  feed.batch_size
end

#key(user_id) ⇒ Object (protected)



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

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

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



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/simplefeed/providers/base/provider.rb', line 29

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 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)



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

def tap(value)
  yield
  value
end

#to_array(user_ids) ⇒ Object (protected)



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

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

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

Yields:

  • (response)


74
75
76
77
78
79
80
81
82
83
84
# File 'lib/simplefeed/providers/base/provider.rb', line 74

def with_response(response = nil)
  response ||= SimpleFeed::Response.new
  yield(response)
  if 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)



58
59
60
61
62
63
64
# File 'lib/simplefeed/providers/base/provider.rb', line 58

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