pomade 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/pomade.rb +129 -0
  2. metadata +78 -0
@@ -0,0 +1,129 @@
1
+ require 'ntlm/http'
2
+ require 'nokogiri'
3
+
4
+ class Pomade
5
+ def initialize(subdomain, username, password, client_id, opts = {})
6
+ @subdomain = subdomain
7
+ @username = username
8
+ @password = password
9
+ @client_id = client_id
10
+
11
+ # Other options
12
+ @options = {}
13
+ @options[:domain] = opts[:domain] || 'timessquare2.com'
14
+ @options[:pathname] = opts[:pathname] || '/p/p.svc/Assets/'
15
+ @options[:time_format] = opts[:time_format] || "%Y-%m-%dT%H:%M:%SZ"
16
+ @options[:login_domain] = opts[:login_domain] || nil
17
+ @options[:debug] = opts[:debug] || false
18
+ end
19
+
20
+ def publish(record)
21
+ # You should set up a record like this:
22
+ # { :id, :data => [ {:target, :type, :value}, ... ] }
23
+ puts "Available options: #{@options}" if @options[:debug]
24
+ @time = Time.now.strftime(@options[:time_format])
25
+
26
+ xmls = []
27
+ record[:data].each do |r|
28
+ xmls << build_xml(record[:id], r[:target], r[:type], r[:value])
29
+ end
30
+
31
+ post(xmls)
32
+ end
33
+
34
+ private
35
+
36
+ def post(data)
37
+ response_data = []
38
+ data.each do |xml|
39
+ req = send_request(xml)
40
+
41
+ if req[:code] == "201"
42
+ puts "===> SUCCESS!" if @options[:debug]
43
+ response_data << req[:data]
44
+ else
45
+ if req[:code] == "401"
46
+ # TODO: Tell user that we couldn't authenticate
47
+ puts "===> ERROR! Authentication" if @options[:debug]
48
+ elsif req[:code] == "400"
49
+ # TODO: Tell the user there was a formatting error
50
+ puts "===> ERROR! Formatting" if @options[:debug]
51
+ else
52
+ # TODO: Tell the user an unknown error has occured
53
+ puts "===> ERROR Unknown" if @options[:debug]
54
+ end
55
+ response_data = false
56
+ break
57
+ end
58
+ end
59
+
60
+ response_data
61
+ end
62
+
63
+ def send_request(body)
64
+ status = false
65
+ data = false
66
+ code = ""
67
+
68
+ puts "Initializing request for #{@subdomain + '.' + @options[:domain]}" if @options[:debug]
69
+ Net::HTTP.start("#{@subdomain}.#{@options[:domain]}", 80) do |http|
70
+ req = Net::HTTP::Post.new(@options[:pathname])
71
+
72
+ req.content_type = 'application/atom+xml'
73
+ req.content_length = body.size - 20 # Currently a bug with the Pomegranate API I believe
74
+ req.body = body
75
+ req.ntlm_auth(@username, @options[:login_domain], @password)
76
+
77
+ response = http.request(req)
78
+ puts response.inspect if @options[:debug]
79
+
80
+ code = response.code
81
+
82
+ if code == "201"
83
+ data = parse_xml(response.body)
84
+ else
85
+ break
86
+ end
87
+ end
88
+
89
+ {:code => code, :data => data}
90
+ end
91
+
92
+ def build_xml(record_id, target, type, value)
93
+ <<-EOF.gsub(/^ {6}/, '')
94
+ <?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>
95
+ <entry
96
+ xml:base="/p/p.svc/"
97
+ xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices"
98
+ xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"
99
+ xmlns="http://www.w3.org/2005/Atom">
100
+ <id></id>
101
+ <title type="text"></title>
102
+ <updated>#{@time}</updated>
103
+ <author><name /></author>
104
+ <category term="pomegranateModel.Asset" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
105
+ <content type="application/xml">
106
+ <m:properties>
107
+ <d:AssetID>--</d:AssetID>
108
+ <d:AssetData>#{value}</d:AssetData>
109
+ <d:AssetType>#{type}</d:AssetType>
110
+ <d:AssetMeta></d:AssetMeta>
111
+ <d:AssetRecordID>#{record_id}</d:AssetRecordID>
112
+ <d:Target>#{target}</d:Target>
113
+ <d:Client>#{@client_id}</d:Client>
114
+ <d:Status>APPROVED</d:Status>
115
+ </m:properties>
116
+ </content>
117
+ </entry>
118
+ EOF
119
+ end
120
+
121
+ def parse_xml(xml)
122
+ parsed_xml = Nokogiri::XML(xml.gsub(/\n|\r| /, ""))
123
+ data = {}
124
+ parsed_xml.css('m|properties').children.each do |p|
125
+ data[p.name] = p.content
126
+ end
127
+ data
128
+ end
129
+ end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pomade
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jake Bellacera
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-09-04 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: ruby-ntlm
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 0.0.1
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 0.0.1
30
+ - !ruby/object:Gem::Dependency
31
+ name: nokogiri
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 1.5.5
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 1.5.5
46
+ description: Ruby wrapper for TimeSquare2's Pomegranate API
47
+ email: hi@jakebellacera.com
48
+ executables: []
49
+ extensions: []
50
+ extra_rdoc_files: []
51
+ files:
52
+ - lib/pomade.rb
53
+ homepage: http://github.com/jakebellacera/pomade
54
+ licenses: []
55
+ post_install_message:
56
+ rdoc_options: []
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ! '>='
63
+ - !ruby/object:Gem::Version
64
+ version: 1.9.3
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ! '>='
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ requirements: []
72
+ rubyforge_project:
73
+ rubygems_version: 1.8.24
74
+ signing_key:
75
+ specification_version: 3
76
+ summary: Pomegranate API Wrapper
77
+ test_files: []
78
+ has_rdoc: false