Class: Graphcommons::API
- Inherits:
-
Object
- Object
- Graphcommons::API
- Defined in:
- lib/graphcommons.rb
Overview
Low-level wrapper for API calls.
Constant Summary collapse
- @@apiurl =
"https://graphcommons.com/api/v1"
Class Method Summary collapse
-
.check_key ⇒ Object
Checks API key to /^sk_.22$/ test.
-
.delete(endpoint, options = {}) ⇒ Object
Handles DELETE requests, returns API response as ruby hash.
-
.get(endpoint, options = {}) ⇒ Object
Handles GET requests, returns API response as ruby hash.
-
.post(endpoint, options = {}) ⇒ Object
Handles POST requests, returns API response as ruby hash.
-
.put(endpoint, options = {}) ⇒ Object
Handles PUT requests, returns API response as ruby hash.
-
.set_key(key) ⇒ Object
Sets API key.
Class Method Details
.check_key ⇒ Object
Checks API key to /^sk_.22$/ test.
Returns true
or false
.
102 103 104 |
# File 'lib/graphcommons.rb', line 102 def self.check_key @@apikey and @@apikey.length > 3 end |
.delete(endpoint, options = {}) ⇒ Object
Handles DELETE requests, returns API response as ruby hash.
63 64 65 66 67 |
# File 'lib/graphcommons.rb', line 63 def self.delete endpoint, = {} uri = self._gd(endpoint, ) puts "DELETE #{uri}" if Graphcommons.verbose RestClient.delete(uri,:authentication=>@@apikey,:content_type=>"application/json") {|data| self._respond data, uri, } end |
.get(endpoint, options = {}) ⇒ Object
Handles GET requests, returns API response as ruby hash.
56 57 58 59 60 |
# File 'lib/graphcommons.rb', line 56 def self.get endpoint, = {} uri = self._gd(endpoint, ) puts "GET #{uri}" if Graphcommons.verbose RestClient.get(uri,:authentication=>@@apikey,:content_type=>"application/json") {|data| self._respond data, uri, } end |
.post(endpoint, options = {}) ⇒ Object
Handles POST requests, returns API response as ruby hash.
70 71 72 73 74 |
# File 'lib/graphcommons.rb', line 70 def self.post endpoint, = {} uri, query = self._pp endpoint, puts "POST #{uri} #{query}" if Graphcommons.verbose RestClient.post(uri,query.to_json,:authentication=>@@apikey,:content_type=>"application/json") {|data| self._respond data, uri, query} end |
.put(endpoint, options = {}) ⇒ Object
Handles PUT requests, returns API response as ruby hash.
77 78 79 80 81 |
# File 'lib/graphcommons.rb', line 77 def self.put endpoint, = {} uri, query = self._pp endpoint, puts "PUT #{uri} #{query}" if Graphcommons.verbose RestClient.put(uri,query.to_json,:authentication=>@@apikey,:content_type=>"application/json") {|data| self._respond data, uri, query} end |
.set_key(key) ⇒ Object
Sets API key.
Returns true
if key is changed, false
if not changed, raises APIError if key argument fails /^sk_.22$/ test.
To get the key, please visit graphcommons.com/me/edit
89 90 91 92 93 94 95 96 97 |
# File 'lib/graphcommons.rb', line 89 def self.set_key key raise Graphcommons::APIError.new("Invalid API key\nKey should be in following format: sk_XXXXXXXXXXXXXXXXXXXXXX") unless key.match(/^sk_.{22}$/) if @@apikey == key return false else @@apikey = key true end end |