Class: Checkoff::Attachments

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/checkoff/attachments.rb,
sig/checkoff.rbs

Overview

Manage attachments in Asana

Constant Summary collapse

MINUTE =

Returns:

  • (Object)
60
HOUR =

Returns:

  • (Object)
MINUTE * 60
DAY =

Returns:

  • (Object)
24 * HOUR
REALLY_LONG_CACHE_TIME =

Returns:

  • (Object)
HOUR * 1
LONG_CACHE_TIME =

Returns:

  • (Object)
MINUTE * 15
SHORT_CACHE_TIME =

Returns:

  • (Object)
MINUTE

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#clientAsana::Client (readonly)

sord warn - Asana::Client wasn't able to be resolved to a constant in this project

Returns:

  • (Asana::Client)


186
187
188
# File 'lib/checkoff/attachments.rb', line 186

def client
  @client
end

#workspacesCheckoff::Workspaces (readonly)



183
184
185
# File 'lib/checkoff/attachments.rb', line 183

def workspaces
  @workspaces
end

Class Method Details

.runvoid

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
  attachments = Checkoff::Attachments.new
  task = tasks.task_by_gid(gid)
  # @sg-ignore
  attachment = attachments.create_attachment_from_url!(url, task)
  puts "Results: #{attachment.inspect}"
end

Instance Method Details

#content_type_from_filename(filename) ⇒ String?

@param filename

Parameters:

  • filename (String)

Returns:

  • (String, nil)


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 create_attachment_from_downloaded_url!(url, resource, attachment_name:,
                                           verify_mode: OpenSSL::SSL::VERIFY_PEER)
  uri = URI(url)
  attachment_name ||= File.basename(uri.path)
  download_uri(uri, verify_mode:) do |tempfile|
    # @sg-ignore
    content_type ||= content_type_from_filename(attachment_name)
    # @sg-ignore
    content_type ||= content_type_from_filename(uri.path)

    # @sg-ignore
    resource.attach(filename: attachment_name, 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 create_attachment_from_url!(url,
                                resource,
                                attachment_name: nil,
                                verify_mode: OpenSSL::SSL::VERIFY_PEER,
                                just_the_url: false)
  if just_the_url
    create_attachment_from_url_alone!(url, resource, attachment_name:)
  else
    create_attachment_from_downloaded_url!(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

Parameters:

Returns:

  • (Asana::Resources::Attachment)


150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/checkoff/attachments.rb', line 150

def create_attachment_from_url_alone!(url, resource, attachment_name:)
  with_params = {
    # @sg-ignore
    'parent' => resource.gid,
    'url' => url,
    'resource_subtype' => 'external',
    'name' => attachment_name,
  }
  options = {}
  Asana::Resources::Resource.new(parse(client.post('/attachments', body: with_params, options:)).first,
                                 client:)
end

#debugvoid

This method returns an undefined value.

@param message

Parameters:

  • message (Object, nil)


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

Parameters:

  • uri (URI)
  • verify_mode: (Integer) (defaults to: OpenSSL::SSL::VERIFY_PEER)

Returns:

  • (Object)


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

#errorvoid

This method returns an undefined value.

@param message

Parameters:

  • message (Object, nil)


1668
# File 'sig/checkoff.rbs', line 1668

def error: (?Object? message) -> void

#finervoid

This method returns an undefined value.

@param message

Parameters:

  • message (Object, nil)


1680
# File 'sig/checkoff.rbs', line 1680

def finer: (?Object? message) -> void

#infovoid

This method returns an undefined value.

@param message

Parameters:

  • message (Object, nil)


1674
# File 'sig/checkoff.rbs', line 1674

def info: (?Object? message) -> void

#log_levelSymbol

@sg-ignore

Returns:

  • (Symbol)


1683
# File 'sig/checkoff.rbs', line 1683

def log_level: () -> Symbol

#logger::Logger

Returns:

  • (::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

Parameters:

  • response (Asana::HttpClient::Response)

Returns:

  • (::Array[::Hash[untyped, untyped]])


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

#warnvoid

This method returns an undefined value.

@param message

Parameters:

  • message (Object, nil)


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

Parameters:

  • response (Object)

Returns:

  • (Object)


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