Class: SimpleFeed::Response

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/simplefeed/response.rb

Instance Method Summary collapse

Constructor Details

#initialize(data = {}) ⇒ Response

Returns a new instance of Response.



25
26
27
# File 'lib/simplefeed/response.rb', line 25

def initialize(data = {})
  @result = data.dup
end

Instance Method Details

#eachObject



15
16
17
18
19
20
21
22
23
# File 'lib/simplefeed/response.rb', line 15

def each
  if block_given?
    @result.each_pair do |user_id, result|
      yield(user_id, result)
    end
  else
    @result.keys.to_enum
  end
end

#for(key_or_user_id, result = nil) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/simplefeed/response.rb', line 29

def for(key_or_user_id, result = nil)
  user_id = key_or_user_id.is_a?(SimpleFeed::Providers::Key) ?
    key_or_user_id.user_id :
    key_or_user_id

  @result[user_id] = result || yield(@result[user_id])
end

#has_user?(user_id) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/simplefeed/response.rb', line 41

def has_user?(user_id)
  @result.key?(user_id)
end

#result(user_id = nil) ⇒ Object Also known as: []



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/simplefeed/response.rb', line 65

def result(user_id = nil)
  if user_id
    @result[user_id]
  else
    if @result.values.size == 1
      @result.values.first
    else
      @result.to_hash
    end
  end
end

#to_hObject



49
50
51
# File 'lib/simplefeed/response.rb', line 49

def to_h
  @result.to_h
end

#transformObject

Passes results assigned to each user to a transformation function that in turn must return a transformed value for an individual response, and be implemented in the subclasses



56
57
58
59
60
61
62
63
# File 'lib/simplefeed/response.rb', line 56

def transform
  if block_given?
    @result.each_pair do |user_id, value|
      @result[user_id] = yield(user_id, value)
    end
  end
  self
end

#user_countObject



45
46
47
# File 'lib/simplefeed/response.rb', line 45

def user_count
  @result.size
end

#user_idsObject



37
38
39
# File 'lib/simplefeed/response.rb', line 37

def user_ids
  @result.keys
end