Class: Textmagic::REST::ListResource

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/textmagic-ruby/rest/list_resource.rb

Instance Method Summary collapse

Methods included from Utils

#key_map, #resource, #to_camel_case, #to_underscore_case

Constructor Details

#initialize(path, client) ⇒ ListResource

Returns a new instance of ListResource.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/textmagic-ruby/rest/list_resource.rb', line 6

def initialize(path, client)
  custom_names = {
      'Replies' => 'Reply'
  }
  @path, @client = path, client
  resource_name = self.class.name.split('::')[-1]
  instance_name = custom_names.fetch(resource_name, resource_name.chop)

  parent_module = self.class.to_s.split('::')[-2]
  full_module_path = if parent_module == 'REST'
                       Textmagic::REST
                     else
                       Textmagic::REST.const_get parent_module
                     end

  @instance_class = full_module_path.const_get instance_name
  @list_key, @instance_id_key = to_underscore_case(resource_name), 'id'
end

Instance Method Details

#create(params = {}) ⇒ Object



47
48
49
50
# File 'lib/textmagic-ruby/rest/list_resource.rb', line 47

def create(params={})
  response = @client.post "#{@path}", params
  @instance_class.new "#{@path}", @client, response
end

#delete(uid, params = {}) ⇒ Object



57
58
59
60
# File 'lib/textmagic-ruby/rest/list_resource.rb', line 57

def delete(uid, params={})
  raise "Can't delete a resource without a REST Client" unless @client
  response = @client.delete "#{@path}/#{uid}", params
end

#get(uid, params = {}) ⇒ Object



42
43
44
45
# File 'lib/textmagic-ruby/rest/list_resource.rb', line 42

def get(uid, params={})
  response = @client.get "#{@path}/#{uid}", params
  @instance_class.new "#{@path}", @client, response
end

#inspectObject

:nodoc:



25
26
27
# File 'lib/textmagic-ruby/rest/list_resource.rb', line 25

def inspect # :nodoc:
  "<#{self.class} @path=#{@path}>"
end

#list(params = {}) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/textmagic-ruby/rest/list_resource.rb', line 29

def list(params={})
  if params.key?('search') or params.key?(:search)
    [:search, 'search'].each do |search|
      params.delete(search)
    end
    path = "#{@path}" << '/search'
  else
    path = "#{@path}"
  end
  response = @client.get path, params
  PaginateResource.new "#{@path}", @client, response, @instance_class
end

#update(uid, params = {}) ⇒ Object



52
53
54
55
# File 'lib/textmagic-ruby/rest/list_resource.rb', line 52

def update(uid, params={})
  response = @client.put "#{@path}/#{uid}", params
  @instance_class.new "#{@path}", @client, response
end