Class: Pomade::Publisher
- Inherits:
-
Object
- Object
- Pomade::Publisher
- Defined in:
- lib/pomade/publisher.rb
Overview
Handles all interactions to Pomegranate.
Instance Method Summary collapse
-
#authenticate(credentials) ⇒ Object
Sets authentication credentials.
-
#authentication_set? ⇒ Boolean
Check if authentication is set.
-
#initialize(args = {}) ⇒ Publisher
constructor
Creates a new instance of
Publisher
that pushes records to Pomegranate. -
#publish(assets) ⇒ Object
Publishes an array of assets to Pomegranate and returns the results in a
hash
. -
#test_authentication ⇒ Object
Performs a GET request on the Pomegranate instance's feed to ensure login credentials are correct.
-
#test_authentication! ⇒ Object
Raises AuthenticationError if authentication fails.
-
#valid?(assets) ⇒ Boolean
Checks if the assets are valid or not.
-
#validate(assets) ⇒ Object
Validates an array of assets.
Constructor Details
#initialize(args = {}) ⇒ Publisher
Creates a new instance of Publisher
that pushes records to Pomegranate. If you do not set any optional arguments, Pomade will connect to Pomegranate's public sandbox instance.
Parameters
- args (hash, optional)
- :subdomain (string) -- The subdomain for the Pomegranate instance that you'd like to connect to.
- :username (string) -- The username used for connecting to your isntance.
- :password (string) -- The password used for connecting to your isntance.
- :client_id (string) -- Your client ID.
- :skip_authentication (string) -- Skip any authentication tests
- :host (string) -- The host (domain name) that your Pomegranate instance lives on.
- :pathname (string) -- The path that is used for interacting with assets.
- :time_format (strftime) -- Change the layout of the timestamp that is posted to your instance.
- :domain (string) -- NTLM login domain.
Returns
An instance of Pomade::Publisher
Example
@pom = Pomade::Publisher.new('my-subdomain', 'myusername', 'mypassword', 'XX')
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/pomade/publisher.rb', line 32 def initialize(args = {}) @subdomain = args[:subdomain] || 'pomegranate' @username = args[:username] || nil @password = args[:password] || nil @client_id = args[:client_id] || 'P0' # Other options @options = {} @options[:skip_authentication] = args[:skip_authentication] || false @options[:host] = args[:host] || 'timessquare2.com' @options[:pathname] = args[:pathname] || '/p/p.svc/Assets/' @options[:time_format] = args[:time_format] || "%Y-%m-%dT%H:%M:%SZ" @options[:domain] = args[:domain] || nil # Test authentication test_authentication! unless @options[:skip_authentication] end |
Instance Method Details
#authenticate(credentials) ⇒ Object
Sets authentication credentials
Parameters
- credentials (hash)
- :subdomain (string) -- The subdomain for the Pomegranate instance that you'd like to connect to.
- :username (string) -- The username used for connecting to your isntance.
- :password (string) -- The password used for connecting to your isntance.
- :client_id (string) -- Your client ID.
Returns
A boolean
depending on whether the authentication passed or failed.
Example
credz = {
username: "myuser",
password: "mypass",
subdomain: "mysubdomain",
client_id: "XX"
}
@pom.authenticate(opts)
# => true
75 76 77 78 79 80 81 82 |
# File 'lib/pomade/publisher.rb', line 75 def authenticate(credentials) @subdomain = credentials[:subdomain] || @subdomain @username = credentials[:username] || @username @password = credentials[:password] || @password @client_id = credentials[:client_id] || @client_id test_authentication unless @options[:skip_authentication] end |
#authentication_set? ⇒ Boolean
Check if authentication is set
86 87 88 |
# File 'lib/pomade/publisher.rb', line 86 def authentication_set? !@username.nil? && !@password.nil? end |
#publish(assets) ⇒ Object
Publishes an array of assets to Pomegranate and returns the results in a hash
.
Parameters
- assets (array) -- A collection of assets. Each item consists of a hash with three keys:
:target
,:type
and:value
. The values for keys:target
and:value
are both strings while the:type
key's value is a symbol. Available values are::image
-- An `IMAGE type asset:video
-- AVIDEO
type asset:text
-- ATEXT
type asset
Returns
A hash
containing two keys: record_id
and assets
.
Example
assets = [
{ target: "PUB~1text", type: :text, value: "jakebellacera" },
{ target: "PUB~1image", type: :image, value: "http://www.gravatar.com/avatar/98363013aa1237798130bc0fd2c4159d.png" }
]
@pom.publish(records)
# =>
{
record_id: "P0-91c8071a-1201-4f99-bc9d-f8d53a947dc1",
assets: [
{
"AssetID" => "9a24c8e2-1066-42fb-be1c-697c5ead476d",
"AssetData" => "jakebellacera",
"AssetType" => "TEXT",
"Target" => "PUB~1text",
"Client" => "P0",
"Status" => "UPLOADED",
"AssetMeta" => "",
"AssetRecordID" => "P0-91c8071a-1201-4f99-bc9d-f8d53a947dc1"
},
{
"AssetID" => "9a24c8e2-1066-42fb-be1c-697c5ead476c",
"AssetData" => "http://www.gravatar.com/avatar/98363013aa1237798130bc0fd2c4159d.png",
"AssetType" => "IMAGE",
"Target" => "PUB~1image",
"Client" => "P0",
"Status" => "UPLOADED",
"AssetMeta" => "",
"AssetRecordID" => "P0-91c8071a-1201-4f99-bc9d-f8d53a947dc1"
}
]
}
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/pomade/publisher.rb', line 168 def publish(assets) @record_id = generate_record_id @time = Time.now.strftime(@options[:time_format]) # Build our XMLs xmls = [] validate(assets).each do |r| xmls << build_xml(r[:target], r[:type].to_s.upcase, r[:value]) end return { record_id: @record_id, assets: post(xmls) } end |
#test_authentication ⇒ Object
Performs a GET request on the Pomegranate instance's feed to ensure login credentials are correct.
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/pomade/publisher.rb', line 92 def test_authentication status = false Net::HTTP.start("#{@subdomain}.#{@options[:host]}", 80) do |http| req = Net::HTTP::Get.new(@options[:pathname]) if authentication_set? req.ntlm_auth(@username, @options[:domain], @password) end response = http.request(req) if response.code.to_i.between?(200,399) status = true else status = false end end return status end |
#test_authentication! ⇒ Object
Raises AuthenticationError if authentication fails
116 117 118 |
# File 'lib/pomade/publisher.rb', line 116 def test_authentication! raise AuthenticationError unless test_authentication end |
#valid?(assets) ⇒ Boolean
Checks if the assets are valid or not.
Parameters
- assets (array) -- A collection of assets. Each item consists of a hash with three keys:
:target
,:type
and:value
. The values for keys:target
and:value
are both strings while the:type
key's value is a symbol. Available values are::image
-- AnIMAGE
type asset:video
-- AVIDEO
type asset:text
-- ATEXT
type asset
Returns
A boolean
depending on if the assets pass or fail validation.
Example
assets = [
{ target: "PUB~1text", type: :text, value: "jakebellacera" },
{ target: "PUB~1image", type: :image, value: "http://www.gravatar.com/avatar/98363013aa1237798130bc0fd2c4159d.png" }
]
@pom.valid?(records)
# => true
244 245 246 247 248 249 250 251 |
# File 'lib/pomade/publisher.rb', line 244 def valid?(assets) begin validate(assets) return true rescue return false end end |
#validate(assets) ⇒ Object
Validates an array of assets.
Parameters
- assets (array) -- A collection of assets. Each item consists of a hash with three keys:
:target
,:type
and:value
. The values for keys:target
and:value
are both strings while the:type
key's value is a symbol. Available values are::image
-- AnIMAGE
type asset:video
-- AVIDEO
type asset:text
-- ATEXT
type asset
Returns
The array
of assets.
Example
assets = [
{ target: "PUB~1text", type: :text, value: "jakebellacera" },
{ target: "PUB~1image", type: :image, value: "http://www.gravatar.com/avatar/98363013aa1237798130bc0fd2c4159d.png" }
]
@pom.validate(records)
# =>
[
{ target: "PUB~1text", type: :text, value: "jakebellacera" },
{ target: "PUB~1image", type: :image, value: "http://www.gravatar.com/avatar/98363013aa1237798130bc0fd2c4159d.png" }
]
211 212 213 214 215 216 217 218 219 |
# File 'lib/pomade/publisher.rb', line 211 def validate(assets) available_keys = [:target, :type, :value].sort assets.each do |a| raise InvalidAssetKeys, "Each asset should only contain the keys: :target, :type, and :value." unless (a.keys & available_keys).sort == available_keys raise InvalidAssetType, "Invalid asset type. Available choices are: :text, :image and :video." unless [:text, :image, :video].include?(a[:type]) test(a) end end |