Sha256: e5ba9059de0df056c5a8a47a014b21ea7fc64934bafc3d082ca03aeaf7931ba4

Contents?: true

Size: 1.61 KB

Versions: 2

Compression:

Stored size: 1.61 KB

Contents

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

module Inbox
  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

2 entries across 2 versions & 2 rubygems

Version Path
nylas-2.0.1 lib/api_thread.rb
inbox-2.0.1 lib/api_thread.rb