Class: Checkoff::Attachments
- Inherits:
-
Object
- Object
- Checkoff::Attachments
- Includes:
- Logging
- Defined in:
- lib/checkoff/attachments.rb,
sig/checkoff.rbs
Overview
Manage attachments in Asana
Constant Summary collapse
- MINUTE =
60- HOUR =
MINUTE * 60
- DAY =
24 * HOUR
- REALLY_LONG_CACHE_TIME =
HOUR- LONG_CACHE_TIME =
MINUTE * 15
- SHORT_CACHE_TIME =
MINUTE
Instance Attribute Summary collapse
-
#client ⇒ Asana::Client
readonly
sord warn - Asana::Client wasn't able to be resolved to a constant in this project.
- #workspaces ⇒ Checkoff::Workspaces readonly
Class Method Summary collapse
Instance Method Summary collapse
-
#content_type_from_filename(filename) ⇒ String?
@param
filename. -
#create_attachment_from_downloaded_url!(url, resource, attachment_name:, verify_mode: OpenSSL::SSL::VERIFY_PEER) ⇒ Object
sord warn - Asana::Resources::Task wasn't able to be resolved to a constant in this project sord warn - Asana::Resources::Attachment wasn't able to be resolved to a constant in this project @param
url. -
#create_attachment_from_url!(url, resource, attachment_name: nil, verify_mode: OpenSSL::SSL::VERIFY_PEER, just_the_url: false) ⇒ Object
sord warn - Asana::Resources::Task wasn't able to be resolved to a constant in this project sord warn - Asana::Resources::Attachment wasn't able to be resolved to a constant in this project @param
url. -
#create_attachment_from_url_alone!(url, resource, attachment_name:) ⇒ Asana::Resources::Attachment
sord warn - Asana::Resources::Task wasn't able to be resolved to a constant in this project sord warn - Asana::Resources::Attachment wasn't able to be resolved to a constant in this project @param
url. -
#debug ⇒ void
@param
message. -
#download_uri(uri, verify_mode: OpenSSL::SSL::VERIFY_PEER, &block) ⇒ Object
Writes contents of URL to a temporary file with the same extension as the URL using Net::HTTP, raising an exception if not succesful.
-
#error ⇒ void
@param
message. -
#finer ⇒ void
@param
message. -
#info ⇒ void
@param
message. -
#initialize(config: Checkoff::Internal::ConfigLoader.load(:asana), workspaces: Checkoff::Workspaces.new(config:), clients: Checkoff::Clients.new(config:), client: clients.client) ⇒ Object
constructor
sord warn - Asana::Client wasn't able to be resolved to a constant in this project @param
config. - #log_level ⇒ Symbol
- #logger ⇒ ::Logger
-
#parse(response) ⇒ ::Array[::Hash[untyped, untyped]]
sord warn - Asana::HttpClient::Response wasn't able to be resolved to a constant in this project https://github.com/Asana/ruby-asana/blob/master/lib/asana/resource_includes/response_helper.rb#L7.
-
#warn ⇒ void
@param
message. -
#write_tempfile_from_response(response) ⇒ void
sord duck - #read_body looks like a duck type, replacing with untyped @param
response.
Constructor Details
#initialize(config: Checkoff::Internal::ConfigLoader.load(:asana), workspaces: Checkoff::Workspaces.new(config:), clients: Checkoff::Clients.new(config:), client: clients.client) ⇒ Object
sord warn - Asana::Client wasn't able to be resolved to a constant in this project
@param config
@param workspaces
@param clients
@param client
48 49 50 51 52 53 54 |
# File 'lib/checkoff/attachments.rb', line 48 def initialize(config: Checkoff::Internal::ConfigLoader.load(:asana), workspaces: Checkoff::Workspaces.new(config:), clients: Checkoff::Clients.new(config:), client: clients.client) @workspaces = workspaces @client = client end |
Instance Attribute Details
#client ⇒ Asana::Client (readonly)
sord warn - Asana::Client wasn't able to be resolved to a constant in this project
182 183 184 |
# File 'lib/checkoff/attachments.rb', line 182 def client @client end |
#workspaces ⇒ Checkoff::Workspaces (readonly)
179 180 181 |
# File 'lib/checkoff/attachments.rb', line 179 def workspaces @workspaces end |
Class Method Details
.run ⇒ void
This method returns an undefined value.
187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/checkoff/attachments.rb', line 187 def run # @type [String] gid = ARGV[0] || raise('Please pass task gid as first argument') # @type [String] url = ARGV[1] || raise('Please pass attachment URL as second argument') tasks = Checkoff::Tasks.new = Checkoff::Attachments.new task = tasks.task_by_gid(gid) raise "Could not find task #{gid}" if task.nil? = .(url, task) puts "Results: #{.inspect}" end |
Instance Method Details
#content_type_from_filename(filename) ⇒ String?
@param filename
162 163 164 |
# File 'lib/checkoff/attachments.rb', line 162 def content_type_from_filename(filename) MIME::Types.type_for(filename)&.first&.content_type end |
#create_attachment_from_downloaded_url!(url, resource, attachment_name:, verify_mode: OpenSSL::SSL::VERIFY_PEER) ⇒ Object
sord warn - Asana::Resources::Task wasn't able to be resolved to a constant in this project
sord warn - Asana::Resources::Attachment wasn't able to be resolved to a constant in this project
@param url
@param resource
@param attachment_name
@param verify_mode — - e.g., OpenSSL::SSL::VERIFY_NONE,OpenSSL::SSL::VERIFY_PEER
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/checkoff/attachments.rb', line 126 def (url, resource, attachment_name:, verify_mode: OpenSSL::SSL::VERIFY_PEER) uri = URI(url) # @type [String] = || File.basename(uri.path || '') download_uri(uri, verify_mode:) do |tempfile| content_type = content_type_from_filename() content_type ||= content_type_from_filename(uri.path || '') # neither the attachment name nor the URL's path has a MIME-recognizable extension content_type ||= 'application/octet-stream' resource.attach(filename: , mime: content_type, io: tempfile) end end |
#create_attachment_from_url!(url, resource, attachment_name: nil, verify_mode: OpenSSL::SSL::VERIFY_PEER, just_the_url: false) ⇒ Object
sord warn - Asana::Resources::Task wasn't able to be resolved to a constant in this project
sord warn - Asana::Resources::Attachment wasn't able to be resolved to a constant in this project
@param url
@param resource
@param attachment_name
@param just_the_url
@param verify_mode — - e.g., OpenSSL::SSL::VERIFY_NONE or OpenSSL::SSL::VERIFY_PEER
63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/checkoff/attachments.rb', line 63 def (url, resource, attachment_name: nil, verify_mode: OpenSSL::SSL::VERIFY_PEER, just_the_url: false) if just_the_url (url, resource, attachment_name:) else (url, resource, attachment_name:, verify_mode:) end end |
#create_attachment_from_url_alone!(url, resource, attachment_name:) ⇒ Asana::Resources::Attachment
sord warn - Asana::Resources::Task wasn't able to be resolved to a constant in this project
sord warn - Asana::Resources::Attachment wasn't able to be resolved to a constant in this project
@param url
@param resource
@param attachment_name
147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/checkoff/attachments.rb', line 147 def (url, resource, attachment_name:) with_params = { 'parent' => resource.gid, 'url' => url, 'resource_subtype' => 'external', 'name' => , } = {} Asana::Resources::Resource.new(parse(client.post('/attachments', body: with_params, options:)).first, client:) end |
#debug ⇒ void
This method returns an undefined value.
@param message
1662 |
# File 'sig/checkoff.rbs', line 1662
def debug: (?Object? message) -> void
|
#download_uri(uri, verify_mode: OpenSSL::SSL::VERIFY_PEER, &block) ⇒ Object
Writes contents of URL to a temporary file with the same extension as the URL using Net::HTTP, raising an exception if not succesful
@param uri
@param verify_mode — - e.g., OpenSSL::SSL::VERIFY_NONE,OpenSSL::SSL::VERIFY_PEER
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/checkoff/attachments.rb', line 86 def download_uri(uri, verify_mode: OpenSSL::SSL::VERIFY_PEER, &block) out = nil host = uri.host || raise("URI has no host: #{uri}") Net::HTTP.start(host, uri.port, use_ssl: uri.scheme == 'https', verify_mode:) do |http| http.request(Net::HTTP::Get.new(uri)) do |response| raise("Unexpected response code: #{response.code}") unless response.code == '200' write_tempfile_from_response(response) { |tempfile| out = block.yield tempfile } end end out rescue StandardError => e debug { e } raise "Error downloading #{uri}: #{e}" end |
#error ⇒ void
This method returns an undefined value.
@param message
1653 |
# File 'sig/checkoff.rbs', line 1653
def error: (?Object? message) -> void
|
#finer ⇒ void
This method returns an undefined value.
@param message
1665 |
# File 'sig/checkoff.rbs', line 1665
def finer: (?Object? message) -> void
|
#info ⇒ void
This method returns an undefined value.
@param message
1659 |
# File 'sig/checkoff.rbs', line 1659
def info: (?Object? message) -> void
|
#log_level ⇒ Symbol
1667 |
# File 'sig/checkoff.rbs', line 1667
def log_level: () -> Symbol
|
#logger ⇒ ::Logger
1650 |
# File 'sig/checkoff.rbs', line 1650
def logger: () -> ::Logger
|
#parse(response) ⇒ ::Array[::Hash[untyped, untyped]]
sord warn - Asana::HttpClient::Response wasn't able to be resolved to a constant in this project https://github.com/Asana/ruby-asana/blob/master/lib/asana/resource_includes/response_helper.rb#L7
@param response
170 171 172 173 174 175 176 |
# File 'lib/checkoff/attachments.rb', line 170 def parse(response) data = response.body.fetch('data') do raise("Unexpected response body: #{response.body}") end extra = response.body.except('data') [data, extra] end |
#warn ⇒ void
This method returns an undefined value.
@param message
1656 |
# File 'sig/checkoff.rbs', line 1656
def warn: (?Object? message) -> void
|
#write_tempfile_from_response(response) ⇒ void
This method returns an undefined value.
sord duck - #read_body looks like a duck type, replacing with untyped
@param response
107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/checkoff/attachments.rb', line 107 def write_tempfile_from_response(response) Tempfile.create('checkoff') do |tempfile| tempfile.binmode response.read_body do |chunk| tempfile.write(chunk) end tempfile.rewind yield tempfile end nil end |