Class: Primal::PrimalAccess
- Inherits:
-
Object
- Object
- Primal::PrimalAccess
- Includes:
- HTTParty
- Defined in:
- lib/primal/PrimalAccess.rb
Overview
The PrimalAccess class abstracts the access to Primal such that you can call simple methods on it to get what you need.
Constant Summary collapse
- @@debugMe =
Uncomment this next line to see what HTTParty is doing debug_output $stderr Set this to false in order to turn off debugging of this class
true
Instance Method Summary collapse
-
#extractJustTopic(topic) ⇒ Object
Sometimes we’re going to get topics that are complex (i.e. they contain a scheme and host) and we want to simplify those.
-
#filterContent(topic, opts = {}) ⇒ Object
Uses the pre-existing topic to filter the default source of content through the interest network defined by the topic.
-
#initialize(appId, appKey, username, password) ⇒ PrimalAccess
constructor
Constructor for the PrimalAccess class.
-
#postNewTopic(topic, opts = {}) ⇒ Object
POSTs a new topic to Primal in order to seed that topic.
-
#postThenFilter(topic, opts = {}) ⇒ Object
This is a convenience method that will POST the topic to Primal and then filter the default source of content through the resulting interest network.
Constructor Details
#initialize(appId, appKey, username, password) ⇒ PrimalAccess
Constructor for the PrimalAccess class
Pass in the username and password of the user you’re going to access in order to construct and object that will work with that user
24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/primal/PrimalAccess.rb', line 24 def initialize(appId, appKey, username, password) @headers = { :headers => { 'Primal-App-ID' => appId, 'Primal-App-Key' => appKey }, :basic_auth => { :username => username, :password => password } } end |
Instance Method Details
#extractJustTopic(topic) ⇒ Object
Sometimes we’re going to get topics that are complex (i.e. they contain a scheme and host) and we want to simplify those. Because we’re not making calls with bare URLs but have told HTTParty what the base_uri is, we need to pull that base uri off of the topic, should it be there.
43 44 45 |
# File 'lib/primal/PrimalAccess.rb', line 43 def extractJustTopic(topic) topic.sub(%r{https://.*?/}, '/') end |
#filterContent(topic, opts = {}) ⇒ Object
Uses the pre-existing topic to filter the default source of content through the interest network defined by the topic.
The given parameter will be used to construct a GET URL that looks like “/topic”
You can pass a dictionary of optional arguments that will be merged in to the query parameters, if you wish. e.g.
{ :"primal:contentScore:min" => 0.7 }
{ :"primal:contentCount:max" => 5 }
{ :contentSource => MyDataSource } ... or ...
{ :contentSource => PrimalSource }
Returns two values: the response code, and the body. If successful (i.e. a response code of 200) then the body will be the JSON payload of the filtered content.
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/primal/PrimalAccess.rb', line 124 def filterContent(topic, opts = {}) topic = extractJustTopic(topic) count = 0 code = 400 body = '' = @headers.merge({ :query => { :timeOut => 'max' }.merge(opts) }) while (count < 10) if @@debugMe $stderr.puts "GETting #{topic}" end response = self.class.get("#{topic}", ) code = response.code body = response.body # # 400 - bad request # 401 - application not authorized to access the user's account # 403 - application not authorized to use Primal # 404 - object not found # if code >= 400 && code <= 404 if @@debugMe $stderr.puts "GET received a #{code}" end break # # 429 - application has reached its request limit for the moment # elsif code == 429 if @@debugMe $stderr.puts "Got a 429. Waiting (#{count})." end # Sleep for 10 seconds sleep 10 # We don't allow as many retries when we might be throttled count += 2 # # 200 - success # elsif code == 200 if @@debugMe $stderr.puts "Results are complete" end break # # We don't know what happened but it can't be good # else abort "Received unexpected response code (#{code}) for GET #{topic}" end end return code, body end |
#postNewTopic(topic, opts = {}) ⇒ Object
POSTs a new topic to Primal in order to seed that topic.
The ‘topic’ parameter will be used to construct a POST URL that looks like “/topic”
Returns two values: the response code and the body. Anything but a response code of 201 is to be considered an error.
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/primal/PrimalAccess.rb', line 57 def postNewTopic(topic, opts = {}) topic = extractJustTopic(topic) count = 0 code = 400 body = '' = @headers.merge(opts) while (count < 5) if @@debugMe $stderr.puts "POSTing to #{topic}" end response = self.class.post("#{topic}", ) code = response.code body = response.body # # 400 - bad request # 401 - application not authorized to access the user's account # 403 - application not authorized to use Primal # if code >= 400 && code <= 403 if @@debugMe $stderr.puts "POST received a #{code}" end break # # 429 - application has reached its request limit for the moment # elsif code == 429 # Sleep for 10 seconds if @@debugMe $stderr.puts "Got a 429. Waiting (#{count})." end sleep 10 count += 1 # # 201 - success # elsif code == 201 if @@debugMe $stderr.puts "POST successful" end break else abort "Received unexpected response code (#{code}) for POST #{uri}" end end return code, body end |
#postThenFilter(topic, opts = {}) ⇒ Object
This is a convenience method that will POST the topic to Primal and then filter the default source of content through the resulting interest network.
The response from this method is a bit less clear than using a POST and filter explicitly, since you may not know which one of the two operations has failed (assuming a failure).
Returns two values: the response code and the body. The only successful response code from this method is 200. If successful then the body contains the JSON payload of the filtered content.
194 195 196 197 198 199 200 |
# File 'lib/primal/PrimalAccess.rb', line 194 def postThenFilter(topic, opts = {}) code, body = postNewTopic(topic) if code == 201 code, body = filterContent(topic, opts) end return code, body end |