Created
August 2, 2014 22:42
-
-
Save zmajstor/1efc359795726c63def7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# builder pattern in ruby | |
# inspired by https://github.com/tboyko/ios_config | |
# and http://blog.rubygeek.com/2009/11/24/builder-pattern-in-ruby/ | |
module Payload | |
# provide base for building Payloads in Configuration Profile | |
class Base | |
def build | |
raise ArgumentError, "missing identifier" unless identifier | |
Hash.new.tap do |key| | |
key['PayloadVersion'] = 1 # do not modify | |
# key['PayloadUUID'] = (uuid || UUIDTools::UUID.random_create).to_s.downcase.delete("^a-z0-9\.") | |
key['PayloadUUID'] = (uuid || rand(1000)).to_s | |
key['PayloadIdentifier'] = identifier | |
key['PayloadType'] = payload_type | |
# optional | |
key['PayloadOrganization'] = organization if organization | |
key['PayloadDisplayName'] = display_name if display_name | |
key['PayloadDescription'] = description if description | |
end | |
end | |
end | |
end | |
module Payload | |
# Webclip payload in Configuration Profile | |
class WebclipBuilder < Base | |
attr_accessor :identifier, :payload_type, :display_name, :description, :organization, :uuid, | |
:url, :icon, :label | |
def initialize(&block) | |
# default values | |
@payload_type = "com.apple.webClip.managed" # do not modify | |
@identifier = 'webclip.intranet' | |
# set values from a block | |
instance_eval &block if block_given? | |
end | |
def build | |
super.tap do |key| | |
key['IsRemovable'] = true # allow user to remove webclip | |
key["Icon"] = StringIO.new(Base64.decode64(icon)) if icon | |
key['Label'] = label | |
key['URL'] = url | |
end | |
end | |
end | |
end | |
# Configuration Profile model, a container for payloads | |
class ProfileBuilder < Payload::Base | |
attr_accessor :identifier, :payload_type, :display_name, :description, :organization, :uuid | |
def initialize(&block) | |
# default values | |
@payload_type = "Configuration" | |
@organization = "ProMDM" | |
# set values from a block | |
instance_eval &block if block_given? | |
@payloads = [] | |
end | |
def add_payload(payload) | |
payload.identifier = "#{identifier}.#{payload.identifier}" | |
payload.organization = payload.organization || organization | |
@payloads << payload | |
end | |
def build | |
super.tap do |key| | |
key['PayloadContent'] = @payloads.map(&:build) | |
end | |
end | |
end | |
# usage: | |
profile = ProfileBuilder.new { |p| p.identifier = "net.promdm.profile1" } | |
webclip = Payload::WebclipBuilder.new do |w| | |
w.identifier = "asss" | |
w.label = "Labela" | |
end | |
profile.add_payload webclip | |
# webclip.identifier = "override" | |
# p webclip.build | |
p profile.build | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment