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.



71
72
73
74
75
76
77
# File 'lib/wp_api_client/relationship.rb', line 71

def initialize(resource, relation)
  if resource["_links"].keys.include? 'curies'
    relation = convert_uri_to_curie(relation)
  end
  @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.



69
70
71
# File 'lib/wp_api_client/relationship.rb', line 69

def relation
  @relation
end

#resourceObject (readonly)

Returns the value of attribute resource.



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

def resource
  @resource
end

Class Method Details

.default_mappingsObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# 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,
    "wp:term" => :term,
    "wp:items" => :terms,
    "wp:post_type" => :post_type,
    "wp:meta" => :meta,
    "wp: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



58
59
60
61
62
63
64
65
# File 'lib/wp_api_client/relationship.rb', line 58

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



54
55
56
# File 'lib/wp_api_client/relationship.rb', line 54

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

.post_type(r) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/wp_api_client/relationship.rb', line 44

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



32
33
34
35
36
37
38
# File 'lib/wp_api_client/relationship.rb', line 32

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



40
41
42
# File 'lib/wp_api_client/relationship.rb', line 40

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

Instance Method Details

#convert_uri_to_curie(uri) ⇒ Object



133
134
135
136
137
138
139
140
141
# File 'lib/wp_api_client/relationship.rb', line 133

def convert_uri_to_curie(uri)
  uri_curie_mappings = {
    "https://api.w.org/term" => "wp:term",
    "https://api.w.org/items" => "wp:items",
    "https://api.w.org/meta" => "wp:meta",
    "https://api.w.org/featuredmedia" => "wp:featuredmedia"
  }
  uri_curie_mappings.dig(uri) || uri
end

#get_relationsObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/wp_api_client/relationship.rb', line 79

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


117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/wp_api_client/relationship.rb', line 117

def load_from_links(relationship, position = nil)
  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

#load_relation(relationship, position = nil) ⇒ Object

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



104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/wp_api_client/relationship.rb', line 104

def load_relation(relationship, position = nil)
  if objects = @resource.dig("_embedded", relationship)
    location = position ? objects[position] : objects
    begin
      WpApiClient::Collection.new(location)
    rescue WpApiClient::ErrorResponse
      load_from_links(relationship, position)
    end
  else
    load_from_links(relationship, position)
  end
end