-
Notifications
You must be signed in to change notification settings - Fork 1
/
FilterByContentCategory.rb
executable file
·54 lines (47 loc) · 1.37 KB
/
FilterByContentCategory.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/env ruby
# Load in the PrimalAccess class
require './PrimalAccess.rb'
require 'rubygems'
# We require this particular gem
#
# To install it:
# gem install json
#
require 'json'
# Prints things out nicely
require 'pp'
# Constructs the PrimalAccess object so we can talk to Primal
primal = PrimalAccess.new("<your appId>", "<your appKey>",
"<your username>", "<your password>")
#
# Here's the important bit. We're going to parse the JSON response and accept
# only those entries in the dc:collection whose dc:type value is "WebPage"
#
def processJSON(json)
collection = json['dc:collection']
# Select only those entries for which the dc:type is "WebPage" and then transform
# them into strings that look pretty
results = collection.select { |dict|
dict['dc:type'] == "WebPage"
}.collect { |dict|
"title: #{dict['dc:title']}\n" +
"link: #{dict['dc:relation']}\n" +
"source: #{dict['dc:type']}\n\n"
}.each { |result|
puts result
}
end
#
# Call the convenience method that POSTs our topic to Primal and then filters
# the content against the resulting interest network.
#
code, body = primal.postThenFilter("/travel/adventure")
# If successful
if code == 200
# Convert the payload to JSON
json = JSON.parse(body)
# Process the result
processJSON(json)
else
puts "Something went wrong #{code} -- #{body}"
end