Class: PowerIQ::Resource::Base

Inherits:
RestClient::Resource
  • Object
show all
Defined in:
lib/poweriq_client/resource/base.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url = nil, options = {}, backwards_compatibility = nil, &block) ⇒ Base

Returns a new instance of Base.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/poweriq_client/resource/base.rb', line 29

def initialize(url=nil,options={}, backwards_compatibility=nil, &block)
  base_headers = { :content_type => 'application/json', :accept=>'application/json' }
  if(options.has_key?(:headers))
    options[:headers].merge!(base_headers)
  else
    options[:headers] = base_headers
  end
  options[:user] = self.class.user unless(options.has_key?(:user))
  options[:password] = self.class.password unless(options.has_key?(:password))
  unless(block_given?)
    block = ResponseHandler.create(self)
  end
  modified_url = if(url.nil?)
    self.class.resource_url
  elsif(url=~%r{^https://})
    url
  else
    self.class.resource_url + url
  end
  super(modified_url,options,backwards_compatibility,&block)
end

Class Attribute Details

.hostObject



54
55
56
57
58
59
60
# File 'lib/poweriq_client/resource/base.rb', line 54

def host
  if(self == Base)
    @host
  else
    @host || Base.host
  end
end

.passwordObject



70
71
72
73
74
75
76
# File 'lib/poweriq_client/resource/base.rb', line 70

def password
  if(self == Base)
    @password
  else
    @password || Base.password
  end
end

.singular=(value) ⇒ Object (writeonly)

Sets the attribute singular

Parameters:

  • value

    the value to set the attribute singular to.



52
53
54
# File 'lib/poweriq_client/resource/base.rb', line 52

def singular=(value)
  @singular = value
end

.userObject



62
63
64
65
66
67
68
# File 'lib/poweriq_client/resource/base.rb', line 62

def user
  if(self == Base)
    @user
  else
    @user || Base.user
  end
end

Instance Attribute Details

#requestObject

Returns the value of attribute request.



27
28
29
# File 'lib/poweriq_client/resource/base.rb', line 27

def request
  @request
end

#responseObject

Returns the value of attribute response.



27
28
29
# File 'lib/poweriq_client/resource/base.rb', line 27

def response
  @response
end

Class Method Details

.resource_name(pluralize = false) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/poweriq_client/resource/base.rb', line 78

def resource_name(pluralize=false)
  base = self.name.demodulize
  if(pluralize)
    base = base.pluralize
  end
  base.underscore
end

.resource_urlObject



86
87
88
# File 'lib/poweriq_client/resource/base.rb', line 86

def resource_url
  "https://#{self.host}/api/v2/#{self.resource_name(!self.singular?)}"
end

.singular?Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/poweriq_client/resource/base.rb', line 90

def singular?
  @singular
end

Instance Method Details

#json_available?Boolean

Returns:

  • (Boolean)


103
104
105
# File 'lib/poweriq_client/resource/base.rb', line 103

def json_available?
  self.response && self.response.json && !self.response.json.empty? && self.response.json.has_key?(self.root_key)
end

#member?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/poweriq_client/resource/base.rb', line 99

def member?
  !!url.match(%r{#{self.class.resource_url}/\d+$})
end

#root_key(collection = false) ⇒ Object



95
96
97
# File 'lib/poweriq_client/resource/base.rb', line 95

def root_key(collection=false)
  self.class.resource_name(collection)
end