Class: ImpactRadiusAPI::APIResource
- Inherits:
-
Object
- Object
- ImpactRadiusAPI::APIResource
- Includes:
- HTTParty
- Defined in:
- lib/impact_radius_api/api_resource.rb
Direct Known Subclasses
Instance Method Summary collapse
- #base_path ⇒ Object
- #class_name ⇒ Object
- #get(api_resource, params = {}) ⇒ Object
- #request(resource_url, params = {}) ⇒ Object
Instance Method Details
#base_path ⇒ Object
9 10 11 12 13 14 |
# File 'lib/impact_radius_api/api_resource.rb', line 9 def base_path if self.class_name == "APIResource" raise NotImplementedError.new("APIResource is an abstract class. You should perform actions on its subclasses (i.e. Publisher)") end "/#{CGI.escape(class_name)}/" end |
#class_name ⇒ Object
5 6 7 |
# File 'lib/impact_radius_api/api_resource.rb', line 5 def class_name self.class.name.split('::')[-1] end |
#get(api_resource, params = {}) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/impact_radius_api/api_resource.rb', line 16 def get(api_resource, params = {}) @resource ||= self.xml_field(api_resource) unless auth_token ||= ImpactRadiusAPI.auth_token raise AuthenticationError.new( "No authentication token (AuthToken) provided. Set your API key using 'ImpactRadiusAPI.auth_token = <API-KEY>'. " + "You can retrieve your authentication token (AuthToken) from the Impact Radius web interface. " + "See http://dev.impactradius.com/display/api/Getting+Started for details." ) end if auth_token =~ /\s/ raise AuthenticationError.new( "Your authentication token (AuthToken) looks invalid. " + "Double-check your authentication token (AuthToken) at http://dev.impactradius.com/display/api/Getting+Started" ) end unless account_sid ||= ImpactRadiusAPI.account_sid raise AuthenticationError.new( "No account_sid (AccountSid) provided. Set your account_sid (AccountSid) using 'ImpactRadiusAPI.account_sid = <AccountSid>'. " + "You can retrieve your account_sid (AccountSid) from the Impact Radius web interface. " + "See http://dev.impactradius.com/display/api/Getting+Started for details." ) end if account_sid =~ /\s/ || account_sid.length != 34 || account_sid[0..1] != "IR" raise AuthenticationError.new( "Your account_sid (AccountSid) looks invalid. " + "Double-check your account_sid (AccountSid) at http://dev.impactradius.com/display/api/Getting+Started" ) end raise ArgumentError, "Params must be a Hash; got #{params.class} instead" unless params.is_a? Hash #resource_url = ImpactRadiusAPI.api_base_url + base_path + api_resource resource_url = "https://" + account_sid + ":" + auth_token +"@" + pre_uri + ImpactRadiusAPI.api_base_uri + base_path + account_sid + "/" + api_resource request(resource_url, params) end |
#request(resource_url, params = {}) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/impact_radius_api/api_resource.rb', line 54 def request(resource_url, params = {}) timeout = ImpactRadiusAPI.api_timeout @resource ||= self.xml_field(resource_url.match(/\/[a-z]+(\?|\z)/i)[0].gsub("/","").gsub("?","")) begin response = self.class.get(resource_url, query: params, timeout: timeout) rescue Timeout::Error raise ConnectionError.new("Timeout error (#{timeout}s)") end process(response) end |