Class: Request

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

Constant Summary collapse

METHOD =

defaults

'search'
OBJECTTYPE =
'Image'
LIMIT =
10
DEPTH =
1
FORMAT =
'id'
URI_PARAMS =

a subset of VALID_OPTIONS, these are concatenated in the request

[:limit, :format, :depth, :objecttype, :firstResult, :keywords, :id, :taxonName, :user, :group, :change, :lastDateChanged, :numChangeDays]
VALID_OPTIONS =

possible options for a Request instance

{'v3' => URI_PARAMS + [:version, :method] }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Request



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
# File 'lib/request.rb', line 18

def initialize(options = {}) 
  opt = {
    :version => 'v3',
    :method => METHOD,
    :limit => LIMIT,
    :format => FORMAT,                # 'id' (brief results) or 'svc' (schema based results)
    :depth => DEPTH,
    :objecttype => OBJECTTYPE,        #  [nil, Taxon, Image, Character, Specimen, View, Matrix, Locality, Collection, OTU ] (nil = all)
    :firstResult => 0,
    :keywords => ''
  }.merge!(options)

  # Check for legal parameters
  opt.keys.each do |p|
    raise RubyMorphbankError, "#{p} is not a valid parameter" if !VALID_OPTIONS[opt[:version]].include?(p)
  end 

  # TODO: add some proper sanitation?!
  opt[:keywords].gsub!(/\s+/, '+') if opt[:keywords].length > 0

  # create the request 
  @request_url = SERVICES_URI + "method=#{opt[:method]}" +
  opt.keys.sort{|a,b| a.to_s <=> b.to_s}.collect{
    |k| ( (URI_PARAMS.include?(k) && ( !opt[k].nil? || (opt[k] && opt[k].empty?)) ) ? "&#{k.to_s}=#{opt[k]}" : '')
  }.join

  # and some housekeepers
  @request_options = opt 
end

Instance Attribute Details

#request_optionsObject (readonly)

Returns the value of attribute request_options.



16
17
18
# File 'lib/request.rb', line 16

def request_options
  @request_options
end

#request_urlObject (readonly)

Returns the value of attribute request_url.



16
17
18
# File 'lib/request.rb', line 16

def request_url
  @request_url
end

Instance Method Details

#get_responseObject



48
49
50
# File 'lib/request.rb', line 48

def get_response
  Response.new(self)
end