Class: TrustedSearch::APIResource
- Inherits:
-
Object
- Object
- TrustedSearch::APIResource
- Includes:
- HTTParty
- Defined in:
- lib/trustedsearch/api_resource.rb
Direct Known Subclasses
Instance Method Summary collapse
- #base_path ⇒ Object
- #class_name ⇒ Object
-
#end_point ⇒ Object
get the end_point based upon the environment.
- #get(api_resource, params = {}, body = '') ⇒ Object
- #get_time ⇒ Object
- #has_keys ⇒ Object
- #post(api_resource, params = {}, body = {}) ⇒ Object
- #process(response) ⇒ Object
- #put(api_resource, params = {}, body = {}) ⇒ Object
- #request(method = 'get', resource_url, params, body) ⇒ Object
- #sign_request(private_key, url, body, timestamp) ⇒ Object
Instance Method Details
#base_path ⇒ Object
9 10 11 12 13 14 |
# File 'lib/trustedsearch/api_resource.rb', line 9 def base_path if self == APIResource raise NotImplementedError.new("APIResource is an abstract class. You should perform actions on its subclasses (i.e. Publisher)") end "" end |
#class_name ⇒ Object
5 6 7 |
# File 'lib/trustedsearch/api_resource.rb', line 5 def class_name self.class.name.split('::')[-1] end |
#end_point ⇒ Object
get the end_point based upon the environment. Default to sandbox.
108 109 110 111 112 113 114 |
# File 'lib/trustedsearch/api_resource.rb', line 108 def end_point if(TrustedSearch.environment == 'production') return TrustedSearch.environments[:production][:domain] else return TrustedSearch.environments[:sandbox][:domain] end end |
#get(api_resource, params = {}, body = '') ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/trustedsearch/api_resource.rb', line 34 def get(api_resource, params = {}, body = '') @resource ||= api_resource has_keys() raise ArgumentError, "Params must be a Hash; got #{params.class} instead" unless params.is_a? Hash = get_time() url_to_sign = base_path + api_resource params.merge!({ apikey: TrustedSearch.public_key, signature: sign_request(TrustedSearch.private_key, url_to_sign, body, ), timestamp: }) resource_url = end_point + url_to_sign request('get', resource_url, params, body) end |
#get_time ⇒ Object
54 55 56 |
# File 'lib/trustedsearch/api_resource.rb', line 54 def get_time return Time.now.utc.to_i end |
#has_keys ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/trustedsearch/api_resource.rb', line 16 def has_keys unless public_key ||= TrustedSearch.public_key raise AuthenticationError.new( "No public_key key provided. Set your public_key using 'TrustedSearch.public_key = <API-KEY>'. " + "You can retrieve your public_key from a TRUSTEDSearch rep. " + "See http://developers.trustedsearch.org/#/getting-started for details." ) end unless private_key ||= TrustedSearch.private_key raise AuthenticationError.new( "No private_key provided. Set your private_key using 'TrustedSearch.private_key = <API-KEY>'. " + "You can retrieve your private_key from a TRUSTEDSearch rep. " + "See http://developers.trustedsearch.org/#/getting-started for details." ) end end |
#post(api_resource, params = {}, body = {}) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/trustedsearch/api_resource.rb', line 58 def post(api_resource, params = {}, body = {}) @resource ||= api_resource has_keys() raise ArgumentError, "Params must be a Hash; got #{params.class} instead" unless params.is_a? Hash = get_time() url_to_sign = base_path + api_resource params.merge!({ apikey: TrustedSearch.public_key, signature: sign_request(TrustedSearch.private_key, url_to_sign, body, ), timestamp: }) resource_url = end_point + url_to_sign request('post', resource_url, params, body) end |
#process(response) ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/trustedsearch/api_resource.rb', line 140 def process(response) #puts response.to_json case response.code when 200, 201, 204 APIResponse.new(response) when 400, 404 raise InvalidRequestError.new(response., response.code) when 401 raise AuthenticationError.new(response., response.code) else raise Error.new(response., response.code) end end |
#put(api_resource, params = {}, body = {}) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/trustedsearch/api_resource.rb', line 78 def put(api_resource, params = {}, body = {}) @resource ||= api_resource has_keys() raise ArgumentError, "Params must be a Hash; got #{params.class} instead" unless params.is_a? Hash = get_time() url_to_sign = base_path + api_resource params.merge!({ apikey: TrustedSearch.public_key, signature: sign_request(TrustedSearch.private_key, url_to_sign, body, ), timestamp: }) resource_url = end_point + url_to_sign request('put', resource_url, params, body) end |
#request(method = 'get', resource_url, params, body) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/trustedsearch/api_resource.rb', line 117 def request(method='get', resource_url, params, body) #puts resource_url #puts params.to_json timeout = TrustedSearch.api_timeout begin case method when 'get' response = self.class.get(resource_url, query: params, timeout: timeout) when 'post' response = self.class.post(resource_url, {:query => params, :body => body.to_json, :timeout => timeout } ) when 'put' response = self.class.put(resource_url, {:query => params, :body => body.to_json, :timeout => timeout } ) end rescue Timeout::Error raise ConnectionError.new("Timeout error (#{timeout}s)") end process(response) end |
#sign_request(private_key, url, body, timestamp) ⇒ Object
99 100 101 102 103 104 105 |
# File 'lib/trustedsearch/api_resource.rb', line 99 def sign_request( private_key, url, body, ) body_md5 = (body == "") ? "" : Base64.strict_encode64( Digest::MD5.digest(body.to_json) ) signature = url + body_md5 + .to_s signature = Base64.strict_encode64( Digest::HMAC.digest(signature, private_key , Digest::SHA1) ) return signature end |