From 69fc8d805396b889b5e8c1c88e8129d93db77d29 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Sat, 20 Aug 2016 13:56:23 -0400 Subject: Updated API to use exceptions and make tweet/user objects more helpful --- src/configuration.cpp | 101 ++++++++++---------------------------------------- 1 file changed, 19 insertions(+), 82 deletions(-) (limited to 'src/configuration.cpp') diff --git a/src/configuration.cpp b/src/configuration.cpp index 63464ed..0010a40 100644 --- a/src/configuration.cpp +++ b/src/configuration.cpp @@ -1,35 +1,27 @@ #include "configuration.h" #include #include - -using nlohmann::json; +#include "codes.h" namespace twitter { - configuration::configuration() + configuration::configuration(std::string data) try { - _valid = false; - } - - configuration::configuration(std::string data) - { - _valid = true; - - auto _data = json::parse(data); + auto json_data = nlohmann::json::parse(data); - _characters_reserved_per_media = _data.at("characters_reserved_per_media"); - _dm_text_character_limit = _data.at("dm_text_character_limit"); - _max_media_per_upload = _data.at("max_media_per_upload"); - _photo_size_limit = _data.at("photo_size_limit"); - _short_url_length = _data.at("short_url_length"); - _short_https_url_length = _data.at("short_url_length_https"); + _characters_reserved_per_media = json_data["characters_reserved_per_media"].get(); + _dm_text_character_limit = json_data["dm_text_character_limit"].get(); + _max_media_per_upload = json_data["max_media_per_upload"].get(); + _photo_size_limit = json_data["photo_size_limit"].get(); + _short_url_length = json_data["short_url_length"].get(); + _short_https_url_length = json_data["short_url_length_https"].get(); - for (json::iterator sizedata = _data.at("photo_sizes").begin(); sizedata != _data.at("photo_sizes").end(); ++sizedata) + for (auto sizedata = std::begin(json_data["photo_sizes"]); sizedata != std::end(json_data["photo_sizes"]); ++sizedata) { photosize size; - size.height = sizedata.value().at("h"); - size.width = sizedata.value().at("w"); - if (sizedata.value().at("resize") == "fit") + size.height = sizedata.value()["h"].get(); + size.width = sizedata.value()["w"].get(); + if (sizedata.value()["resize"].get() == "fit") { size.resize = resizetype::fit; } else { @@ -39,71 +31,16 @@ namespace twitter { _photo_sizes[sizedata.key()] = size; } - for (auto path : _data.at("non_username_paths")) + for (auto path : json_data["non_username_paths"]) { _non_username_paths.insert(path.get()); } - } - - size_t configuration::getCharactersReservedPerMedia() const - { - assert(_valid); - - return _characters_reserved_per_media; - } - - size_t configuration::getDirectMessageCharacterLimit() const - { - assert(_valid); - - return _dm_text_character_limit; - } - - size_t configuration::getMaxMediaPerUpload() const - { - assert(_valid); - - return _max_media_per_upload; - } - - size_t configuration::getPhotoSizeLimit() const - { - assert(_valid); - - return _photo_size_limit; - } - - std::map configuration::getPhotoSizes() const - { - assert(_valid); - - return _photo_sizes; - } - - size_t configuration::getShortUrlLength() const - { - assert(_valid); - - return _short_url_length; - } - - size_t configuration::getShortHttpsUrlLength() const - { - assert(_valid); - - return _short_https_url_length; - } - - std::set configuration::getNonUsernamePaths() const + } catch (const std::invalid_argument& error) { - assert(_valid); - - return _non_username_paths; - } - - configuration::operator bool() const + std::throw_with_nested(malformed_object("configuration", data)); + } catch (const std::domain_error& error) { - return _valid; + std::throw_with_nested(malformed_object("configuration", data)); } - + }; -- cgit 1.4.1