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,
  "folder" => Inbox::Folder,
  "label" => Inbox::Label,
}

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



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

def self.collection_name
  "n"
end

Instance Method Details

#_build_exclude_types(exclude_types) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/namespace.rb', line 104

def _build_exclude_types(exclude_types)
  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

  exclude_string = exclude_string[0..-2]
end

#calendarsObject



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

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

#contactsObject



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

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

#delta_stream(cursor, exclude_types = [], timeout = 0) ⇒ Object



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/namespace.rb', line 163

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

  exclude_string = ""

  if exclude_types.any?
    exclude_string = _build_exclude_types(exclude_types)
  end

  # loop and yield deltas indefinitely.
  path = @_api.url_for_path("/n/#{@namespace_id}/delta/streaming?cursor=#{cursor}#{exclude_string}")

  parser = Yajl::Parser.new(:symbolize_keys => false)
  parser.on_parse_complete = proc do |data|
    delta = Inbox.interpret_response(OpenStruct.new(:code => '200'), data, {:expected_class => Object, :result_parsed => true})

    if not OBJECTS_TABLE.has_key?(delta['object'])
      next
    end

    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

  EventMachine.run do
    http = EventMachine::HttpRequest.new(path, :connect_timeout => 0, :inactivity_timeout => timeout).get(:keepalive => true)
    http.stream do |chunk|
      parser << chunk
    end
    http.errback do
      raise UnexpectedResponse.new http.error
    end
  end
end

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



118
119
120
121
122
123
124
125
126
127
128
129
130
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
# File 'lib/namespace.rb', line 118

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 = _build_exclude_types(exclude_types)
  end

  # 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|
      if not OBJECTS_TABLE.has_key?(delta['object'])
        next
      end

      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



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

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

#eventsObject



62
63
64
# File 'lib/namespace.rb', line 62

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

#filesObject



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

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

#foldersObject



66
67
68
# File 'lib/namespace.rb', line 66

def folders
  @folders ||= RestfulModelCollection.new(Folder, @_api, @id)
end

#get_cursor(timestamp) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/namespace.rb', line 74

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

#labelsObject



70
71
72
# File 'lib/namespace.rb', line 70

def labels
  @labels ||= RestfulModelCollection.new(Label, @_api, @id)
end

#messagesObject



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

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

#tagsObject



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

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

#threadsObject



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

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