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 * 1
- 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::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::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::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
sord warn - URI wasn't able to be resolved to a constant in this project 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
@sg-ignore.
- #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) ⇒ Object
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
186 187 188 |
# File 'lib/checkoff/attachments.rb', line 186 def client @client end |
#workspaces ⇒ Checkoff::Workspaces (readonly)
183 184 185 |
# File 'lib/checkoff/attachments.rb', line 183 def workspaces @workspaces end |
Class Method Details
.run ⇒ void
This method returns an undefined value.
191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/checkoff/attachments.rb', line 191 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) # @sg-ignore = .(url, task) puts "Results: #{.inspect}" end |
Instance Method Details
#content_type_from_filename(filename) ⇒ String?
@param filename
166 167 168 |
# File 'lib/checkoff/attachments.rb', line 166 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::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
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/checkoff/attachments.rb', line 129 def (url, resource, attachment_name:, verify_mode: OpenSSL::SSL::VERIFY_PEER) uri = URI(url) ||= File.basename(uri.path) download_uri(uri, verify_mode:) do |tempfile| # @sg-ignore content_type ||= content_type_from_filename() # @sg-ignore content_type ||= content_type_from_filename(uri.path) # @sg-ignore 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::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::Attachment wasn't able to be resolved to a constant in this project
@param url
@param resource
@param attachment_name
150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/checkoff/attachments.rb', line 150 def (url, resource, attachment_name:) with_params = { # @sg-ignore '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
1677 |
# File 'sig/checkoff.rbs', line 1677
def debug: (?Object? message) -> void
|
#download_uri(uri, verify_mode: OpenSSL::SSL::VERIFY_PEER, &block) ⇒ Object
sord warn - URI wasn't able to be resolved to a constant in this project 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 101 |
# File 'lib/checkoff/attachments.rb', line 86 def download_uri(uri, verify_mode: OpenSSL::SSL::VERIFY_PEER, &block) out = nil # @sg-ignore Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https', verify_mode:) do |http| # @sg-ignore 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
1668 |
# File 'sig/checkoff.rbs', line 1668
def error: (?Object? message) -> void
|
#finer ⇒ void
This method returns an undefined value.
@param message
1680 |
# File 'sig/checkoff.rbs', line 1680
def finer: (?Object? message) -> void
|
#info ⇒ void
This method returns an undefined value.
@param message
1674 |
# File 'sig/checkoff.rbs', line 1674
def info: (?Object? message) -> void
|
#log_level ⇒ Symbol
@sg-ignore
1683 |
# File 'sig/checkoff.rbs', line 1683
def log_level: () -> Symbol
|
#logger ⇒ ::Logger
1665 |
# File 'sig/checkoff.rbs', line 1665
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
174 175 176 177 178 179 180 |
# File 'lib/checkoff/attachments.rb', line 174 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
1671 |
# File 'sig/checkoff.rbs', line 1671
def warn: (?Object? message) -> void
|
#write_tempfile_from_response(response) ⇒ Object
sord duck - #read_body looks like a duck type, replacing with untyped
@param response
108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/checkoff/attachments.rb', line 108 def write_tempfile_from_response(response) Tempfile.create('checkoff') do |tempfile| # @sg-ignore tempfile.binmode # @sg-ignore response.read_body do |chunk| tempfile.write(chunk) end # @sg-ignore tempfile.rewind yield tempfile end end |