Class: Purest::APIMethods

Inherits:
Rest
  • Object
show all
Defined in:
lib/purest/api_methods.rb

Instance Method Summary collapse

Methods inherited from Rest

access_method?, #authenticated?, #concat_url, #initialize, #logout, method_missing, #use_named_parameter

Constructor Details

This class inherits a constructor from Purest::Rest

Instance Method Details

#append_path(url, options) ⇒ Object



5
6
7
8
9
10
# File 'lib/purest/api_methods.rb', line 5

def append_path(url, options)
  options.each do |path|
    new_path = path.to_s.gsub('show_', '')
    url.map! { |u| u + "/#{new_path}" } if !@options.nil? && @options[path]
  end
end

#create(options = nil, path = nil, appended_path = nil) ⇒ Object

Create a resource, POST

Parameters:

  • options (Hash) (defaults to: nil)

    options to pass in

  • path (String) (defaults to: nil)

    the endpoint to send to

  • appended_path (String) (defaults to: nil)

    additional paths for some endpoints



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/purest/api_methods.rb', line 52

def create(options = nil, path = nil, appended_path = nil)
  @options = options
  create_session unless authenticated?

  raw_resp = @conn.post do |req|
    # Base URL + path
    url = ["/api/#{Purest.configuration.api_version}/#{path}"]

    # Base URL + path + name, if supplied
    url.map! { |u| u + "/#{@options[:name]}" } if @options[:name]

    # Base URL + path + appended_path, if supplied
    url.map! { |u| u + "/#{appended_path}" } if appended_path

    # Turn whatever options we have into JSON
    req.body = @options.to_json

    # Set the request URL, and finally make the request
    req.url concat_url url
  end

  JSON.parse(raw_resp.body, symbolize_names: true)
end

#delete(options = nil, path = nil, appended_path = nil) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/purest/api_methods.rb', line 102

def delete(options = nil, path = nil, appended_path = nil)
  @options = options

  raw_resp = @conn.delete do |req|
    url = "/api/#{Purest.configuration.api_version}/#{path}"
    url += "/#{@options[:name]}" if @options[:name]
    url += "/#{appended_path}" if appended_path

    req.url url
  end

  # Unfortuantely this can't all be done in a single request, so if you
  # want to eradicate a subsequent request has to be made :(
  if @options[:eradicate]
    raw_resp = @conn.delete do |req|
      req.url "/api/#{Purest.configuration.api_version}/#{path}/#{@options[:name]}"
      req.body = @options.to_json
    end
  end

  JSON.parse(raw_resp.body, symbolize_names: true)
end

#get(options = nil, path = nil, params = nil, appended_paths = nil) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/purest/api_methods.rb', line 12

def get(options = nil, path = nil, params = nil, appended_paths = nil)
  @options = options
  create_session unless authenticated?

  raw_resp = @conn.get do |req|
    url = ["/api/#{Purest.configuration.api_version}/#{path}"]

    # Name is pretty universal, since most endpoints allow you to query
    # specific items, e.g. /hosts/host1 or /volume/volume1
    # where host1 and volume1 are names
    url.map! { |u| u + "/#{@options[:name]}" } if !@options.nil? && @options[:name]

    # Here we append the various paths, based on available GET endpoints
    append_path(url, appended_paths) unless appended_paths.nil?

    # Generate methods for url building based on
    # params passed in, e.g. [:pending, :action] becomes
    # use_pending and use_action
    params.each do |param|
      self.class.send(:define_method, :"use_#{param}") do |options|
        options ? use_named_parameter(param, options[param]) : []
      end
    end

    # Generate array, consisting of url parts, to be built
    # by concat_url method below
    params.each do |param|
      url += send(:"use_#{param}", @options)
    end

    req.url concat_url url
  end

  JSON.parse(raw_resp.body, symbolize_names: true)
end

#update(options = nil, path = nil, appended_path = nil) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/purest/api_methods.rb', line 76

def update(options = nil, path = nil, appended_path = nil)
  @options = options
  create_session unless authenticated?

  raw_resp = @conn.put do |req|
    # Base URL + path
    url = "/api/#{Purest.configuration.api_version}/#{path}"

    # Base URL + path + name, if supplied
    url += "/#{@options[:name]}" if @options[:name]

    # Base URL + appended path
    url += "/#{appended_path}" if appended_path

    # Since :name and :new_name are used the same throughout almost every class
    # it seems reasonable to do this here
    @options[:name] = @options.delete(:new_name) if !@options.nil? && @options[:new_name]

    req.body = @options.to_json

    req.url url
  end

  JSON.parse(raw_resp.body, symbolize_names: true)
end