class ShareProgress::Button
Attributes
advanced_options[RW]
auto_fill[RW]
errors[R]
id[RW]
include[RW]
is_active[RW]
page_title[RW]
page_url[RW]
variants[RW]
Public Class Methods
all(limit: 100, offset: 0)
click to toggle source
# 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_keys()
click to toggle source
# File lib/share_progress/button.rb, line 44 def allowed_keys optional_keys + required_keys end
create(page_url:, button_template:, **options)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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
new(params)
click to toggle source
# File lib/share_progress/button.rb, line 69 def initialize(params) @variant_collection = VariantCollection.new update_attributes(params) end
update(options={})
click to toggle source
this method is used by instance.save and Button.create
# 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
Private Class Methods
advanced_options_keys()
click to toggle source
# File lib/share_progress/button.rb, line 64 def advanced_options_keys [:automatic_traffic_routing, :buttons_optimize_actions, :custom_params, :prompt] end
endpoint(method=nil)
click to toggle source
# File lib/share_progress/button.rb, line 50 def endpoint(method=nil) extension = method.nil? ? "" : "/#{method}" "/buttons#{extension}" end
optional_keys()
click to toggle source
# File lib/share_progress/button.rb, line 60 def optional_keys [:id, :page_title, :auto_fill, :variants, :advanced_options, :share_button_html, :is_active, :errors] end
required_keys()
click to toggle source
currently no validation, but worth noting that they're different
# File lib/share_progress/button.rb, line 56 def required_keys [:page_url, :button_template] end
Public Instance Methods
add_or_update_variant(variant)
click to toggle source
# File lib/share_progress/button.rb, line 96 def add_or_update_variant(variant) @variant_collection.add_or_update(variant) end
destroy()
click to toggle source
# File lib/share_progress/button.rb, line 108 def destroy self.class.destroy(id) ? self : false end
find_variant(variant)
click to toggle source
# File lib/share_progress/button.rb, line 104 def find_variant(variant) @variant_collection.find(variant) end
remove_variant(variant)
click to toggle source
# File lib/share_progress/button.rb, line 100 def remove_variant(variant) @variant_collection.remove(variant) end
save()
click to toggle source
# 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)
click to toggle source
# 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
variants=(variants)
click to toggle source
# File lib/share_progress/button.rb, line 88 def variants=(variants) @variant_collection.update_variants(variants) end
Private Instance Methods
serialize()
click to toggle source
# File lib/share_progress/button.rb, line 114 def serialize serialized = Hash.new self.class.allowed_keys.each do |key| value = send(key) serialized[key] = value unless value.nil? end serialized end