Class: ShareProgress::Button

Inherits:
Object
  • Object
show all
Defined in:
lib/share_progress/button.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Button

Returns a new instance of Button.



69
70
71
72
# File 'lib/share_progress/button.rb', line 69

def initialize(params)
  @variant_collection = VariantCollection.new
  update_attributes(params)
end

Instance Attribute Details

#advanced_optionsObject

Returns the value of attribute advanced_options.



10
11
12
# File 'lib/share_progress/button.rb', line 10

def advanced_options
  @advanced_options
end

#auto_fillObject

Returns the value of attribute auto_fill.



10
11
12
# File 'lib/share_progress/button.rb', line 10

def auto_fill
  @auto_fill
end

#button_templateObject

Returns the value of attribute button_template.



10
11
12
# File 'lib/share_progress/button.rb', line 10

def button_template
  @button_template
end

#errorsObject (readonly)

Returns the value of attribute errors.



11
12
13
# File 'lib/share_progress/button.rb', line 11

def errors
  @errors
end

#idObject

Returns the value of attribute id.



10
11
12
# File 'lib/share_progress/button.rb', line 10

def id
  @id
end

#includeObject

Returns the value of attribute include.



10
11
12
# File 'lib/share_progress/button.rb', line 10

def include
  @include
end

#is_activeObject

Returns the value of attribute is_active.



10
11
12
# File 'lib/share_progress/button.rb', line 10

def is_active
  @is_active
end

#page_titleObject

Returns the value of attribute page_title.



10
11
12
# File 'lib/share_progress/button.rb', line 10

def page_title
  @page_title
end

#page_urlObject

Returns the value of attribute page_url.



10
11
12
# File 'lib/share_progress/button.rb', line 10

def page_url
  @page_url
end

#share_button_htmlObject

Returns the value of attribute share_button_html.



10
11
12
# File 'lib/share_progress/button.rb', line 10

def share_button_html
  @share_button_html
end

#variantsObject

Returns the value of attribute variants.



10
11
12
# File 'lib/share_progress/button.rb', line 10

def variants
  @variants
end

Class Method Details

.all(limit: 100, offset: 0) ⇒ Object



39
40
41
42
# File 'lib/share_progress/button.rb', line 39

def all(limit: 100, offset: 0)
  matches = Client.get endpoint, { query: { limit: limit, offset: offset } }
  matches.map{ |match| new(match) }
end

.allowed_keysObject



44
45
46
# File 'lib/share_progress/button.rb', line 44

def allowed_keys
  optional_keys + required_keys
end

.create(page_url:, button_template:, **options) ⇒ Object



15
16
17
18
# File 'lib/share_progress/button.rb', line 15

def create(page_url:, button_template:, **options)
  created = update(options.merge(page_url: page_url, button_template: button_template))
  created.nil? ? new({}) : new(created)
end

.destroy(id) ⇒ Object



34
35
36
37
# File 'lib/share_progress/button.rb', line 34

def destroy(id)
  deleted = Client.post endpoint('delete'), { query: { id: id } }
  (deleted.size > 0)
end

.find(id) ⇒ Object

Raises:



28
29
30
31
32
# File 'lib/share_progress/button.rb', line 28

def find(id)
  matches = Client.get endpoint('read'), { query: { id: id } }
  raise RecordNotFound.new("No button exists with id #{id}") if matches.size < 1
  new(matches[0])
end

.update(options = {}) ⇒ Object

this method is used by instance.save and Button.create



21
22
23
24
25
26
# File 'lib/share_progress/button.rb', line 21

def update(options={})
  Utils.filter_keys(options, allowed_keys)
  Utils.filter_keys(options[:advanced_options], advanced_options_keys)
  created = Client.post endpoint('update'), { body: options }
  created[0] # the API returns a list of length 1
end

Instance Method Details

#add_or_update_variant(variant) ⇒ Object



96
97
98
# File 'lib/share_progress/button.rb', line 96

def add_or_update_variant(variant)
  @variant_collection.add_or_update(variant)
end

#destroyObject



108
109
110
# File 'lib/share_progress/button.rb', line 108

def destroy
  self.class.destroy(id) ? self : false
end

#find_variant(variant) ⇒ Object



104
105
106
# File 'lib/share_progress/button.rb', line 104

def find_variant(variant)
  @variant_collection.find(variant)
end

#remove_variant(variant) ⇒ Object



100
101
102
# File 'lib/share_progress/button.rb', line 100

def remove_variant(variant)
  @variant_collection.remove(variant)
end

#saveObject



82
83
84
85
86
# File 'lib/share_progress/button.rb', line 82

def save
  result = self.class.update(serialize)
  update_attributes(result)
  (errors.size == 0)
end

#update_attributes(params) ⇒ Object



74
75
76
77
78
79
80
# File 'lib/share_progress/button.rb', line 74

def update_attributes(params)
  params = Utils.symbolize_keys(params)
  params.each_pair do |key, value|
    instance_variable_set("@#{key}", value) unless key == :variants
  end
  self.variants = params[:variants] if params.include? :variants
end