Class: Rapidash::Base

Inherits:
Object
  • Object
show all
Includes:
Resourceable, Urlable
Defined in:
lib/rapidash/base.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Resourceable

included, #resource, #resource!

Methods included from Urlable

included

Constructor Details

#initialize(*args) ⇒ Base

Returns a new instance of Base.



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/rapidash/base.rb', line 16

def initialize(*args)
  @client, @id, options = args

  if @id.is_a?(Hash)
    options = @id
    @id = nil
  end

  @options ||= {}
  @options.merge!(options || {})
  @url = "#{base_url}#{resource_url}"
  @url += "/#{@id}" if @id
end

Class Attribute Details

.root_elementObject

Returns the value of attribute root_element.



9
10
11
# File 'lib/rapidash/base.rb', line 9

def root_element
  @root_element
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



6
7
8
# File 'lib/rapidash/base.rb', line 6

def client
  @client
end

#optionsObject

Returns the value of attribute options.



6
7
8
# File 'lib/rapidash/base.rb', line 6

def options
  @options
end

#urlObject

Returns the value of attribute url.



6
7
8
# File 'lib/rapidash/base.rb', line 6

def url
  @url
end

Class Method Details

.root(name) ⇒ Object



11
12
13
# File 'lib/rapidash/base.rb', line 11

def root(name)
  @root_element = name.to_sym
end

Instance Method Details

#call!Object



48
49
50
51
52
53
54
55
# File 'lib/rapidash/base.rb', line 48

def call!
  self.options ||= {}
  options.delete(:previous_url)
  options[:headers] ||= {}
  options[:headers]["content-type"] = "application/json"
  method = options.delete(:method) || :get
  client.send(method, url, options)
end

#create!(params) ⇒ Object



30
31
32
33
34
# File 'lib/rapidash/base.rb', line 30

def create!(params)
  options[:method] = :post
  set_body!(params)
  call!
end

#delete!Object



42
43
44
45
# File 'lib/rapidash/base.rb', line 42

def delete!
  options[:method] = :delete
  call!
end

#update!(params) ⇒ Object



36
37
38
39
40
# File 'lib/rapidash/base.rb', line 36

def update!(params)
  options[:method] = client.class.patch ? :patch : :put
  set_body!(params)
  call!
end