Class: Tweetkit::Response::Resources

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/tweetkit/response.rb

Defined Under Namespace

Classes: Resource

Constant Summary collapse

VALID_RESOURCES =
Set['users', 'tweets', 'media']

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resources) ⇒ Resources

Returns a new instance of Resources.



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

def initialize(resources)
  @resources = resources
  build_and_normalize_resources(resources) unless resources.nil?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, **args) ⇒ Object



39
40
41
42
43
# File 'lib/tweetkit/response.rb', line 39

def method_missing(method, **args)
  return nil if VALID_RESOURCES.include?(method.to_s)

  super
end

Instance Attribute Details

#resourcesObject

Returns the value of attribute resources.



20
21
22
# File 'lib/tweetkit/response.rb', line 20

def resources
  @resources
end

Instance Method Details

#build_and_normalize_resource(resource, resource_type) ⇒ Object



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

def build_and_normalize_resource(resource, resource_type)
  Tweetkit::Response::Resources::Resource.new(resource, resource_type)
end

#build_and_normalize_resources(resources) ⇒ Object



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

def build_and_normalize_resources(resources)
  resources.each_key do |resource_type|
    normalized_resource = build_and_normalize_resource(@resources[resource_type], resource_type)
    instance_variable_set(:"@#{resource_type}", normalized_resource)
    self.class.define_method(resource_type) { instance_variable_get("@#{resource_type}") }
  end
end

#respond_to_missing?(method, *args) ⇒ Boolean

Returns:

  • (Boolean)


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

def respond_to_missing?(method, *args)
  VALID_RESOURCES.include?(method.to_s) || super
end