Class: TentClient::Post

Inherits:
Object
  • Object
show all
Defined in:
lib/tent-client/post.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, options = {}) ⇒ Post

Returns a new instance of Post.



4
5
6
# File 'lib/tent-client/post.rb', line 4

def initialize(client, options = {})
  @client, @request_method = client, options.delete(:request_method)
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



3
4
5
# File 'lib/tent-client/post.rb', line 3

def client
  @client
end

#request_methodObject (readonly)

Returns the value of attribute request_method.



3
4
5
# File 'lib/tent-client/post.rb', line 3

def request_method
  @request_method
end

Instance Method Details

#children(entity, post_id, params = {}, options = {}, &block) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/tent-client/post.rb', line 87

def children(entity, post_id, params = {}, options = {}, &block)
  # TODO: handle options[:page] => :first || :last || page-id

  params = { :entity => entity, :post => post_id }.merge(params)

  new_block = proc do |request|
    request.headers['Accept'] = POST_CHILDREN_CONTENT_TYPE
    yield(request) if block_given?
  end

  client.http.send(request_method || :get, :post, params, &new_block)
end

#create(data, params = {}, options = {}, &block) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/tent-client/post.rb', line 33

def create(data, params = {}, options = {}, &block)
  if (Array === (attachments = options.delete(:attachments))) && attachments.any?
    parts = multipart_parts(data, attachments)
    client.http.multipart_request(:post, :new_post, params, parts, &block)
  else
    client.http.post(:new_post, params, data, &block)
  end
end

#delete(entity, post_id, params = {}, &block) ⇒ Object



25
26
27
# File 'lib/tent-client/post.rb', line 25

def delete(entity, post_id, params = {}, &block)
  client.http.delete(:post, { :entity => entity, :post => post_id }.merge(params), &block)
end

#get(entity, post_id, params = {}, &block) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/tent-client/post.rb', line 12

def get(entity, post_id, params = {}, &block)
  new_block = proc do |request|
    request.headers['Accept'] = POST_MEDIA_TYPE
    yield(request) if block_given?
  end

  client.http.send(request_method || :get, :post, { :entity => entity, :post => post_id }.merge(params), &new_block)
end

#get_attachment(entity, post_id, attachment_name, params = {}, &block) ⇒ Object



21
22
23
# File 'lib/tent-client/post.rb', line 21

def get_attachment(entity, post_id, attachment_name, params = {}, &block)
  client.http.send(request_method || :get, :post_attachment, { :entity => entity, :post => post_id, :name => attachment_name }.merge(params), &block)
end

#headObject



8
9
10
# File 'lib/tent-client/post.rb', line 8

def head
  self.class.new(client, :request_method => :head)
end

#list(params = {}, &block) ⇒ Object



29
30
31
# File 'lib/tent-client/post.rb', line 29

def list(params = {}, &block)
  client.http.send(request_method || :get, :posts_feed, params, &block)
end

#mentions(entity, post_id, params = {}, options = {}, &block) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/tent-client/post.rb', line 61

def mentions(entity, post_id, params = {}, options = {}, &block)
  # TODO: handle options[:page] => :first || :last || page-id

  params = { :entity => entity, :post => post_id }.merge(params)

  new_block = proc do |request|
    request.headers['Accept'] = POST_MENTIONS_CONTENT_TYPE
    yield(request) if block_given?
  end

  client.http.send(request_method || :get, :post, params, &new_block)
end

#update(entity, post_id, data, params = {}, options = {}, &block) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/tent-client/post.rb', line 42

def update(entity, post_id, data, params = {}, options = {}, &block)
  params = { :entity => entity, :post => post_id }.merge(params)
  if (Array === (attachments = options.delete(:attachments))) && attachments.any?
    parts = multipart_parts(data, attachments, options)
    client.http.multipart_request(:put, :post, params, parts, &block)
  else
    new_block = proc do |request|
      if options.delete(:import)
        request.options['tent.import'] = true
      elsif options.delete(:notification)
        request.options['tent.notification'] = true
      end
      yield(request) if block_given?
    end

    client.http.put(:post, params, data, &new_block)
  end
end

#versions(entity, post_id, params = {}, options = {}, &block) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/tent-client/post.rb', line 74

def versions(entity, post_id, params = {}, options = {}, &block)
  # TODO: handle options[:page] => :first || :last || page-id

  params = { :entity => entity, :post => post_id }.merge(params)

  new_block = proc do |request|
    request.headers['Accept'] = POST_VERSIONS_CONTENT_TYPE
    yield(request) if block_given?
  end

  client.http.send(request_method || :get, :post, params, &new_block)
end