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
- #delete(api_resource, params = {}, body = {}) ⇒ 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 |
#delete(api_resource, params = {}, body = {}) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/trustedsearch/api_resource.rb', line 101 def delete(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('delete', resource_url, params, body) end |
#end_point ⇒ Object
get the end_point based upon the environment. Default to sandbox.
131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/trustedsearch/api_resource.rb', line 131 def end_point if(ENV['RAKE_ENV']) TrustedSearch.environment = ENV['RAKE_ENV'] end if(TrustedSearch.environment == 'production') return TrustedSearch.environments[:production][:domain] elsif(TrustedSearch.environment == 'local') return TrustedSearch.environments[:local][: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 53 54 |
# 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
56 57 58 |
# File 'lib/trustedsearch/api_resource.rb', line 56 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
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/trustedsearch/api_resource.rb', line 60 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
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/trustedsearch/api_resource.rb', line 169 def process(response) # puts response.to_s # body = JSON.parse(response.body) # puts body.to_json # puts response.message case response.code when 200, 201, 204 APIResponse.new(response) when 400, 404, 409 raise InvalidRequestError.new(response, response.code) when 401 raise AuthenticationError.new(response, response.code) else error = Error.new(response., response.code) error.body = response.body raise error end end |
#put(api_resource, params = {}, body = {}) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/trustedsearch/api_resource.rb', line 80 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
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/trustedsearch/api_resource.rb', line 144 def request(method='get', resource_url, params, body) #puts resource_url #puts params.to_json timeout = TrustedSearch.api_timeout begin puts resource_url 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 } ) when 'delete' response = self.class.delete(resource_url, {:query => params, :body => body.to_json, :timeout => timeout } ) end rescue Timeout::Error connection_response = ApiMockResponse.new("Timeout error (#{timeout}s)", nil, 500) raise ConnectionError.new(connection_response) end process(response) end |
#sign_request(private_key, url, body, timestamp) ⇒ Object
122 123 124 125 126 127 128 |
# File 'lib/trustedsearch/api_resource.rb', line 122 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 |