Skip to content
This repository was archived by the owner on Oct 22, 2020. It is now read-only.

Commit a0e3456

Browse files
committed
Tidy up #run
1 parent c6a6ef9 commit a0e3456

File tree

1 file changed

+31
-26
lines changed

1 file changed

+31
-26
lines changed

lib/wpxf/wordpress/file_download.rb

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -66,20 +66,12 @@ def export_path
6666
# Run the module.
6767
# @return [Boolean] true if successful.
6868
def run
69-
raise 'A value must be specified for #working_directory' unless working_directory
69+
validate_implementation
7070

7171
return false unless super
72-
res = request_file
73-
74-
if res.nil? || res.timed_out?
75-
emit_error 'Request timed out, try increasing the http_client_timeout'
76-
return false
77-
end
7872

79-
if res.code != 200
80-
emit_error "Server responded with code #{res.code}"
81-
return false
82-
end
73+
res = request_file
74+
return false unless validate_result(res)
8375

8476
if export_path.nil?
8577
emit_success "Result: \n#{res.body}"
@@ -92,26 +84,39 @@ def run
9284

9385
private
9486

87+
def validate_implementation
88+
raise 'A value must be specified for #working_directory' unless working_directory
89+
end
90+
91+
def validate_result(res)
92+
if res.nil? || res.timed_out?
93+
emit_error 'Request timed out, try increasing the http_client_timeout'
94+
return false
95+
end
96+
97+
return true unless res.code != 200
98+
99+
emit_error "Server responded with code #{res.code}"
100+
false
101+
end
102+
103+
def core_request_opts
104+
{
105+
method: download_request_method,
106+
url: downloader_url,
107+
params: download_request_params,
108+
body: download_request_body,
109+
cookie: session_cookie
110+
}
111+
end
112+
95113
def request_file
96114
if export_path.nil?
97115
emit_info 'Requesting file...'
98-
return execute_request(
99-
method: download_request_method,
100-
url: downloader_url,
101-
params: download_request_params,
102-
body: download_request_body,
103-
cookie: session_cookie
104-
)
116+
return execute_request(core_request_opts)
105117
else
106118
emit_info 'Downloading file...'
107-
return download_file(
108-
url: downloader_url,
109-
method: download_request_method,
110-
params: download_request_params,
111-
local_filename: export_path,
112-
body: download_request_body,
113-
cookie: session_cookie
114-
)
119+
return download_file(core_request_opts.merge(local_filename: export_path))
115120
end
116121
end
117122
end

0 commit comments

Comments
 (0)