mastodon-cpp  0.30.1
Public Member Functions | List of all members
Mastodon::Easy::Attachment Class Reference

Class to hold attachments. More...

#include <attachment.hpp>

Inheritance diagram for Mastodon::Easy::Attachment:
Mastodon::Easy::Entity

Public Member Functions

 Attachment (const string &json)
 Constructs an Attachment object from a JSON string. More...
 
 Attachment ()
 Constructs an empty Attachment object. More...
 
virtual bool valid () const
 Returns true if the Entity holds valid data. More...
 
double aspect () const
 Aspect of original image. More...
 
double aspect_small () const
 Aspect of preview image. More...
 
uint_fast64_t bitrate () const
 Returns the bitrate of a video. More...
 
const string description () const
 Returns the image description. More...
 
Attachment description (const string &description)
 Sets the image description. More...
 
const std::chrono::duration< double > duration () const
 Returns the duration of a video in seconds. More...
 
const string file () const
 Gets file to upload. More...
 
Attachment file (const string &file)
 Sets file to upload. More...
 
const std::array< double, 2 > focus () const
 Returns the focus point (x, y) More...
 
Attachment focus (const std::array< double, 2 > &focus)
 Sets the focus point (x, y) More...
 
double framerate () const
 Returns the framerate of a video in frames per second. More...
 
uint_fast64_t height () const
 Returns the height of the original image. More...
 
uint_fast64_t height_small () const
 Returns the height of the preview image. More...
 
const string id () const
 Returns the ID of the attachment. More...
 
const string preview_url () const
 Returns the URL of the preview image. More...
 
const string remote_url () const
 Returns the remote URL of the original image. More...
 
const string size () const
 Returns the size of the original image. More...
 
const string size_small () const
 Returns the size of the preview image. More...
 
const string text_url () const
 Returns shorter URL for the image. More...
 
attachment_type type () const
 Returns attachment type. More...
 
const string url () const
 Returns URL of the locally hosted version of the image. More...
 
uint_fast64_t width () const
 Returns the width of the original image. More...
 
uint_fast64_t width_small () const
 Returns the width of the preview image. More...
 
- Public Member Functions inherited from Mastodon::Easy::Entity
 Entity (const string &json)
 Constructs an Entity object from a JSON string. More...
 
 Entity ()
 Constructs an empty Entity object. More...
 
void from_string (const string &json)
 Replaces the Entity with a new one from a JSON string. More...
 
const Json::Value to_object () const
 Returns the JSON object of the Entity. More...
 
const string error () const
 Returns error string sent by the server. More...
 
bool was_set () const
 Returns true if the last requested value was set, false if it was unset. More...
 

Additional Inherited Members

- Protected Member Functions inherited from Mastodon::Easy::Entity
const Json::Value get (const string &key) const
 Returns the value of key as Json::Value. More...
 
const string get_string (const string &key) const
 Returns the value of key as std::string. More...
 
uint_fast64_t get_uint64 (const string &key) const
 Returns the value of key as std::uint_fast64_t. More...
 
double get_double (const string &key) const
 Returns the value of key as double. More...
 
bool get_bool (const string &key) const
 Returns the value of key as bool. More...
 
const system_clock::time_point get_time_point (const string &key) const
 Returns the value of key as time_point. More...
 
const std::vector< string > get_vector (const string &key) const
 Returns the value of key as vector. More...
 
void set (const string &key, const Json::Value &value)
 Sets the value of key. More...
 
std::uint_fast64_t stouint64 (const string &str) const
 
bool check_valid (const std::vector< string > &attributes) const
 Checks if an Entity is valid. More...
 

Detailed Description

Class to hold attachments.

Since
before 0.11.0

Constructor & Destructor Documentation

◆ Attachment() [1/2]

Attachment::Attachment ( const string &  json)
explicit

Constructs an Attachment object from a JSON string.

Parameters
jsonJSON string
Since
before 0.11.0
25 : Entity(json)
26 {}
Entity()
Constructs an empty Entity object.
Definition: entity.cpp:65

◆ Attachment() [2/2]

Attachment::Attachment ( )

Constructs an empty Attachment object.

Since
before 0.11.0
29 : Entity()
30 {}
Entity()
Constructs an empty Entity object.
Definition: entity.cpp:65

Member Function Documentation

◆ aspect()

double Attachment::aspect ( ) const

Aspect of original image.

Since
before 0.11.0
46 {
47  return get_double("meta.original.aspect");
48 }
double get_double(const string &key) const
Returns the value of key as double.
Definition: entity.cpp:162

◆ aspect_small()

double Attachment::aspect_small ( ) const

Aspect of preview image.

Since
before 0.11.0
51 {
52  return get_double("meta.small.aspect");
53 }
double get_double(const string &key) const
Returns the value of key as double.
Definition: entity.cpp:162

◆ bitrate()

uint_fast64_t Attachment::bitrate ( ) const

Returns the bitrate of a video.

Since
before 0.11.0
56 {
57  return get_uint64("meta.original.bitrate");
58 }
uint_fast64_t get_uint64(const string &key) const
Returns the value of key as std::uint_fast64_t.
Definition: entity.cpp:148

◆ description() [1/2]

const string Attachment::description ( ) const

Returns the image description.

Since
before 0.11.0
61 {
62  return get_string("description");
63 }
const string get_string(const string &key) const
Returns the value of key as std::string.
Definition: entity.cpp:134

◆ description() [2/2]

Attachment Attachment::description ( const string &  description)

Sets the image description.

Since
0.17.0
66 {
67  set("description", Json::Value(description));
68  return *this;
69 }
const string description() const
Returns the image description.
Definition: attachment.cpp:60

◆ duration()

const std::chrono::duration< double > Attachment::duration ( ) const

Returns the duration of a video in seconds.

Since
before 0.11.0
72 {
73  const double sec = get_double("meta.original.duration");
74 
75  return std::chrono::duration<double>(sec);
76 }
double get_double(const string &key) const
Returns the value of key as double.
Definition: entity.cpp:162

◆ file() [1/2]

const string Attachment::file ( ) const

Gets file to upload.

Since
0.17.0
79 {
80  return get_string("file");
81 }
const string get_string(const string &key) const
Returns the value of key as std::string.
Definition: entity.cpp:134

◆ file() [2/2]

Attachment Attachment::file ( const string &  file)

Sets file to upload.

Since
0.17.0
Parameters
fileFilename
84 {
85  set("file", Json::Value(file));
86  return *this;
87 }
const string file() const
Gets file to upload.
Definition: attachment.cpp:78

◆ focus() [1/2]

const std::array< double, 2 > Attachment::focus ( ) const

Returns the focus point (x, y)

Values are between -1.0 and 1.0.

Since
before 0.11.0
91 {
92  const Json::Value x = get("meta.focus.x");
93  const Json::Value y = get("meta.focus.y");
94  if (x.isDouble() && y.isDouble())
95  {
96  return
97  {{
98  x.asDouble(),
99  y.asDouble()
100  }};
101  }
102 
103  return {};
104 }

◆ focus() [2/2]

Attachment Attachment::focus ( const std::array< double, 2 > &  focus)

Sets the focus point (x, y)

Values are between -1.0 and 1.0.

Since
0.17.0
107 {
108  set("meta.focus.x", Json::Value(focus[0]));
109  set("meta.focus.y", Json::Value(focus[1]));
110  return *this;
111 }
const std::array< double, 2 > focus() const
Returns the focus point (x, y)
Definition: attachment.cpp:90

◆ framerate()

double Attachment::framerate ( ) const

Returns the framerate of a video in frames per second.

Since
before 0.11.0
114 {
115  string strframes = get_string("meta.original.frame_rate");
116 
117  if (!strframes.empty())
118  {
119  std::size_t pos = strframes.find('/');
120  if (pos != std::string::npos)
121  {
122  std::uint_fast16_t frames = std::stoul(strframes.substr(0, pos));
123  std::uint_fast16_t divider = std::stoul(strframes.substr(pos + 1));
124 
125  return frames / divider;
126  }
127  }
128 
129  return 0.0;
130 }
const string get_string(const string &key) const
Returns the value of key as std::string.
Definition: entity.cpp:134

◆ height()

uint_fast64_t Attachment::height ( ) const

Returns the height of the original image.

Since
before 0.11.0
133 {
134  return get_uint64("meta.original.height");
135 }
uint_fast64_t get_uint64(const string &key) const
Returns the value of key as std::uint_fast64_t.
Definition: entity.cpp:148

◆ height_small()

uint_fast64_t Attachment::height_small ( ) const

Returns the height of the preview image.

Since
before 0.11.0
138 {
139  return get_uint64("meta.small.height");
140 }
uint_fast64_t get_uint64(const string &key) const
Returns the value of key as std::uint_fast64_t.
Definition: entity.cpp:148

◆ id()

const string Attachment::id ( ) const

Returns the ID of the attachment.

Since
before 0.11.0
143 {
144  return get_string("id");
145 }
const string get_string(const string &key) const
Returns the value of key as std::string.
Definition: entity.cpp:134

◆ preview_url()

const string Attachment::preview_url ( ) const

Returns the URL of the preview image.

Since
before 0.11.0
148 {
149  return get_string("preview_url");
150 }
const string get_string(const string &key) const
Returns the value of key as std::string.
Definition: entity.cpp:134

◆ remote_url()

const string Attachment::remote_url ( ) const

Returns the remote URL of the original image.

Since
before 0.11.0
153 {
154  return get_string("remote_url");
155 }
const string get_string(const string &key) const
Returns the value of key as std::string.
Definition: entity.cpp:134

◆ size()

const string Attachment::size ( ) const

Returns the size of the original image.

Since
before 0.11.0
158 {
159  return get_string("meta.original.size");
160 }
const string get_string(const string &key) const
Returns the value of key as std::string.
Definition: entity.cpp:134

◆ size_small()

const string Attachment::size_small ( ) const

Returns the size of the preview image.

Since
before 0.11.0
163 {
164  return get_string("meta.small.size");
165 }
const string get_string(const string &key) const
Returns the value of key as std::string.
Definition: entity.cpp:134

◆ text_url()

const string Attachment::text_url ( ) const

Returns shorter URL for the image.

Since
before 0.11.0
168 {
169  return get_string("text_url");
170 }
const string get_string(const string &key) const
Returns the value of key as std::string.
Definition: entity.cpp:134

◆ type()

Easy::attachment_type Attachment::type ( ) const

Returns attachment type.

Since
before 0.11.0
173 {
174  const string strtype = get_string("type");
175  if (strtype.compare("image") == 0)
176  return attachment_type::Image;
177  else if (strtype.compare("video") == 0)
178  return attachment_type::Video;
179  else if (strtype.compare("gifv") == 0)
180  return attachment_type::Gifv;
181  else if (strtype.compare("unknown") == 0)
182  return attachment_type::Unknown;
183 
184  ttdebug << "Could not get data: type\n";
185  return attachment_type::Undefined;
186 }
const string get_string(const string &key) const
Returns the value of key as std::string.
Definition: entity.cpp:134

◆ url()

const string Attachment::url ( ) const

Returns URL of the locally hosted version of the image.

Since
before 0.11.0
189 {
190  return get_string("url");
191 }
const string get_string(const string &key) const
Returns the value of key as std::string.
Definition: entity.cpp:134

◆ valid()

bool Attachment::valid ( ) const
virtual

Returns true if the Entity holds valid data.

Since
before 0.11.0 (virtual since 0.18.2)

Implements Mastodon::Easy::Entity.

33 {
34  const std::vector<string> attributes =
35  {{
36  "id",
37  "type",
38  "url",
39  "preview_url"
40  }};
41 
42  return Entity::check_valid(attributes);
43 }
bool check_valid(const std::vector< string > &attributes) const
Checks if an Entity is valid.
Definition: entity.cpp:69

◆ width()

uint_fast64_t Attachment::width ( ) const

Returns the width of the original image.

Since
before 0.11.0
194 {
195  return get_uint64("meta.original.width");
196 }
uint_fast64_t get_uint64(const string &key) const
Returns the value of key as std::uint_fast64_t.
Definition: entity.cpp:148

◆ width_small()

uint_fast64_t Attachment::width_small ( ) const

Returns the width of the preview image.

Since
before 0.11.0
199 {
200  return get_uint64("meta.small.width");
201 }
uint_fast64_t get_uint64(const string &key) const
Returns the value of key as std::uint_fast64_t.
Definition: entity.cpp:148

The documentation for this class was generated from the following files: