Class: Graphcommons::API

Inherits:
Object
  • Object
show all
Defined in:
lib/graphcommons.rb

Overview

Low-level wrapper for API calls.

Direct Known Subclasses

Endpoint, Search, Signal

Constant Summary collapse

@@apiurl =
"https://graphcommons.com/api/v1"

Class Method Summary collapse

Class Method Details

.check_keyObject

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, options = {}
  uri = self._gd(endpoint, options)
  puts "DELETE #{uri}" if Graphcommons.verbose
  RestClient.delete(uri,:authentication=>@@apikey,:content_type=>"application/json") {|data| self._respond data, uri, options}
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, options = {}
  uri = self._gd(endpoint, options)
  puts "GET #{uri}" if Graphcommons.verbose
  RestClient.get(uri,:authentication=>@@apikey,:content_type=>"application/json") {|data| self._respond data, uri, options}
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, options = {}
  uri, query = self._pp endpoint, options
  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, options = {}
  uri, query = self._pp endpoint, options
  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