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

- (Hash) addresses (readonly)

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

- (Integer) body_section (readonly)

Returns:

  • (Integer)


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

def body_section
  @body_section
end

- (Time) date (readonly)

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

- (Time) date_indexed (readonly)

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

- (String) email_message_id (readonly)

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

- (String) file_name (readonly)

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

- (Array) file_name_structure (readonly)

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

- (String) id (readonly)

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

- (String) message_id (readonly)

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

- (Hash) person_info (readonly)

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

- (Integer) size (readonly)

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

- (String) subject (readonly)

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

- (true, false) supports_preview (readonly)

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

- (String) type (readonly)

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

+ (Array<File>) all(account, query = {})

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.

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

    The matching file objects.

See Also:

  • Account#messages


122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/context-io/file.rb', line 122

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

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

+ (File) from_json(account_id, json)

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.



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/context-io/file.rb', line 161

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

- (String) content

Note:

Data transfer for this call is metered and charged at the end of the month. See Context.IO's pricing page for more info.

Fetch the content of the message.

Returns:

  • (String)

    The raw contents of the file.



148
149
150
# File 'lib/context-io/file.rb', line 148

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