Class: ApsisOnSteroids
- Inherits:
-
Object
- Object
- ApsisOnSteroids
- Defined in:
- lib/apsis-on-steroids.rb
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_name(name) ⇒ Object
- #mailing_lists ⇒ Object
- #parse_obj(obj) ⇒ Object
- #read_queued_response(url) ⇒ Object
- #req_json(url, type = :get, method_args = {}) ⇒ Object
- #subscriber_by_email(email) ⇒ Object
Constructor Details
#initialize(args) ⇒ ApsisOnSteroids
Returns a new instance of ApsisOnSteroids.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/apsis-on-steroids.rb', line 16 def initialize(args) raise "Invalid API key: '#{args[:api_key]}' from: '#{args}'." if args[:api_key].to_s.strip.empty? @args = args @http = Http2.new( :host => "se.api.anpdm.com", :port => 8443, :ssl => true, :follow_redirects => false, :debug => args[:debug], :extra_headers => { "Accept" => "text/json, application/json" }, :basic_auth => { :user => @args[:api_key], :passwd => "" } ) 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.
8 9 10 |
# File 'lib/apsis-on-steroids.rb', line 8 def http @http end |
Class Method Details
.const_missing(name) ⇒ Object
10 11 12 13 14 |
# File 'lib/apsis-on-steroids.rb', line 10 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
69 70 71 72 73 74 75 76 |
# File 'lib/apsis-on-steroids.rb', line 69 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
51 52 53 |
# File 'lib/apsis-on-steroids.rb', line 51 def debugs(str) puts str if @args[:debug] end |
#destroy ⇒ Object
Closes connection and removes all references to resource-objects.
46 47 48 49 |
# File 'lib/apsis-on-steroids.rb', line 46 def destroy @http.destroy if @http @http = nil end |
#mailing_list_by_name(name) ⇒ Object
78 79 80 81 82 83 84 |
# File 'lib/apsis-on-steroids.rb', line 78 def mailing_list_by_name(name) self.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
55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/apsis-on-steroids.rb', line 55 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 |
#parse_obj(obj) ⇒ Object
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/apsis-on-steroids.rb', line 143 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) end return obj else return obj end end |
#read_queued_response(url) ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/apsis-on-steroids.rb', line 123 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 |
#req_json(url, type = :get, method_args = {}) ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/apsis-on-steroids.rb', line 104 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) http_res = @http.__send__(type, args) begin res = JSON.parse(http_res.body) rescue JSON::ParserError raise "Invalid JSON given: '#{http_res.body}'." 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 |
#subscriber_by_email(email) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/apsis-on-steroids.rb', line 86 def subscriber_by_email(email) begin res = req_json("v1/subscribers/email/lookup/#{CGI.escape(email)}") rescue raise "Could not find subscriber by that email in the system" end sub = ApsisOnSteroids::Subscriber.new( :aos => self, :data => { "Id" => res["Result"], "Email" => email } ) return sub end |