Module: Sensis
- Defined in:
- lib/sensis.rb,
lib/sensis/version.rb
Defined Under Namespace
Classes: ResponseData
Constant Summary collapse
- VERSION =
"0.0.3"
Class Method Summary collapse
- .api_key ⇒ Object
- .config ⇒ Object
- .endpoint(endpoint_type, options) ⇒ Object
- .env ⇒ Object
- .execute(endpoint_type, options) ⇒ Object
-
.get_listing_by_id(options = {}) ⇒ Object
Get Listing By ID - developers.sensis.com.au/docs/endpoint_reference/Get_by_Listing_ID options key string API key (required) See Authenticating for details.
-
.report(options = {}) ⇒ Object
Report - developers.sensis.com.au/docs/endpoint_reference/Report key string API key (required) See Authenticating for details.
-
.search(options = {}) ⇒ Object
Search - developers.sensis.com.au/docs/endpoint_reference/Search options (from developers.sensis.com.au/docs/endpoint_reference/Search) key string API key (required) See Authenticating for details.
Class Method Details
.api_key ⇒ Object
19 20 21 |
# File 'lib/sensis.rb', line 19 def Sensis.api_key @api_key ||= Sensis.config[::Rails.env]["api_key"] unless Sensis.config.nil? end |
.config ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/sensis.rb', line 7 def Sensis.config if @config.nil? config_file = "#{::Rails.root}/config/sensis.yml" if File.exists?(config_file) @config = YAML.load_file(config_file) @api_key = @config[::Rails.env]["api_key"] @env = @config[::Rails.env]["env"] end end return @config end |
.endpoint(endpoint_type, options) ⇒ Object
126 127 128 129 130 131 132 133 |
# File 'lib/sensis.rb', line 126 def Sensis.endpoint(endpoint_type, ) env ||= .delete(:env) env ||= Sensis.env env ||= "test" endpoint = "http://api.sensis.com.au/ob-20110511/#{env}/#{endpoint_type}" endpoint = "#{endpoint}/#{[:eventName]}" if endpoint_type == "report" return endpoint end |
.env ⇒ Object
23 24 25 |
# File 'lib/sensis.rb', line 23 def Sensis.env @env ||= Sensis.config[::Rails.env]["env"] unless Sensis.config.nil? end |
.execute(endpoint_type, options) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/sensis.rb', line 87 def Sensis.execute(endpoint_type, ) # location of the search endpoint endpoint = Sensis.endpoint(endpoint_type, ) # construct a URL with the query string, escaping any special characters. url = "#{endpoint}?" .keys.each do |k| if [k].is_a?(Array) [k].each do |v| url = "#{url}&#{k}=#{URI.encode(v)}" end else url = "#{url}&#{k}=#{URI.encode([k].to_s)}" end end # call the endpoint, returning the HTTP response response = Net::HTTP.get_response(URI.parse(url)) # raise an exception if not HTTP 200 (OK) response.error! unless response.instance_of? Net::HTTPOK # convert the response message in to a Hash object result = JSON.parse(response.body) res = ResponseData.new(result) if result # ensure successful status code case result["code"] when 200 # success return res when 206 # spell-checker was run puts "Note: #{result["message"]}" return res else raise "API returned error: #{res.}, code: #{result.code}" end end |
.get_listing_by_id(options = {}) ⇒ Object
Get Listing By ID - developers.sensis.com.au/docs/endpoint_reference/Get_by_Listing_ID options key string API key (required) See Authenticating for details. query string Unique ID of the listing to return (required) This is the id field of the listing. See Listing Schema for details. mode string Default is “test” - values “test”,“prod” - decides which endpoint is used - test or production
60 61 62 63 64 65 66 67 |
# File 'lib/sensis.rb', line 60 def Sensis.get_listing_by_id( = {}) [:key] ||= Sensis.api_key errors = [] errors << ":key (api key) is required" if [:key].blank? errors << ":query is required" if [:query].blank? raise errors.join("; ") unless errors.empty? Sensis.execute("getByListingId", ) end |
.report(options = {}) ⇒ Object
Report - developers.sensis.com.au/docs/endpoint_reference/Report key string API key (required) See Authenticating for details. userIp string IP address of user accessing your application (required) See Reporting Usage Events for details. id string or an array reportingId of listing associated with event (required) The reportingId field provided in each listing returned in a search response. Multiple id parameters can be added where the same event applies to each listing, except where the content parameter is specified. See Reporting Usage Events for details. userAgent string User agent of user accessing your application For example, from the user-agent HTTP header. See Reporting Usage Events for details. userSessionId string Session id of user accessing your application See Note below. content string Specific content to which the event applies (required) Only required for certain events. See Reporting Usage Events for details. mode string Default is “test” - values “test”,“prod” - decides which endpoint is used - test or production
77 78 79 80 81 82 83 84 85 |
# File 'lib/sensis.rb', line 77 def Sensis.report( = {}) [:key] ||= Sensis.api_key errors = [] errors << ":key (api key) is required" if [:key].blank? errors << ":userIp is required" if [:userIp].blank? errors << ":id is required" if [:id].blank? raise errors.join("; ") unless errors.empty? Sensis.execute("report", ) end |
.search(options = {}) ⇒ Object
Search - developers.sensis.com.au/docs/endpoint_reference/Search options (from developers.sensis.com.au/docs/endpoint_reference/Search) key string API key (required) See Authenticating for details. query string What to search for (required unless location is given) See Search Query Tips for details. location string Location to search in (required unless query is given) See Location Tips for details. page number Page number to return. See Pagination for details. rows number Number of listings to return per page. See Pagination for details. sortBy string Listing sort order. See Sorting for details. sensitiveCategories boolean Filtering potentially unsafe content. See Filtering Unsafe Content for details. categoryId string Filter listings returned by category id See Category Filtering for details. postcode string Filter listings returned by postcode See Postcode Filtering for details. radius number Filter listings returned to those within the radius distance of the location. See Radius Filtering for details. suburb string Filter listings returned to those within the given suburb. Repeat the parameter to include multiple suburbs in the filter. See Suburb Filtering for details. state string Filter listings returned to those within the given state. Repeat the parameter to include multiple states in the filter. See State Filtering for details. boundingBox string Filter listings returned to those within a bounding box. See Bounding Box Filtering for details. content string Filter listings returned to only those with certain types of content. See Filtering by Content Type for details. productKeyword string Filter listings returned to only those containing certain product keywords. See Filtering by Product Keyword for details. mode string Default is “test” - values “test”,“prod” - decides which endpoint is used - test or production Example - Sensis.serch(:key => “key you got from developers.sensis.com.au”, :query => “poker”)
46 47 48 49 50 51 52 53 |
# File 'lib/sensis.rb', line 46 def Sensis.search( = {}) [:key] ||= Sensis.api_key errors = [] errors << ":key (api key) is required" if [:key].blank? errors << ":query or :location is required" if [:query].blank? && [:location].blank? raise errors.join("; ") unless errors.empty? Sensis.execute("search", ) end |