Class: Wikidotrb::Connector::AjaxModuleConnectorClient
- Inherits:
-
Object
- Object
- Wikidotrb::Connector::AjaxModuleConnectorClient
- Defined in:
- lib/wikidotrb/connector/ajax.rb
Instance Attribute Summary collapse
-
#config
readonly
Returns the value of attribute config.
-
#header
readonly
Returns the value of attribute header.
-
#site_name
readonly
Returns the value of attribute site_name.
Instance Method Summary collapse
-
#check_existence_and_ssl(site_name) ⇒ Boolean
サイトの存在とSSLの対応をチェック.
- #handle_response(response, body, retry_count) private
- #handle_wikidot_status(response_body, body, retry_count) private
-
#initialize(site_name:, config: nil) ⇒ AjaxModuleConnectorClient
constructor
AjaxModuleConnectorClientオブジェクトの初期化.
- #perform_request(body, semaphore, site_name, site_ssl_supported) private
-
#request(bodies:, return_exceptions: false, site_name: nil, site_ssl_supported: nil) ⇒ Array<Hash>
ajax-module-connector.phpへのリクエストを行う.
- #retry_or_raise(retry_count, body, status = nil) private
Constructor Details
#initialize(site_name:, config: nil) ⇒ AjaxModuleConnectorClient
AjaxModuleConnectorClientオブジェクトの初期化
78 79 80 81 82 83 84 85 |
# File 'lib/wikidotrb/connector/ajax.rb', line 78 def initialize(site_name:, config: nil) raise ArgumentError, "site_name cannot be nil" if site_name.nil? @site_name = site_name @config = config || AjaxModuleConnectorConfig.new @header = AjaxRequestHeader.new @logger = Wikidotrb::Common::Logger end |
Instance Attribute Details
#config (readonly)
Returns the value of attribute config.
73 74 75 |
# File 'lib/wikidotrb/connector/ajax.rb', line 73 def config @config end |
#header (readonly)
Returns the value of attribute header.
73 74 75 |
# File 'lib/wikidotrb/connector/ajax.rb', line 73 def header @header end |
#site_name (readonly)
Returns the value of attribute site_name.
73 74 75 |
# File 'lib/wikidotrb/connector/ajax.rb', line 73 def site_name @site_name end |
Instance Method Details
#check_existence_and_ssl(site_name) ⇒ Boolean
サイトの存在とSSLの対応をチェック
90 91 92 93 94 95 96 97 98 99 |
# File 'lib/wikidotrb/connector/ajax.rb', line 90 def check_existence_and_ssl(site_name) http_url = "http://#{site_name}.wikidot.com" https_url = "https://#{site_name}.wikidot.com" http_response = HTTPX.get(http_url) raise Wikidotrb::Common::Exceptions::NotFoundException, "Site is not found: #{site_name}.wikidot.com" if http_response.status == 404 https_response = HTTPX.get(https_url) https_response.status == 200 || (https_response.status == 301 && https_response.headers["location"].start_with?("https")) end |
#handle_response(response, body, retry_count) (private)
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/wikidotrb/connector/ajax.rb', line 148 def handle_response(response, body, retry_count) if response.is_a?(HTTPX::ErrorResponse) @logger.error("AMC is respond error: #{response.error} -> #{body}") retry_count += 1 return retry_or_raise(retry_count, body) if retry_count >= @config.attempt_limit @logger.info("AMC is retrying after error: #{response.error} (retry: #{retry_count})") sleep @config.retry_interval return nil end if response.status != 200 retry_count += 1 return retry_or_raise(retry_count, body) if retry_count >= @config.attempt_limit @logger.info("AMC is respond status: #{response.status} (retry: #{retry_count}) -> #{body}") sleep @config.retry_interval return nil end response_body = JSON.parse(response.body.to_s) raise ResponseDataException, "AMC is respond empty data" if response_body.nil? || response_body.empty? puts "response_body: #{body} -> #{response_body}" handle_wikidot_status(response_body, body, retry_count) rescue JSON::ParserError @logger.error("AMC is respond non-json data: \"#{response.body}\" -> #{body}") raise Wikidotrb::Common::Exceptions::ResponseDataException, "AMC is respond non-json data: \"#{response.body}\"" end |
#handle_wikidot_status(response_body, body, retry_count) (private)
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/wikidotrb/connector/ajax.rb', line 180 def handle_wikidot_status(response_body, body, retry_count) if response_body["status"] if response_body["status"] == "try_again" retry_count += 1 return retry_or_raise(retry_count, body, "try_again") if retry_count >= @config.attempt_limit @logger.info("AMC is respond status: \"try_again\" (retry: #{retry_count})") sleep @config.retry_interval return nil elsif response_body["status"] != "ok" raise Wikidotrb::Common::Exceptions::WikidotStatusCodeException.new( "AMC is respond error status: \"#{response_body["status"]}\"", response_body["status"] ) end end response_body end |
#perform_request(body, semaphore, site_name, site_ssl_supported) (private)
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/wikidotrb/connector/ajax.rb', line 123 def perform_request(body, semaphore, site_name, site_ssl_supported) retry_count = 0 loop do semaphore.acquire begin protocol = site_ssl_supported ? "https" : "http" url = "#{protocol}://#{site_name}.wikidot.com/ajax-module-connector.php" body["wikidot_token7"] = 123_456 @logger.debug("Ajax Request: #{url} -> #{body}") response = HTTPX.post( url, headers: @header.get_header, form: body, timeout: { operation: @config.request_timeout } ) return handle_response(response, body, retry_count) ensure semaphore.release end end end |
#request(bodies:, return_exceptions: false, site_name: nil, site_ssl_supported: nil) ⇒ Array<Hash>
ajax-module-connector.phpへのリクエストを行う
108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/wikidotrb/connector/ajax.rb', line 108 def request(bodies:, return_exceptions: false, site_name: nil, site_ssl_supported: nil) semaphore = Concurrent::Semaphore.new(@config.semaphore_limit) site_name ||= @site_name site_ssl_supported ||= check_existence_and_ssl(site_name) tasks = bodies.map do |body| Concurrent::Promises.future { perform_request(body, semaphore, site_name, site_ssl_supported) } end results = Concurrent::Promises.zip(*tasks).value! return_exceptions ? results : results.each { |r| raise r if r.is_a?(Exception) } end |
#retry_or_raise(retry_count, body, status = nil) (private)
199 200 201 202 203 |
# File 'lib/wikidotrb/connector/ajax.rb', line 199 def retry_or_raise(retry_count, body, status = nil) = status.nil? ? "AMC encountered an error" : "AMC is respond status: \"#{status}\"" @logger.error("#{} -> #{body}") raise AMCHttpStatusCodeException.new(, nil) end |