Class: ContextIO::File

Inherits:
Resource show all
Defined in:
lib/context-io/file.rb

Overview

A file found as an email attachment

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Request

#delete, #get, #post, #put, #request

Instance Attribute Details

#addressesHash (readonly)

Returns Information on the different addresses attached to this file’s message.

Returns:

  • (Hash)

    Information on the different addresses attached to this file’s message.



31
32
33
# File 'lib/context-io/file.rb', line 31

def addresses
  @addresses
end

#body_sectionInteger (readonly)

Returns:

  • (Integer)


39
40
41
# File 'lib/context-io/file.rb', line 39

def body_section
  @body_section
end

#dateTime (readonly)

Returns When this file was sent.

Returns:

  • (Time)

    When this file was sent



26
27
28
# File 'lib/context-io/file.rb', line 26

def date
  @date
end

#date_indexedTime (readonly)

Returns When Context.IO indexed the file (not the same as when it was sent).

Returns:

  • (Time)

    When Context.IO indexed the file (not the same as when it was sent)



52
53
54
# File 'lib/context-io/file.rb', line 52

def date_indexed
  @date_indexed
end

#email_message_idString (readonly)

Returns The email message ID (The Message-ID header).

Returns:

  • (String)

    The email message ID (The Message-ID header)



56
57
58
# File 'lib/context-io/file.rb', line 56

def email_message_id
  @email_message_id
end

#file_nameString (readonly)

Returns The full filename.

Returns:

  • (String)

    The full filename



35
36
37
# File 'lib/context-io/file.rb', line 35

def file_name
  @file_name
end

#file_name_structureArray (readonly)

Returns The file name split up into parts.

Returns:

  • (Array)

    The file name split up into parts



64
65
66
# File 'lib/context-io/file.rb', line 64

def file_name_structure
  @file_name_structure
end

#idString (readonly)

Returns The ID of the file.

Returns:

  • (String)

    The ID of the file



10
11
12
# File 'lib/context-io/file.rb', line 10

def id
  @id
end

#message_idString (readonly)

Returns The (Context.IO) ID of the message this file was attached to.

Returns:

  • (String)

    The (Context.IO) ID of the message this file was attached to



47
48
49
# File 'lib/context-io/file.rb', line 47

def message_id
  @message_id
end

#person_infoHash (readonly)

Returns Information about the people involved with the message.

Returns:

  • (Hash)

    Information about the people involved with the message



60
61
62
# File 'lib/context-io/file.rb', line 60

def person_info
  @person_info
end

#sizeInteger (readonly)

Returns The size of the file, in bytes.

Returns:

  • (Integer)

    The size of the file, in bytes.



14
15
16
# File 'lib/context-io/file.rb', line 14

def size
  @size
end

#subjectString (readonly)

Returns The subject of the message this file was attached to.

Returns:

  • (String)

    The subject of the message this file was attached to



22
23
24
# File 'lib/context-io/file.rb', line 22

def subject
  @subject
end

#supports_previewtrue, false (readonly)

Returns Whether the file supports preview.

Returns:

  • (true, false)

    Whether the file supports preview



43
44
45
# File 'lib/context-io/file.rb', line 43

def supports_preview
  @supports_preview
end

#typeString (readonly)

Returns The MIME type of the file.

Returns:

  • (String)

    The MIME type of the file.



18
19
20
# File 'lib/context-io/file.rb', line 18

def type
  @type
end

Class Method Details

.all(account, query = {}) ⇒ Array<File>, Array<Hash>

Get all files for a given account, optionally filtered with a query

Examples:

Get all files for the account

files = ContextIO::File.all()

Get 10 files that we have sent

files = ContextIO::File.all(,
  :from => .email_addresses.first,
  :limit => 10
)

Find PDF files

files = ContextIO::File.all(, :file_name => '*.pdf')

Find JP(E)G files

files = ContextIO::File.all(, :file_name => /\.jpe?g$/)

Parameters:

  • account (Account, #to_s)

    The account or account ID to search for files in.

  • query (Hash) (defaults to: {})

    A query to filter files by.

Options Hash (query):

  • :file_name (String, Regexp)

    The filename to search for. The string can contain shell globs (‘*’, ‘?’ and ‘[]’).

  • :email (String)

    The email address of the contact for whom you want the latest files exchanged with. By “exchanged with contact X”, we mean any email received from contact X, sent to contact X or sent by anyone to both contact X and the source owner.

  • :to (String)

    The email address of a contact files have been sent to.

  • :from (String)

    The email address of a contact files have been sent from.

  • :cc (String)

    The email address of a contact CC’ed on the messages.

  • :bcc (String)

    The email address of a contact BCC’ed on the messages.

  • :date_before (#to_i)

    Only include files attached to messages sent before this timestamp. The value of this filter is applied to the Date header of the message, which refers to the time the message is sent from the origin.

  • :date_after (#to_i)

    Only include files attached to messages sent after this timestamp. The value of this filter is applied to the Date header of the message, which refers to the time the message is sent from the origin.

  • :indexed_before (#to_i)

    Only include files attached to messages indexed before this timestamp. This is not the same as the date of the email, it is the time Context.IO indexed this message.

  • :indexed_after (#to_i)

    Only include files attached to messages indexed after this timestamp. This is not the same as the date of the email, it is the time Context.IO indexed this message.

  • :group_by_revisions (true, false) — default: false

    If this is set to true, the method will return an array of Hashes, where each Hash represents a group of revisions of the same file. The Hash has an ‘:occurences` field, which is an Array of ContextIO::File objects, a `:file_name` field, which is the name of the file, and a `:latest_date` field, which is a Time object representing the last time a message with this file was sent.

  • :limit (#to_i)

    The maximum count of results to return.

  • :offset (#to_i) — default: 0

    The offset to begin returning files at.

Returns:

  • (Array<File>, Array<Hash>)

    The matching file objects. If the ‘:group_by_revisions` flag is set, the return value changes, see the documentation for that flag above.

See Also:

  • Account#messages


131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/context-io/file.rb', line 131

def self.all(, query={})
  if query[:file_name] && query[:file_name].is_a?(Regexp)
    query[:file_name] = "/#{query[:file_name].source}/"
  end

  [:date_before, :date_after, :indexed, :indexed_after].each do |field|
    if query[field] && query[field].respond_to?(:to_i)
      query[field] = query[field].to_i
    end
  end

  if query[:group_by_revisions]
    query[:group_by_revisions] = query[:group_by_revisions] ? '1' : '0'
  end

   = .is_a?(Account) ? .id : .to_s
  get("/2.0/accounts/#{}/files", query).map do |file|
    if query[:group_by_revisions]
      occurences = file['occurences'].map do |file|
        File.from_json(, file)
      end

      {
        :occurences => occurences,
        :file_name => file['file_name'],
        :latest_date => Time.at(file['latest_date'].to_i)
      }
    else
      File.from_json(, file)
    end
  end
end

.from_json(account_id, json) ⇒ File

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Create a File with the JSON from Context.IO.

Parameters:

  • account_id (String)

    The account ID.

  • json (Hash)

    The parsed JSON object returned by a Context.IO API request. See their documentation for possible keys.

Returns:

  • (File)

    A file with the given attributes.



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/context-io/file.rb', line 186

def self.from_json(, json)
  file = new
  file.instance_eval do
    @id = json['file_id']
    @account_id = 
    @size = json['size']
    @type = json['type']
    @subject = json['subject']
    @date = Time.at(json['date'])
    @addresses = {}
    json['addresses'].each do |type, info|
      @addresses[type.to_sym] = {}
      if info.is_a?(Hash)
        info.each do |key, value|
          @addresses[type.to_sym][key.to_sym] = value
        end
      elsif info.is_a?(Array)
        @addresses[type.to_sym] = []
        info.each do |email_info|
          @addresses[type.to_sym] << {}
          email_info.each do |key, value|
            @addresses[type.to_sym].last[key.to_sym] = value
          end
        end
      end
    end
    @file_name = json['file_name']
    @body_section = json['body_section']
    @supports_preview = json['supports_preview']
    @message_id = json['message_id']
    @date_indexed = Time.at(json['date_indexed'])
    @email_message_id = json['email_message_id']
    @person_info = {}
    json['person_info'].each do |email, info|
      @person_info[email] = {}
      info.each do |key, value|
        @person_info[email][key.to_sym] = value
      end
    end
    @file_name_structure = []
    json['file_name_structure'].each do |part|
      @file_name_structure << [part.first, part.last.to_sym]
    end
  end

  file
end

Instance Method Details

#contentString

Note:

Data transfer for this call is metered and charged at the end of the month. See [Context.IO’s pricing page](context.io/pricing) for more info.

Fetch the content of the message.

Returns:

  • (String)

    The raw contents of the file.



173
174
175
# File 'lib/context-io/file.rb', line 173

def content
  get("/2.0/accounts/#@account_id/files/#@id/content", :raw => true)
end