Sha256: 8860be1403f8abe0016d959f1fc9703fa4b468dbc964ef5c58e9145479171f13

Contents?: true

Size: 1.61 KB

Versions: 1

Compression:

Stored size: 1.61 KB

Contents

require 'restful_model'
require 'time_attr_accessor'
require 'mixins'

module Nylas
  class Thread < RestfulModel
    extend TimeAttrAccessor

    parameter :subject
    parameter :participants
    parameter :snippet
    parameter :message_ids
    parameter :draft_ids
    parameter :labels
    parameter :folder
    parameter :starred
    parameter :unread
    parameter :has_attachments
    time_attr_accessor :last_message_timestamp
    time_attr_accessor :first_message_timestamp

    include ReadUnreadMethods

    def inflate(json)
      super
      @labels ||= []
      @folder ||= nil

      # This is a special case --- we receive label data from the API
      # as JSON but we want it to behave like an API object.
      @labels.map! do |label_json|
       label = Label.new(@_api)
       label.inflate(label_json)
       label
      end

      if not folder.nil? and folder.is_a?(Hash)
       folder = Folder.new(@_api)
       folder.inflate(@folder)
       @folder = folder
      end
    end

    def messages
      @messages ||= RestfulModelCollection.new(Message, @_api, {:thread_id=>@id})
    end

    def drafts
      @drafts ||= RestfulModelCollection.new(Draft, @_api, {:thread_id=>@id})
    end

    def as_json(options = {})
      hash = {}

      if not @unread.nil?
        hash["unread"] = @unread
      end

      if not @starred.nil?
        hash["starred"] = @starred
      end

      if not @labels.nil? and @labels != []
        hash["label_ids"] = @labels.map do |label|
          label.id
        end
      end

      if not @folder.nil?
        hash["folder_id"] = @folder.id
      end

      hash
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
nylas-3.0.0 lib/api_thread.rb