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.


23
24
25
# File 'lib/simplefeed/response.rb', line 23

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

Instance Method Details

#eachObject


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

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


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

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 ? result : yield(@result[user_id])
end

#has_user?(user_id) ⇒ Boolean

Returns:

  • (Boolean)

39
40
41
# File 'lib/simplefeed/response.rb', line 39

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

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


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

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

#to_hObject


47
48
49
# File 'lib/simplefeed/response.rb', line 47

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


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

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


43
44
45
# File 'lib/simplefeed/response.rb', line 43

def user_count
  @result.size
end

#user_idsObject


35
36
37
# File 'lib/simplefeed/response.rb', line 35

def user_ids
  @result.keys
end