Class: FormhubRuby::ApiConnector
- Inherits:
-
Object
- Object
- FormhubRuby::ApiConnector
- Defined in:
- lib/formhub_ruby/api_connector.rb
Instance Attribute Summary collapse
-
#cast_integers ⇒ Object
Returns the value of attribute cast_integers.
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#fields ⇒ Object
Returns the value of attribute fields.
-
#filetype ⇒ Object
readonly
Returns the value of attribute filetype.
-
#formname ⇒ Object
readonly
Returns the value of attribute formname.
-
#limit ⇒ Object
Returns the value of attribute limit.
-
#password ⇒ Object
readonly
Returns the value of attribute password.
-
#query ⇒ Object
Returns the value of attribute query.
-
#sort ⇒ Object
Returns the value of attribute sort.
-
#start ⇒ Object
Returns the value of attribute start.
-
#username ⇒ Object
readonly
Returns the value of attribute username.
Instance Method Summary collapse
- #api_parameters ⇒ Object
- #api_parameters_array ⇒ Object
- #api_parameters_joined ⇒ Object
- #api_query ⇒ Object
- #api_uri ⇒ Object
- #cast_to_value_to_int(str) ⇒ Object
- #fetch ⇒ Object
- #fields_query ⇒ Object
- #get_count ⇒ Object
-
#get_response(custom_uri) ⇒ Object
private.
-
#initialize(args) ⇒ ApiConnector
constructor
A new instance of ApiConnector.
-
#sort_query ⇒ Object
Note that integers seem to be stored as strings in Formhub database, and will be sorted as such.
- #stringify_hash_values(hash) ⇒ Object
- #validates_sort ⇒ Object
Constructor Details
#initialize(args) ⇒ ApiConnector
Returns a new instance of ApiConnector.
12 13 14 15 16 17 18 19 20 21 22 23 24 |
# 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 |
Instance Attribute Details
#cast_integers ⇒ Object
Returns the value of attribute cast_integers.
9 10 11 |
# File 'lib/formhub_ruby/api_connector.rb', line 9 def cast_integers @cast_integers end |
#data ⇒ Object (readonly)
Returns the value of attribute data.
8 9 10 |
# File 'lib/formhub_ruby/api_connector.rb', line 8 def data @data end |
#fields ⇒ Object
Returns the value of attribute fields.
9 10 11 |
# File 'lib/formhub_ruby/api_connector.rb', line 9 def fields @fields end |
#filetype ⇒ Object (readonly)
Returns the value of attribute filetype.
8 9 10 |
# File 'lib/formhub_ruby/api_connector.rb', line 8 def filetype @filetype end |
#formname ⇒ Object (readonly)
Returns the value of attribute formname.
8 9 10 |
# File 'lib/formhub_ruby/api_connector.rb', line 8 def formname @formname end |
#limit ⇒ Object
Returns the value of attribute limit.
9 10 11 |
# File 'lib/formhub_ruby/api_connector.rb', line 9 def limit @limit end |
#password ⇒ Object (readonly)
Returns the value of attribute password.
8 9 10 |
# File 'lib/formhub_ruby/api_connector.rb', line 8 def password @password end |
#query ⇒ Object
Returns the value of attribute query.
9 10 11 |
# File 'lib/formhub_ruby/api_connector.rb', line 9 def query @query end |
#sort ⇒ Object
Returns the value of attribute sort.
9 10 11 |
# File 'lib/formhub_ruby/api_connector.rb', line 9 def sort @sort end |
#start ⇒ Object
Returns the value of attribute start.
9 10 11 |
# File 'lib/formhub_ruby/api_connector.rb', line 9 def start @start end |
#username ⇒ Object (readonly)
Returns the value of attribute username.
8 9 10 |
# File 'lib/formhub_ruby/api_connector.rb', line 8 def username @username end |
Instance Method Details
#api_parameters ⇒ Object
68 69 70 71 72 |
# File 'lib/formhub_ruby/api_connector.rb', line 68 def api_parameters if api_parameters_array.any? "?#{api_parameters_joined}" end end |
#api_parameters_array ⇒ Object
80 81 82 |
# File 'lib/formhub_ruby/api_connector.rb', line 80 def api_parameters_array [api_query, start, limit, sort_query, fields_query] end |
#api_parameters_joined ⇒ Object
84 85 86 |
# File 'lib/formhub_ruby/api_connector.rb', line 84 def api_parameters_joined api_parameters_array.compact.join('&') end |
#api_query ⇒ Object
74 75 76 77 78 |
# File 'lib/formhub_ruby/api_connector.rb', line 74 def api_query if @query "query=#{CGI.escape stringify_hash_values(@query).to_json}" end end |
#api_uri ⇒ Object
64 65 66 |
# File 'lib/formhub_ruby/api_connector.rb', line 64 def api_uri "http://formhub.org/#{@username}/forms/#{@formname}/api" + api_parameters.to_s end |
#cast_to_value_to_int(str) ⇒ Object
127 128 129 130 131 132 133 |
# File 'lib/formhub_ruby/api_connector.rb', line 127 def cast_to_value_to_int(str) begin Integer(str) rescue ArgumentError, TypeError str end end |
#fetch ⇒ Object
26 27 28 |
# File 'lib/formhub_ruby/api_connector.rb', line 26 def fetch get_response(api_uri) end |
#fields_query ⇒ Object
114 115 116 117 118 |
# File 'lib/formhub_ruby/api_connector.rb', line 114 def fields_query if @fields "fields=#{CGI.escape @fields.to_json}" end end |
#get_count ⇒ Object
30 31 32 33 |
# 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) ⇒ Object
private
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# 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 response = Net::HTTP.start(uri.hostname, uri.port) do |http| http.request(req) end begin 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 ⇒ Object
Note that integers seem to be stored as strings in Formhub database, and will be sorted as such
107 108 109 110 111 112 |
# File 'lib/formhub_ruby/api_connector.rb', line 107 def sort_query if @sort validates_sort "sort=#{CGI.escape @sort.to_json}" end end |
#stringify_hash_values(hash) ⇒ Object
100 101 102 |
# File 'lib/formhub_ruby/api_connector.rb', line 100 def stringify_hash_values(hash) hash.merge(hash){|k,hashv|hashv.to_s} end |
#validates_sort ⇒ Object
120 121 122 123 124 125 |
# File 'lib/formhub_ruby/api_connector.rb', line 120 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 |