Class: WpApiClient::Relationship

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

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource, relation) ⇒ Relationship

Returns a new instance of Relationship.



66
67
68
69
# File 'lib/wp_api_client/relationship.rb', line 66

def initialize(resource, relation)
  @resource = resource
  @relation = relation
end

Class Attribute Details

.mappingsObject



9
10
11
# File 'lib/wp_api_client/relationship.rb', line 9

def mappings
  @mappings ||= default_mappings
end

Instance Attribute Details

#relationObject (readonly)

Returns the value of attribute relation.



64
65
66
# File 'lib/wp_api_client/relationship.rb', line 64

def relation
  @relation
end

#resourceObject (readonly)

Returns the value of attribute resource.



63
64
65
# File 'lib/wp_api_client/relationship.rb', line 63

def resource
  @resource
end

Class Method Details

.default_mappingsObject



17
18
19
20
21
22
23
24
25
# File 'lib/wp_api_client/relationship.rb', line 17

def default_mappings
  {
    "https://api.w.org/term" => :term,
    "https://api.w.org/items" => :terms,
    "http://api.w.org/v2/post_type" => :post_type,
    "https://api.w.org/meta" => :meta,
    "https://api.w.org/featuredmedia" => :post
  }
end

.define(relation, type) ⇒ Object



13
14
15
# File 'lib/wp_api_client/relationship.rb', line 13

def define(relation, type)
  mappings[relation] = type
end

.meta(r) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/wp_api_client/relationship.rb', line 53

def meta(r)
  relations = {}
  meta = WpApiClient.get_client.get(r.resource["_links"][r.relation].first["href"])
  meta.map do |m|
    relations.merge! Hash[m.key, m.value]
  end
  relations
end

.post(r) ⇒ Object



49
50
51
# File 'lib/wp_api_client/relationship.rb', line 49

def post(r)
  r.load_relation(r.relation)
end

.post_type(r) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/wp_api_client/relationship.rb', line 39

def post_type(r)
  relations = {}
  r.resource["_links"][r.relation].each_with_index do |link, position|
    # get the post type out of the linked URL.
    post_type = URI.parse(link["href"]).path.split('wp/v2/').pop.split('/').first
    relations.merge! Hash[post_type, r.load_relation(r.relation, position)]
  end
  relations
end

.term(r) ⇒ Object



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

def term(r)
  relations = {}
  r.resource["_links"][r.relation].each_with_index do |link, position|
    relations.merge! Hash[link["taxonomy"], r.load_relation(r.relation, position)]
  end
  relations
end

.terms(r) ⇒ Object



35
36
37
# File 'lib/wp_api_client/relationship.rb', line 35

def terms(r)
  r.load_relation(r.relation, 0)
end

Instance Method Details

#get_relationsObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/wp_api_client/relationship.rb', line 71

def get_relations
  mapping = self.class.mappings[@relation]
  if !mapping
    raise WpApiClient::RelationNotDefined.new %{
=> The relation "#{@relation}" is not defined.

To add a new relation, define it at configuration. For example, to define this
relation as one that links to a post object, you would do the following.

WpApiClient.configure do |c|
  c.define_mapping(#{@relation}, :post)
end

The currently defined relations are:

#{self.class.mappings.keys.join("\n") }

Available mappings are :post, :term, and :meta.}
  end

  # Only try to fetch the relation if there are any links to it
  self.class.send(mapping, self) if resource["_links"][relation]
end

#load_relation(relationship, position = nil) ⇒ Object

try to load an embedded object; call out to the API if not



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/wp_api_client/relationship.rb', line 96

def load_relation(relationship, position = nil)
  if objects = @resource.dig("_embedded", relationship)
    location = position ? objects[position] : objects
    WpApiClient::Collection.new(location)
  else
    unless position.nil?
      location = @resource["_links"].dig(relationship, position.to_i, "href")
    else
      if @resource["_links"][relationship].is_a? Array
        # If the resources are linked severally, crank through and
        # retrieve them one by one as an array
        return @resource["_links"][relationship].map { |link| WpApiClient.get_client.get(link["href"]) }
      else
        # Otherwise, get the single link to the lot
        location = @resource["_links"][relationship]["href"]
      end
    end
    WpApiClient.get_client.get(location) if location
  end
end