Class: OdkInstance

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

Overview

require ‘nokogiri’

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ OdkInstance

Returns a new instance of OdkInstance.



10
11
12
# File 'lib/opendatakit.rb', line 10

def initialize(url)
  @url = URI.parse(url)
end

Instance Attribute Details

#urlObject

Returns the value of attribute url.



8
9
10
# File 'lib/opendatakit.rb', line 8

def url
  @url
end

Instance Method Details

#addSubmissions(submission_data, media_files) ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/opendatakit.rb', line 62

def addSubmissions(submission_data,media_files)
  submission_post_url = URI.join(@url,"submission")
  params = { :xml_submission_file => File.open(submission_data)}

  http = HTTPClient.new
  httpresults = http.post(submission_post_url,params)
  httpresults.content
end

#getSubmissions(formname) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/opendatakit.rb', line 18

def getSubmissions(formname)
  submission_url = URI.join(@url,"view/submissionList")
  params = { :formId => formname }
  submission_url.query = URI.encode_www_form(params)

  http = HTTPClient.new
  httpresults = http.get submission_url

  if httpresults.status == 200
    submission_xml = httpresults.content
    doc = REXML::Document.new(submission_xml)
    all_id_elements = doc.elements.to_a( "//id" ).map { |el| el.text } 
  else
    "Form name not found"
  end
end

#uploadForm(form_xml) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/opendatakit.rb', line 35

def uploadForm(form_xml)
  form_post_url = URI.join(@url,"formUpload")
  params = { :form_def_file => File.open(form_xml)}
  doc = REXML::Document.new(File.open(form_xml))
  title = REXML::XPath.first( doc, "//h:title" )

  http = HTTPClient.new
  httpresults = http.post(form_post_url,params)

  if httpresults.status_code == 201
    title.text
  else
    "Form could not be uploaded"
  end
end

#uploadXmlform(doc) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/opendatakit.rb', line 50

def uploadXmlform(doc)
  form_post_url = URI.join(@url,"formUpload")
  params = { :form_def_file => StringIO.new(doc) }
  http = HTTPClient.new
  httpresults = http.post(form_post_url,params)
  if httpresults.status_code == 201
    httpresults.status_code
  else
    "Form could not be uploaded"
  end
end