class FormhubRuby::ApiConnector
Attributes
cast_integers[RW]
data[R]
fields[RW]
filetype[R]
formname[R]
limit[RW]
password[R]
query[RW]
sort[RW]
start[RW]
username[R]
Public Class Methods
new(args)
click to toggle source
# File lib/formhub_ruby/api_connector.rb, line 12 def initialize(args) @username = args[:username] || FormhubRuby.configuration.username @password = args[:password] || FormhubRuby.configuration.password @filetype = args[:filetype] || 'json' @formname = args[:formname] @query = args[:query] @start = args[:start] @limit = args[:limit] @sort = args[:sort] @fields = args[:fields] @cast_integers = args[:cast_integers] || false @data = [] end
Public Instance Methods
api_parameters()
click to toggle source
# File lib/formhub_ruby/api_connector.rb, line 69 def api_parameters if api_parameters_array.any? "?#{api_parameters_joined}" end end
api_parameters_array()
click to toggle source
# File lib/formhub_ruby/api_connector.rb, line 81 def api_parameters_array [api_query, start, limit, sort_query, fields_query] end
api_parameters_joined()
click to toggle source
# File lib/formhub_ruby/api_connector.rb, line 85 def api_parameters_joined api_parameters_array.compact.join('&') end
api_query()
click to toggle source
# File lib/formhub_ruby/api_connector.rb, line 75 def api_query if @query "query=#{CGI.escape stringify_hash_values(@query).to_json}" end end
api_uri()
click to toggle source
# File lib/formhub_ruby/api_connector.rb, line 65 def api_uri "http://formhub.org/#{@username}/forms/#{@formname}/api" + api_parameters.to_s end
cast_to_value_to_int(str)
click to toggle source
# File lib/formhub_ruby/api_connector.rb, line 128 def cast_to_value_to_int(str) begin Integer(str) rescue ArgumentError, TypeError str end end
fetch()
click to toggle source
# File lib/formhub_ruby/api_connector.rb, line 26 def fetch get_response(api_uri) end
fields_query()
click to toggle source
# File lib/formhub_ruby/api_connector.rb, line 115 def fields_query if @fields "fields=#{CGI.escape @fields.to_json}" end end
get_count()
click to toggle source
# File lib/formhub_ruby/api_connector.rb, line 30 def get_count response = get_response("#{api_uri}&count=1") response[0]['count'] end
get_response(custom_uri)
click to toggle source
private
# File lib/formhub_ruby/api_connector.rb, line 40 def get_response(custom_uri) uri = URI(custom_uri) req = Net::HTTP::Get.new(uri) req.basic_auth @username, @password begin response = Net::HTTP.start(uri.hostname, uri.port) do |http| http.request(req) end returned_data = JSON.parse(response.body) @data = if @cast_integers returned_data.map do |row| Hash[ row.map { |a, b| [a, cast_to_value_to_int(b)] } ] end else returned_data end rescue raise 'API connection error' end end
sort_query()
click to toggle source
Note that integers seem to be stored as strings in Formhub database, and will be sorted as such
# File lib/formhub_ruby/api_connector.rb, line 108 def sort_query if @sort validates_sort "sort=#{CGI.escape @sort.to_json}" end end
stringify_hash_values(hash)
click to toggle source
# File lib/formhub_ruby/api_connector.rb, line 101 def stringify_hash_values(hash) hash.merge(hash){|k,hashv|hashv.to_s} end
validates_sort()
click to toggle source
# File lib/formhub_ruby/api_connector.rb, line 121 def validates_sort unless @sort.values.all? { |value| value == 1 || value == -1} raise 'The sort option is hash taking +1 (ascending) or -1 (descending) value ' end end