Class: Oodle::API

Inherits:
Object
  • Object
show all
Defined in:
lib/oodle.rb

Overview

Oodle Class

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, version = :v2) ⇒ API

Returns a new instance of API.



42
43
44
45
46
47
48
49
# File 'lib/oodle.rb', line 42

def initialize(key,version=:v2)
  @key = key
  @version = version
  @attributes = []
  @refinements = []
  @exclude_sources = []
  @format = RESPONSE_FORMATS[:json]
end

Instance Attribute Details

#assisted_searchObject

Returns the value of attribute assisted_search.



40
41
42
# File 'lib/oodle.rb', line 40

def assisted_search
  @assisted_search
end

#attributesObject

Returns the value of attribute attributes.



40
41
42
# File 'lib/oodle.rb', line 40

def attributes
  @attributes
end

#categoryObject

Returns the value of attribute category.



40
41
42
# File 'lib/oodle.rb', line 40

def category
  @category
end

#ctime_highObject

Returns the value of attribute ctime_high.



40
41
42
# File 'lib/oodle.rb', line 40

def ctime_high
  @ctime_high
end

#ctime_lowObject

Returns the value of attribute ctime_low.



40
41
42
# File 'lib/oodle.rb', line 40

def ctime_low
  @ctime_low
end

#exclude_sourcesObject

Returns the value of attribute exclude_sources.



40
41
42
# File 'lib/oodle.rb', line 40

def exclude_sources
  @exclude_sources
end

#fetchedObject

Returns the value of attribute fetched.



40
41
42
# File 'lib/oodle.rb', line 40

def fetched
  @fetched
end

#formatObject

Returns the value of attribute format.



40
41
42
# File 'lib/oodle.rb', line 40

def format
  @format
end

#jsoncallbackObject

Returns the value of attribute jsoncallback.



40
41
42
# File 'lib/oodle.rb', line 40

def jsoncallback
  @jsoncallback
end

#keyObject

Returns the value of attribute key.



40
41
42
# File 'lib/oodle.rb', line 40

def key
  @key
end

#locationObject

Returns the value of attribute location.



40
41
42
# File 'lib/oodle.rb', line 40

def location
  @location
end

#numObject

Returns the value of attribute num.



40
41
42
# File 'lib/oodle.rb', line 40

def num
  @num
end

#qObject

Returns the value of attribute q.



40
41
42
# File 'lib/oodle.rb', line 40

def q
  @q
end

#radiusObject

Returns the value of attribute radius.



40
41
42
# File 'lib/oodle.rb', line 40

def radius
  @radius
end

#refinementsObject

Returns the value of attribute refinements.



40
41
42
# File 'lib/oodle.rb', line 40

def refinements
  @refinements
end

#regionObject

Returns the value of attribute region.



40
41
42
# File 'lib/oodle.rb', line 40

def region
  @region
end

#sortObject

Returns the value of attribute sort.



40
41
42
# File 'lib/oodle.rb', line 40

def sort
  @sort
end

#startObject

Returns the value of attribute start.



40
41
42
# File 'lib/oodle.rb', line 40

def start
  @start
end

#versionObject

Returns the value of attribute version.



40
41
42
# File 'lib/oodle.rb', line 40

def version
  @version
end

Instance Method Details

#attributes_as_stringObject



112
113
114
115
116
# File 'lib/oodle.rb', line 112

def attributes_as_string
  res = ""
  res = self.attributes.join(',') if self.attributes.size > 0
  res
end

#build_urlObject

Requires version be set Build the url from the state of the current self



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/oodle.rb', line 60

def build_url
  unless key && key.length > 0
    raise ArgumentError, 'Missing API key parameter. Visit http://developer.oodle.com/request-api-key/ to get one.'
  end

  unless region && region.length > 0
      raise ArgumentError, 'Missing region paramter. Visit http://developer.oodle.com/regions-list/ for possible regions.'
  end

  unless (category && category.length > 0) || (q && q.length > 0)
      raise ArgumentError, 'You must supply a category or query parameter. Visit http://developer.oodle.com/categories-list/ for possible categories.'
  end

  unless num.to_i >= 1 && num.to_i <= 50
    warn "num parameter is #{num.to_i} but should be between 1 and 50"
  end

  url = VERSION_URLS[self.version]
  url += "?" unless url && url[-1] == '?'
  # must CGI escape each param value
  url = "#{url}key=#{CGI::escape(self.key)}"
  url = "#{url}&region=#{CGI::escape(self.region)}" if self.region
  url = "#{url}&category=#{CGI::escape(self.category)}" if self.category
  url = "#{url}&q=#{CGI::escape(self.q)}" if self.q
  url = "#{url}&attributes=#{CGI::escape(self.attributes_as_string)}" if self.attributes.size > 0
  url = "#{url}&location=#{CGI::escape(self.location)}" if self.location
  url = "#{url}&radius=#{CGI::escape(self.radius.to_s)}" if self.radius
  url = "#{url}&start=#{CGI::escape(self.start.to_s)}" if self.start
  url = "#{url}&num=#{CGI::escape(self.num.to_s)}" if self.num
  url = "#{url}&sort=#{CGI::escape(self.sort)}" if self.sort
  url = "#{url}&refinements=#{CGI::escape(self.refinements_as_string)}" if self.refinements.size > 0
  url = "#{url}&ctime_low=#{CGI::escape(self.ctime_low)}" if self.ctime_low
  url = "#{url}&ctime_high=#{CGI::escape(self.ctime_high)}" if self.ctime_high
  url = "#{url}&exclude_sources=#{CGI::escape(self.exclude_sources_as_string)}" if self.exclude_sources.size > 0
  url = "#{url}&assisted_search=#{CGI::escape(self.assisted_search)}" if self.assisted_search
  url = "#{url}&format=#{CGI::escape(self.format)}&jsoncallback=none" if self.format
  url
end

#exclude_sources_as_stringObject

THESE CAN BE REFACTORED INTO SOMETHING MORE META-LIKE



100
101
102
103
104
# File 'lib/oodle.rb', line 100

def exclude_sources_as_string
  res = ""
  res = self.exclude_sources.join(',') if self.exclude_sources.size > 0
  res
end

#fetch_categories(raw = false) ⇒ Object



144
145
146
147
148
149
150
151
152
# File 'lib/oodle.rb', line 144

def fetch_categories(raw=false)
  xml = http_pull(CATEGORIES_URL)
  if raw
    xml
  else
    # parse into objects
    XmlSimple.xml_in xml, { 'ForceArray' => false, 'AttrPrefix' => true }
  end
end

#fetch_listings(format = nil) ⇒ Object

Returns an array of listings based on the last fetch if fetch is empty then will trigger a fetch



130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/oodle.rb', line 130

def fetch_listings(format=nil)
  result = nil
  raw = fetch_raw(format)
  case self.format
  when 'xml'
    # parse xml raw
    result = XmlSimple.xml_in raw, { 'ForceArray' => false, 'AttrPrefix' => true }
  when 'json'
    result = JSON.parse(raw)
  end
  result = raw unless result
  result
end

#fetch_raw(format = nil) ⇒ Object

xml - output is formatted in plain old XML

json - output is fromatted in JSON (JavaScript Object Notation)


123
124
125
126
# File 'lib/oodle.rb', line 123

def fetch_raw(format=nil)
  @format = RESPONSE_FORMATS[format] if format
  http_pull(build_url)
end

#fetch_regions(raw = false) ⇒ Object



154
155
156
157
158
159
160
161
162
# File 'lib/oodle.rb', line 154

def fetch_regions(raw=false)
  xml = http_pull(REGIONS_URL)
  if raw
    xml
  else
    # parse into objects
    XmlSimple.xml_in xml, { 'ForceArray' => false, 'AttrPrefix' => true }
  end
end

#http_pull(dest_url) ⇒ Object

A convience method to do actual http pulls Notice that there is not any exception trapping



53
54
55
56
# File 'lib/oodle.rb', line 53

def http_pull(dest_url)
  res = Net::HTTP.get_response(URI.parse(dest_url))
  self.fetched = res.body
end

#refinements_as_stringObject



106
107
108
109
110
# File 'lib/oodle.rb', line 106

def refinements_as_string
  res = ""
  res = self.refinements.join(',') if self.refinements.size > 0
  res
end