Class: FormhubRuby::ApiConnector

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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_integersObject

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

#dataObject (readonly)

Returns the value of attribute data.



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

def data
  @data
end

#fieldsObject

Returns the value of attribute fields.



9
10
11
# File 'lib/formhub_ruby/api_connector.rb', line 9

def fields
  @fields
end

#filetypeObject (readonly)

Returns the value of attribute filetype.



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

def filetype
  @filetype
end

#formnameObject (readonly)

Returns the value of attribute formname.



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

def formname
  @formname
end

#limitObject

Returns the value of attribute limit.



9
10
11
# File 'lib/formhub_ruby/api_connector.rb', line 9

def limit
  @limit
end

#passwordObject (readonly)

Returns the value of attribute password.



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

def password
  @password
end

#queryObject

Returns the value of attribute query.



9
10
11
# File 'lib/formhub_ruby/api_connector.rb', line 9

def query
  @query
end

#sortObject

Returns the value of attribute sort.



9
10
11
# File 'lib/formhub_ruby/api_connector.rb', line 9

def sort
  @sort
end

#startObject

Returns the value of attribute start.



9
10
11
# File 'lib/formhub_ruby/api_connector.rb', line 9

def start
  @start
end

#usernameObject (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_parametersObject



69
70
71
72
73
# File 'lib/formhub_ruby/api_connector.rb', line 69

def api_parameters
   if api_parameters_array.any?
      "?#{api_parameters_joined}"
   end
end

#api_parameters_arrayObject



81
82
83
# File 'lib/formhub_ruby/api_connector.rb', line 81

def api_parameters_array
  [api_query, start, limit, sort_query, fields_query]
end

#api_parameters_joinedObject



85
86
87
# File 'lib/formhub_ruby/api_connector.rb', line 85

def api_parameters_joined
  api_parameters_array.compact.join('&')
end

#api_queryObject



75
76
77
78
79
# 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_uriObject



65
66
67
# 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) ⇒ Object



128
129
130
131
132
133
134
# 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

#fetchObject



26
27
28
# File 'lib/formhub_ruby/api_connector.rb', line 26

def fetch
  get_response(api_uri)
end

#fields_queryObject



115
116
117
118
119
# File 'lib/formhub_ruby/api_connector.rb', line 115

def fields_query
  if @fields
    "fields=#{CGI.escape  @fields.to_json}"  
  end    
end

#get_countObject



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
62
# 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_queryObject

Note that integers seem to be stored as strings in Formhub database, and will be sorted as such



108
109
110
111
112
113
# 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) ⇒ Object



101
102
103
# File 'lib/formhub_ruby/api_connector.rb', line 101

def stringify_hash_values(hash)
  hash.merge(hash){|k,hashv|hashv.to_s}
end

#validates_sortObject



121
122
123
124
125
126
# 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