Class: Inbox::Namespace

Inherits:
RestfulModel show all
Defined in:
lib/namespace.rb

Constant Summary collapse

OBJECTS_TABLE =
{
  "account" => Inbox::Account,
  "calendar" => Inbox::Calendar,
  "draft" => Inbox::Draft,
  "thread" => Inbox::Thread,
  "contact" => Inbox::Contact,
  "event" => Inbox::Event,
  "file" => Inbox::File,
  "message" => Inbox::Message,
  "namespace" => Inbox::Namespace,
  "tag" => Inbox::Tag,
}

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from RestfulModel

#==, #as_json, #destroy, #inflate, #initialize, #save!, #update, #url

Methods included from TimeAttrAccessor

#time_attr_accessor

Methods included from Parameters

included, #parameters

Constructor Details

This class inherits a constructor from Inbox::RestfulModel

Class Method Details

.collection_nameObject



26
27
28
# File 'lib/namespace.rb', line 26

def self.collection_name
  "n"
end

Instance Method Details

#calendarsObject



54
55
56
# File 'lib/namespace.rb', line 54

def calendars
  @calendars ||= RestfulModelCollection.new(Calendar, @_api, @id)
end

#contactsObject



50
51
52
# File 'lib/namespace.rb', line 50

def contacts
  @contacts ||= RestfulModelCollection.new(Contact, @_api, @id)
end

#deltas(cursor, exclude_types = []) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/namespace.rb', line 90

def deltas(cursor, exclude_types=[])
  raise 'Please provide a block for receiving the delta objects' if !block_given?
  exclude_string = ""

  if exclude_types.any?
    exclude_string = "&exclude_types="

    exclude_types.each do |value|
      count = 0
      if OBJECTS_TABLE.has_value?(value)
        param_name = OBJECTS_TABLE.key(value)
        exclude_string += "#{param_name},"
      end
    end
  end

  exclude_string = exclude_string[0..-2]

  # loop and yield deltas until we've come to the end.
  loop do
    path = @_api.url_for_path("/n/#{@namespace_id}/delta?cursor=#{cursor}#{exclude_string}")
    json = nil

    RestClient.get(path) do |response,request,result|
      json = Inbox.interpret_response(result, response, {:expected_class => Object})
    end

    start_cursor = json["cursor_start"]
    end_cursor = json["cursor_end"]

    json["deltas"].each do |delta|
      cls = OBJECTS_TABLE[delta['object']]
      obj = cls.new(@_api, @namespace_id)

      case delta["event"]
      when 'create', 'modify'
          obj.inflate(delta['attributes'])
          obj.cursor = delta["cursor"]
          yield delta["event"], obj
      when 'delete'
          obj.id = delta["id"]
          obj.cursor = delta["cursor"]
          yield delta["event"], obj
      end
    end

    break if start_cursor == end_cursor
    cursor = end_cursor
  end
end

#draftsObject



46
47
48
# File 'lib/namespace.rb', line 46

def drafts
  @drafts ||= RestfulModelCollection.new(Draft, @_api, @id)
end

#eventsObject



58
59
60
# File 'lib/namespace.rb', line 58

def events
  @events ||= RestfulModelCollection.new(Event, @_api, @id)
end

#filesObject



42
43
44
# File 'lib/namespace.rb', line 42

def files
  @files ||= RestfulModelCollection.new(File, @_api, @id)
end

#get_cursor(timestamp) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/namespace.rb', line 62

def get_cursor(timestamp)
  # Get the cursor corresponding to a specific timestamp.
  path = @_api.url_for_path("/n/#{@namespace_id}/delta/generate_cursor")
  data = { :start => timestamp }

  cursor = nil

  RestClient.post(path, data.to_json, :content_type => :json) do |response,request,result|
    json = Inbox.interpret_response(result, response, {:expected_class => Object})
    cursor = json["cursor"]
  end

  cursor
end

#messagesObject



38
39
40
# File 'lib/namespace.rb', line 38

def messages
  @messages ||= RestfulModelCollection.new(Message, @_api, @id)
end

#tagsObject



34
35
36
# File 'lib/namespace.rb', line 34

def tags
  @tags ||= RestfulModelCollection.new(Tag, @_api, @id)
end

#threadsObject



30
31
32
# File 'lib/namespace.rb', line 30

def threads
  @threads ||= RestfulModelCollection.new(Thread, @_api, @id)
end