Class: PhoneGap::Build::RestResource

Inherits:
Base
  • Object
show all
Includes:
Creatable
Defined in:
lib/phone_gap/build/rest_resource.rb

Direct Known Subclasses

App

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Creatable

#creatable_attributes, included, #updatable_attributes

Constructor Details

#initialize(params = {}) ⇒ RestResource

Returns a new instance of RestResource.



13
14
15
16
17
# File 'lib/phone_gap/build/rest_resource.rb', line 13

def initialize(params = {})
  @poll_time_limit = 120
  @poll_interval = 5
  super(params)
end

Instance Attribute Details

#errorsObject



54
55
56
# File 'lib/phone_gap/build/rest_resource.rb', line 54

def errors
  @errors ||= []
end

#idObject (readonly)

Returns the value of attribute id.



9
10
11
# File 'lib/phone_gap/build/rest_resource.rb', line 9

def id
  @id
end

#poll_intervalObject

Returns the value of attribute poll_interval.



10
11
12
# File 'lib/phone_gap/build/rest_resource.rb', line 10

def poll_interval
  @poll_interval
end

#poll_time_limitObject

Returns the value of attribute poll_time_limit.



10
11
12
# File 'lib/phone_gap/build/rest_resource.rb', line 10

def poll_time_limit
  @poll_time_limit
end

Instance Method Details

#as_json(params = {}) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/phone_gap/build/rest_resource.rb', line 42

def as_json(params = {})
  if params[:only]
    json = params[:only].inject({}) do | memo, attribute_name|
      memo[attribute_name[1..-1].to_sym] = instance_variable_get(attribute_name)
      memo
    end
  else
    json = {}
  end
  params[:remove_nils] ? json.delete_if {|k, v| v.nil? } : json
end

#createObject



19
20
21
22
23
24
25
26
27
28
# File 'lib/phone_gap/build/rest_resource.rb', line 19

def create
  response = ApiRequest.new.post(path, post_options)
  if response.success?
    populate_from_json(JSON.parse(response.body))
    self
  else
    update_errors(JSON.parse(response.body))
    false
  end
end

#destroyObject



38
39
40
# File 'lib/phone_gap/build/rest_resource.rb', line 38

def destroy
  ApiRequest.new.delete(path)
end

#saveObject



34
35
36
# File 'lib/phone_gap/build/rest_resource.rb', line 34

def save
  @id ? update : create
end

#updateObject



30
31
32
# File 'lib/phone_gap/build/rest_resource.rb', line 30

def update
  ApiRequest.new.put(path, query: {data: as_json(only: updatable_attributes)})
end