Class: ApsisOnSteroids
- Inherits:
-
Object
- Object
- ApsisOnSteroids
- Defined in:
- lib/apsis-on-steroids.rb
Constant Summary collapse
- STRFTIME_FORMAT =
"%Y%m%dT%H%M%S"
Instance Attribute Summary collapse
-
#http ⇒ Object
readonly
Returns the value of attribute http.
Class Method Summary collapse
Instance Method Summary collapse
- #create_mailing_list(data) ⇒ Object
- #debugs(str) ⇒ Object
-
#destroy ⇒ Object
Closes connection and removes all references to resource-objects.
-
#initialize(args) ⇒ ApsisOnSteroids
constructor
A new instance of ApsisOnSteroids.
- #mailing_list_by_id(id) ⇒ Object
- #mailing_list_by_name(name) ⇒ Object
- #mailing_lists ⇒ Object
- #new_url_builder ⇒ Object
- #parse_obj(obj) ⇒ Object
- #read_paginated_response(resource_url) ⇒ Object
- #read_queued_response(url) ⇒ Object
- #read_resources_from_array(resource_class_name, resource_array) ⇒ Object
- #req_json(url, type = :get, method_args = {}) ⇒ Object
- #sendings_by_date_interval(date_from, date_to) ⇒ Object
- #subscriber_by_email(email) ⇒ Object
Constructor Details
#initialize(args) ⇒ ApsisOnSteroids
Returns a new instance of ApsisOnSteroids.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/apsis-on-steroids.rb', line 19 def initialize(args) raise "Invalid API key: '#{args[:api_key]}' from: '#{args}'." if args[:api_key].to_s.strip.empty? @args = args reconnect if block_given? begin yield self ensure @http.destroy @http = nil end end end |
Instance Attribute Details
#http ⇒ Object (readonly)
Returns the value of attribute http.
11 12 13 |
# File 'lib/apsis-on-steroids.rb', line 11 def http @http end |
Class Method Details
.const_missing(name) ⇒ Object
13 14 15 16 17 |
# File 'lib/apsis-on-steroids.rb', line 13 def self.const_missing(name) require "#{File.dirname(__FILE__)}/../include/#{::StringCases.camel_to_snake(name)}" raise "Still not loaded: '#{name}'." unless ApsisOnSteroids.const_defined?(name) return ApsisOnSteroids.const_get(name) end |
Instance Method Details
#create_mailing_list(data) ⇒ Object
68 69 70 71 72 73 74 75 |
# File 'lib/apsis-on-steroids.rb', line 68 def create_mailing_list(data) res = req_json("v1/mailinglists/", :post, json: data) if res["Code"] == 1 # Success! else raise "Unexpected result: '#{res}'." end end |
#debugs(str) ⇒ Object
50 51 52 |
# File 'lib/apsis-on-steroids.rb', line 50 def debugs(str) puts str if @args[:debug] end |
#destroy ⇒ Object
Closes connection and removes all references to resource-objects.
45 46 47 48 |
# File 'lib/apsis-on-steroids.rb', line 45 def destroy @http.destroy if @http @http = nil end |
#mailing_list_by_id(id) ⇒ Object
103 104 105 106 107 108 109 110 111 |
# File 'lib/apsis-on-steroids.rb', line 103 def mailing_list_by_id(id) tried_ids = [] mailing_lists.each do |mailing_list| return mailing_list if mailing_list.data(:id) == id.to_i tried_ids << mailing_list.data(:id) end raise "Mailing list by that ID could not be found: #{id} in list #{tried_ids}" end |
#mailing_list_by_name(name) ⇒ Object
95 96 97 98 99 100 101 |
# File 'lib/apsis-on-steroids.rb', line 95 def mailing_list_by_name(name) mailing_lists.each do |mlist| return mlist if name.to_s == mlist.data(:name).to_s end raise "Could not find mailing list by that name: '#{name}'." end |
#mailing_lists ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/apsis-on-steroids.rb', line 54 def mailing_lists res = req_json("v1/mailinglists/1/999") ret = [] res["Result"]["Items"].each do |mlist| ret << ApsisOnSteroids::MailingList.new( aos: self, data: mlist ) end return ret end |
#new_url_builder ⇒ Object
35 36 37 38 39 40 41 42 |
# File 'lib/apsis-on-steroids.rb', line 35 def new_url_builder ub = ::Http2::UrlBuilder.new ub.protocol = "https" ub.port = "8443" ub.host = "se.api.anpdm.com" return ub end |
#parse_obj(obj) ⇒ Object
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
# File 'lib/apsis-on-steroids.rb', line 215 def parse_obj(obj) if obj.is_a?(Array) ret = [] obj.each do |obj_i| ret << parse_obj(obj_i) end return ret elsif obj.is_a?(Hash) ret = {} obj.each do |key, val| ret[key] = parse_obj(val) end return ret elsif obj.is_a?(String) # Automatically convert dates. if match = obj.match(/^\/Date\((\d+)\+(\d+)\)\/$/) unix_t = match[1].to_i / 1000 return Time.at(unix_t) elsif match = obj.match(/^\/Date\((\d+)\)\/$/) unix_t = match[1].to_i / 1000 return Time.at(unix_t) end return obj else return obj end end |
#read_paginated_response(resource_url) ⇒ Object
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/apsis-on-steroids.rb', line 194 def read_paginated_response(resource_url) page = 1 resource_url = resource_url.gsub("%{size}", "1000") loop do resource_url_to_use = resource_url.gsub("%{page}", page.to_s) result = req_json(resource_url_to_use) result["Result"]["Items"].each do |resource_data| yield resource_data end size_no = result["Result"]["TotalPages"] if page >= size_no break else page += 1 end end end |
#read_queued_response(url) ⇒ Object
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/apsis-on-steroids.rb', line 165 def read_queued_response(url) uri = URI.parse(url) Timeout.timeout(300) do loop do sleep 1 res = req_json(uri.path) if res["State"] == "2" uri_data = URI.parse(res["DataUrl"]) return req_json(uri_data.path) elsif res["State"] == "1" || res["State"] == "0" # Keep waiting. else raise "Unknown state '#{res["State"]}': #{res}" end end end end |
#read_resources_from_array(resource_class_name, resource_array) ⇒ Object
185 186 187 188 189 190 191 192 |
# File 'lib/apsis-on-steroids.rb', line 185 def read_resources_from_array(resource_class_name, resource_array) Enumerator.new do |yielder| resource_array.each do |resource_data| resource = ApsisOnSteroids.const_get(resource_class_name).new(aos: self, data: resource_data) yielder << resource end end end |
#req_json(url, type = :get, method_args = {}) ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/apsis-on-steroids.rb', line 131 def req_json(url, type = :get, method_args = {}) # Parse arguments, send and parse the result. args = {url: url.start_with?('/') ? url[1..-1] : url}.merge(method_args) try = ::Tretry.new try.timeout = 300 if type == :get try.tries = 3 try.before_retry { @http.reconnect } else # Don't retry a manipulatable method! try.tries = 1 end res = nil try.try do http_res = @http.__send__(type, args) # Throw custom JSON error for debugging if the JSON was corrupt (this actually happens!). begin res = JSON.parse(http_res.body) rescue JSON::ParserError raise "Invalid JSON given: '#{http_res.body}'." end end # Check for various kind of server errors and raise them as Ruby errors if present. raise "Failed on server with code #{res["Code"]}: #{res["Message"]}" if res.is_a?(Hash) && res.key?("Code") && res["Code"] < 0 raise "Failed on server with state #{res["State"]} and name '#{res["StateName"]}': #{res["Message"]}" if res.is_a?(Hash) && res.key?("State") && res["State"].to_i < 0 # Return the result. return res end |
#sendings_by_date_interval(date_from, date_to) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/apsis-on-steroids.rb', line 77 def sendings_by_date_interval(date_from, date_to) date_from_str = date_from.strftime(STRFTIME_FORMAT) date_to_str = date_to.strftime(STRFTIME_FORMAT) Enumerator.new do |yielder| res = req_json("v1/sendqueues/date/from/#{date_from_str}/to/#{date_to_str}") res["Result"].each do |sending_data| sending = ApsisOnSteroids::Sending.new( aos: self, data: sending_data ) yielder << sending end end end |
#subscriber_by_email(email) ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/apsis-on-steroids.rb', line 113 def subscriber_by_email(email) begin res = req_json("subscribers/v2/email", :post, json: email) rescue raise ApsisOnSteroids::Errors::SubscriberNotFound, "Could not find subscriber by that email in the system: '#{email}'." end sub = ApsisOnSteroids::Subscriber.new( aos: self, data: { "Id" => res["Result"], "Email" => email } ) return sub end |