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

Class Method Details

.api_keyObject



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

.configObject



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, options)
  env ||= options.delete(:env)
  env ||=  Sensis.env
  env ||= "test"
  endpoint = "http://api.sensis.com.au/ob-20110511/#{env}/#{endpoint_type}"
  endpoint = "#{endpoint}/#{options[:eventName]}" if endpoint_type == "report"
  return endpoint
end

.envObject



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, options)
  # location of the search endpoint
  endpoint = Sensis.endpoint(endpoint_type, options)

  # construct a URL with the query string, escaping any special characters.
  url = "#{endpoint}?"
  options.keys.each do |k|
    if options[k].is_a?(Array)
      options[k].each do |v|
        url = "#{url}&#{k}=#{URI.encode(v)}"
      end
    else
      url = "#{url}&#{k}=#{URI.encode(options[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.message}, 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(options = {})
  options[:key] ||= Sensis.api_key
  errors = []
  errors << ":key (api key) is required" if options[:key].blank?
  errors << ":query is required" if options[:query].blank? 
  raise errors.join("; ") unless errors.empty?
  Sensis.execute("getByListingId", options)
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(options = {})
  options[:key] ||= Sensis.api_key
  errors = []
  errors << ":key (api key) is required" if options[:key].blank?
  errors << ":userIp is required" if options[:userIp].blank?
  errors << ":id is required" if options[:id].blank?
  raise errors.join("; ") unless errors.empty?
  Sensis.execute("report", options)
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(options = {})
  options[:key] ||= Sensis.api_key
  errors = []
  errors << ":key (api key) is required" if options[:key].blank?
  errors << ":query or :location is required" if options[:query].blank? && options[:location].blank?
  raise errors.join("; ") unless errors.empty?
  Sensis.execute("search", options)
end