Class: WpApiClient::Entities::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/wp_api_client/entities/base.rb

Direct Known Subclasses

Image, Meta, Post, Taxonomy, Term

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource) ⇒ Base

Returns a new instance of Base.



15
16
17
18
19
20
# File 'lib/wp_api_client/entities/base.rb', line 15

def initialize(resource)
  unless resource.is_a? Hash or resource.is_a? OpenStruct
    raise ArgumentError.new('Tried to initialize a WP-API resource with something other than a Hash')
  end
  @resource = OpenStruct.new(resource)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



36
37
38
# File 'lib/wp_api_client/entities/base.rb', line 36

def method_missing(method, *args)
  @resource.send(method, *args)
end

Instance Attribute Details

#resourceObject (readonly)

Returns the value of attribute resource.



7
8
9
# File 'lib/wp_api_client/entities/base.rb', line 7

def resource
  @resource
end

Class Method Details

.build(resource) ⇒ Object

Raises:

  • (Exception)


9
10
11
12
13
# File 'lib/wp_api_client/entities/base.rb', line 9

def self.build(resource)
  raise Exception if resource.nil?
  type = WpApiClient::Entities::Types.find { |type| type.represents?(resource) }
  type.new(resource)
end

Instance Method Details



22
23
24
# File 'lib/wp_api_client/entities/base.rb', line 22

def links
  resource["_links"]
end

#relations(relation, relation_to_return = nil) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/wp_api_client/entities/base.rb', line 26

def relations(relation, relation_to_return = nil)
  relationship = Relationship.new(@resource, relation)
  relations = relationship.get_relations
  if relation_to_return
    relations[relation_to_return]
  else
    relations
  end
end