Class: AppnexusApi::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/appnexusapi/resource.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(json, service, dbg_info = nil) ⇒ Resource

Returns a new instance of Resource.



5
6
7
8
9
# File 'lib/appnexusapi/resource.rb', line 5

def initialize(json, service, dbg_info = nil)
  @raw_json = json
  @service = service
  @dbg_info = dbg_info
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/appnexusapi/resource.rb', line 26

def method_missing(sym, *args, &block)
  if @raw_json.respond_to?(sym)
    @raw_json.send(sym, *args, &block)
  elsif @raw_json.key?(sym.to_s)
    @raw_json[sym.to_s]
  else
    super(sym, *args, &block)
  end
end

Instance Attribute Details

#dbg_infoObject (readonly)

Returns the value of attribute dbg_info.



3
4
5
# File 'lib/appnexusapi/resource.rb', line 3

def dbg_info
  @dbg_info
end

#raw_jsonObject (readonly)

Returns the value of attribute raw_json.



3
4
5
# File 'lib/appnexusapi/resource.rb', line 3

def raw_json
  @raw_json
end

Instance Method Details

#delete(route_params = {}) ⇒ Object



22
23
24
# File 'lib/appnexusapi/resource.rb', line 22

def delete(route_params = {})
  @service.delete(id, route_params)
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/appnexusapi/resource.rb', line 36

def respond_to_missing?(method_name, include_private = false)
  @raw_json.respond_to?(method_name) || @raw_json.key?(method_name.to_s) || super
end

#saveObject

If you have modified the @raw_json hash in place, you can just do resource.save



17
18
19
20
# File 'lib/appnexusapi/resource.rb', line 17

def save
  @service.update(id, {}, @raw_json).raw_json
  self
end

#to_sObject



40
41
42
# File 'lib/appnexusapi/resource.rb', line 40

def to_s
  @raw_json.inspect
end

#update(route_params = {}, body_params = {}) ⇒ Object



11
12
13
14
# File 'lib/appnexusapi/resource.rb', line 11

def update(route_params = {}, body_params = {})
  @raw_json = @service.update(id, route_params, body_params).raw_json
  self
end