Class: Resource

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(session, options) ⇒ Resource

Returns a new instance of Resource.



6
7
8
9
10
# File 'lib/resource.rb', line 6

def initialize(session,options)
  @options = options
  @session = session
  @last_resource = session.resource
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



4
5
6
# File 'lib/resource.rb', line 4

def options
  @options
end

#rawObject

Returns the value of attribute raw.



4
5
6
# File 'lib/resource.rb', line 4

def raw
  @raw
end

Instance Method Details

#create_resource_onceObject

Purpose: Create service requests URL: [API endpoint]/requests. Sample URL: api.city.gov/dev/v2/requests.xml Format sent: Content-Type: application/x-www-form-urlencoded Formats returned: XML (JSON available if denoted by Service Discovery) HTTP Method: POST Requires API Key: Yes

Required Arguments jurisdiction_id service_code (obtained from GET Service List method) location: either lat & long or address_string or address_id must be submitted attribute - only required if the service_code requires a service definition with required fields Explanation: An array of key/value responses based on Service Definitions. This takes the form of attribute=value where multiple code/value pairs can be specified as well as multiple values for the same code in the case of a multivaluelist datatype (attribute[]=value1&attribute[]=value2&attribute[]=value3) - see example



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/resource.rb', line 60

def create_resource_once
  @raw.xml  = []
  @raw.json = []
  # For each endpoint...
  @session.discovery.xml.response.discovery.endpoints.endpoint.each do |endpoint|
    next if type(endpoint) == 'production' and !@options.production
    @raw.xml << Client.new.post("#{endpoint.url}/requests.xml",data_from_options)
  end
  
  @session.discovery.json.response.endpoints.each do |endpoint|
    next if type(endpoint) == 'production' and !@options.production
    @raw.json << Client.new.post("#{endpoint.url}/requests.json",data_from_options)
  end if @options.json
  
  @raw.options = @options
  @raw
end

#data_from_optionsObject



78
79
80
81
82
83
84
85
86
# File 'lib/resource.rb', line 78

def data_from_options
  ## Converting @options back to a hash and merge them with user-supplied
  #  post data.
  { :api_key => @options.api_key,
    :jurisdiction_id => @options.jurisdiction_id,
    :service_code => first_service_code,
    :address_string => @options.address
  }.merge!(@options.post)
end

#definition_resourceObject



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/resource.rb', line 31

def definition_resource
  @raw = []
  @session.raw_services.each do |raw_service|
    raw_service.response.each do |service|
      next if !service. or service. == 'false'
      response = Client.new(raw_service.base,raw_service.format)
      response.get_and_unwrap("/services/#{service.service_code}.#{raw_service.format}?jurisdiction_id=#{@options[:jurisdiction_id]}",'')
      @raw << response
    end
  end
end

#discovery_urlObject



16
17
18
19
20
# File 'lib/resource.rb', line 16

def discovery_url
  @raw = OpenStruct.new
  @raw.json = Client.new(@options[:discovery_url],'json').get(".json")
  @raw.xml  = Client.new(@options[:discovery_url],'xml').get(".xml")
end

#first_service_codeObject



88
89
90
# File 'lib/resource.rb', line 88

def first_service_code
  @session.resources[1].xml.first.response.services.service.service_code
end

#get_nextObject



12
13
14
# File 'lib/resource.rb', line 12

def get_next
  self.send @options[:with]
end

#services_resourceObject



22
23
24
25
26
27
28
29
# File 'lib/resource.rb', line 22

def services_resource
  @raw = []
  @session.endpoint_array.each do |endpoint|
    response = Client.new(endpoint[0],endpoint[1])
    response.get_and_unwrap("/services.#{endpoint[1]}?jurisdiction_id=#{@options[:jurisdiction_id]}",'services.service')
    @raw << response
  end
end