Class: WordpressClient::Media

Inherits:
Object
  • Object
show all
Defined in:
lib/wordpress_client/media.rb

Overview

Represents a media record in Wordpress.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id: nil, slug: nil, media_type: nil, title_html: nil, alt_text: nil, description: nil, date: nil, updated_at: nil, guid: nil, link: nil, media_details: {}) ⇒ Media

Creates a new instance, populating the fields with the passed values.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/wordpress_client/media.rb', line 50

def initialize(
  id: nil,
  slug: nil,
  media_type: nil,
  title_html: nil,
  alt_text: nil,
  description: nil,
  date: nil,
  updated_at: nil,
  guid: nil,
  link: nil,
  media_details: {}
)
  @id = id
  @slug = slug
  @media_type = media_type
  @title_html = title_html
  @date = date
  @updated_at = updated_at
  @alt_text = alt_text
  @description = description
  @guid = guid
  @link = link
  @media_details = media_details
end

Instance Attribute Details

#alt_textObject

Returns the value of attribute alt_text.



4
5
6
# File 'lib/wordpress_client/media.rb', line 4

def alt_text
  @alt_text
end

#dateTime?

Returns the date of the media, in UTC if available.

Returns:

  • (Time, nil)

    the date of the media, in UTC if available



20
21
22
# File 'lib/wordpress_client/media.rb', line 20

def date
  @date
end

#descriptionObject

Returns the value of attribute description.



4
5
6
# File 'lib/wordpress_client/media.rb', line 4

def description
  @description
end

#guidString Also known as: source_url

Returns the permalink/GUID – or source_url – of the media.

Media that are embedded in posts have a source_url attribute and no guid, and stand-alone media has a guid but no source_url. They are both backed by the same data, so this method handles both cases, and is aliased to both names.

Returns:

  • (String)

    the permalink/GUID – or source_url – of the media



26
27
28
# File 'lib/wordpress_client/media.rb', line 26

def guid
  @guid
end

#idObject

Returns the value of attribute id.



4
5
6
# File 'lib/wordpress_client/media.rb', line 4

def id
  @id
end

Returns the value of attribute link.



4
5
6
# File 'lib/wordpress_client/media.rb', line 4

def link
  @link
end

#media_detailsHash<String,Object>

Returns the media details if available.

Media details cannot be documented here. It’s up to you to handle this generic “payload” attribute the best way you can.

Returns:

  • (Hash<String,Object>)

    the media details returned from the server



36
37
38
# File 'lib/wordpress_client/media.rb', line 36

def media_details
  @media_details
end

#media_typeString

Returns the type of the media.

Examples:

media.media_type #=> "image"

Returns:

  • (String)

    the type of the media



10
11
12
# File 'lib/wordpress_client/media.rb', line 10

def media_type
  @media_type
end

#slugObject

Returns the value of attribute slug.



4
5
6
# File 'lib/wordpress_client/media.rb', line 4

def slug
  @slug
end

#title_htmlString

Returns the title of the media, HTML escaped.

Examples:

media.title_html #=> "Sunset &mdash; Painting by some person"

Returns:

  • (String)

    the title of the media, HTML escaped



15
16
17
# File 'lib/wordpress_client/media.rb', line 15

def title_html
  @title_html
end

#updated_atTime?

Returns the modification date of the media, in UTC if available.

Returns:

  • (Time, nil)

    the modification date of the media, in UTC if available



23
24
25
# File 'lib/wordpress_client/media.rb', line 23

def updated_at
  @updated_at
end

Class Method Details

.parse(data) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



45
46
47
# File 'lib/wordpress_client/media.rb', line 45

def self.parse(data)
  MediaParser.parse(data)
end

Instance Method Details

#as_imageObject

Returns the same Media instance if it is an image, else nil.



79
80
81
82
83
# File 'lib/wordpress_client/media.rb', line 79

def as_image
  if media_type == "image"
    self
  end
end