diff options
Diffstat (limited to 'src/configuration.cpp')
-rw-r--r-- | src/configuration.cpp | 101 |
1 files changed, 19 insertions, 82 deletions
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 @@ | |||
1 | #include "configuration.h" | 1 | #include "configuration.h" |
2 | #include <json.hpp> | 2 | #include <json.hpp> |
3 | #include <cassert> | 3 | #include <cassert> |
4 | 4 | #include "codes.h" | |
5 | using nlohmann::json; | ||
6 | 5 | ||
7 | namespace twitter { | 6 | namespace twitter { |
8 | 7 | ||
9 | configuration::configuration() | 8 | configuration::configuration(std::string data) try |
10 | { | 9 | { |
11 | _valid = false; | 10 | auto json_data = nlohmann::json::parse(data); |
12 | } | ||
13 | |||
14 | configuration::configuration(std::string data) | ||
15 | { | ||
16 | _valid = true; | ||
17 | |||
18 | auto _data = json::parse(data); | ||
19 | 11 | ||
20 | _characters_reserved_per_media = _data.at("characters_reserved_per_media"); | 12 | _characters_reserved_per_media = json_data["characters_reserved_per_media"].get<size_t>(); |
21 | _dm_text_character_limit = _data.at("dm_text_character_limit"); | 13 | _dm_text_character_limit = json_data["dm_text_character_limit"].get<size_t>(); |
22 | _max_media_per_upload = _data.at("max_media_per_upload"); | 14 | _max_media_per_upload = json_data["max_media_per_upload"].get<size_t>(); |
23 | _photo_size_limit = _data.at("photo_size_limit"); | 15 | _photo_size_limit = json_data["photo_size_limit"].get<size_t>(); |
24 | _short_url_length = _data.at("short_url_length"); | 16 | _short_url_length = json_data["short_url_length"].get<size_t>(); |
25 | _short_https_url_length = _data.at("short_url_length_https"); | 17 | _short_https_url_length = json_data["short_url_length_https"].get<size_t>(); |
26 | 18 | ||
27 | for (json::iterator sizedata = _data.at("photo_sizes").begin(); sizedata != _data.at("photo_sizes").end(); ++sizedata) | 19 | for (auto sizedata = std::begin(json_data["photo_sizes"]); sizedata != std::end(json_data["photo_sizes"]); ++sizedata) |
28 | { | 20 | { |
29 | photosize size; | 21 | photosize size; |
30 | size.height = sizedata.value().at("h"); | 22 | size.height = sizedata.value()["h"].get<size_t>(); |
31 | size.width = sizedata.value().at("w"); | 23 | size.width = sizedata.value()["w"].get<size_t>(); |
32 | if (sizedata.value().at("resize") == "fit") | 24 | if (sizedata.value()["resize"].get<std::string>() == "fit") |
33 | { | 25 | { |
34 | size.resize = resizetype::fit; | 26 | size.resize = resizetype::fit; |
35 | } else { | 27 | } else { |
@@ -39,71 +31,16 @@ namespace twitter { | |||
39 | _photo_sizes[sizedata.key()] = size; | 31 | _photo_sizes[sizedata.key()] = size; |
40 | } | 32 | } |
41 | 33 | ||
42 | for (auto path : _data.at("non_username_paths")) | 34 | for (auto path : json_data["non_username_paths"]) |
43 | { | 35 | { |
44 | _non_username_paths.insert(path.get<std::string>()); | 36 | _non_username_paths.insert(path.get<std::string>()); |
45 | } | 37 | } |
46 | } | 38 | } catch (const std::invalid_argument& error) |
47 | |||
48 | size_t configuration::getCharactersReservedPerMedia() const | ||
49 | { | ||
50 | assert(_valid); | ||
51 | |||
52 | return _characters_reserved_per_media; | ||
53 | } | ||
54 | |||
55 | size_t configuration::getDirectMessageCharacterLimit() const | ||
56 | { | ||
57 | assert(_valid); | ||
58 | |||
59 | return _dm_text_character_limit; | ||
60 | } | ||
61 | |||
62 | size_t configuration::getMaxMediaPerUpload() const | ||
63 | { | ||
64 | assert(_valid); | ||
65 | |||
66 | return _max_media_per_upload; | ||
67 | } | ||
68 | |||
69 | size_t configuration::getPhotoSizeLimit() const | ||
70 | { | ||
71 | assert(_valid); | ||
72 | |||
73 | return _photo_size_limit; | ||
74 | } | ||
75 | |||
76 | std::map<std::string, configuration::photosize> configuration::getPhotoSizes() const | ||
77 | { | ||
78 | assert(_valid); | ||
79 | |||
80 | return _photo_sizes; | ||
81 | } | ||
82 | |||
83 | size_t configuration::getShortUrlLength() const | ||
84 | { | ||
85 | assert(_valid); | ||
86 | |||
87 | return _short_url_length; | ||
88 | } | ||
89 | |||
90 | size_t configuration::getShortHttpsUrlLength() const | ||
91 | { | ||
92 | assert(_valid); | ||
93 | |||
94 | return _short_https_url_length; | ||
95 | } | ||
96 | |||
97 | std::set<std::string> configuration::getNonUsernamePaths() const | ||
98 | { | 39 | { |
99 | assert(_valid); | 40 | std::throw_with_nested(malformed_object("configuration", data)); |
100 | 41 | } catch (const std::domain_error& error) | |
101 | return _non_username_paths; | ||
102 | } | ||
103 | |||
104 | configuration::operator bool() const | ||
105 | { | 42 | { |
106 | return _valid; | 43 | std::throw_with_nested(malformed_object("configuration", data)); |
107 | } | 44 | } |
108 | 45 | ||
109 | }; | 46 | }; |