formhub_ruby 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/Guardfile +8 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +10 -0
- data/formhub_ruby.gemspec +33 -0
- data/lib/formhub_ruby.rb +18 -0
- data/lib/formhub_ruby/api_connector.rb +46 -0
- data/lib/formhub_ruby/configuration.rb +10 -0
- data/lib/formhub_ruby/version.rb +3 -0
- data/spec/api_connectior_spec.rb +30 -0
- data/spec/client_spec.rb +0 -0
- data/spec/spec_helper.rb +35 -0
- metadata +215 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 19a10a14da1e872eccaf21412f6ba5dbfb4b577c
|
4
|
+
data.tar.gz: d0708177046b6a808fb1787023a4ad79203beda5
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 08b626d2b17c11e413c3477ab3a7ab5ae65a1b5440c883e4f4c88f3f8d35d88233d65bdc7ff480d9bf3403ce3d1b21e22b748cf19d5c5d3c87e7d508d773d39b
|
7
|
+
data.tar.gz: edb9912b9fb3a5f7d4e809ec576a4e28b4414eeaca2b95eb399f1cdf14d67f21028344ec544134de428da3e4970edf837accd145d00cae2fbbf0c4f9037cd73b
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Guardfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Loïc Seigland
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# FormhubRuby
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'formhub_ruby'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install formhub_ruby
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it ( http://github.com/<my-github-username>/formhub_ruby/fork )
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
require 'rspec/core/rake_task'
|
2
|
+
require 'bundler/gem_tasks'
|
3
|
+
|
4
|
+
# Default directory to look in is `/specs`
|
5
|
+
# Run with `rake spec`
|
6
|
+
RSpec::Core::RakeTask.new(:spec) do |task|
|
7
|
+
task.rspec_opts = ['--color', '--format', 'nested']
|
8
|
+
end
|
9
|
+
|
10
|
+
task :default => :spec
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'formhub_ruby/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'formhub_ruby'
|
8
|
+
spec.version = FormhubRuby::VERSION
|
9
|
+
spec.authors = ['Loïc Seigland']
|
10
|
+
spec.email = ['loic@loicseigland.ie']
|
11
|
+
spec.summary = %q(Simple Client for the Formhub API)
|
12
|
+
# spec.description = %q(TODO: Write a longer description. Optional.)
|
13
|
+
spec.homepage = 'http://loicseigland.ie'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ['lib']
|
20
|
+
|
21
|
+
spec.add_development_dependency 'bundler', '~> 1.5'
|
22
|
+
spec.add_development_dependency 'rake'
|
23
|
+
spec.add_development_dependency 'rspec'
|
24
|
+
spec.add_development_dependency 'rspec-nc'
|
25
|
+
spec.add_development_dependency 'guard'
|
26
|
+
spec.add_development_dependency 'guard-rspec'
|
27
|
+
spec.add_development_dependency 'pry'
|
28
|
+
spec.add_development_dependency 'pry-remote'
|
29
|
+
spec.add_development_dependency 'pry-nav'
|
30
|
+
spec.add_development_dependency 'vcr'
|
31
|
+
spec.add_development_dependency 'webmock'
|
32
|
+
|
33
|
+
end
|
data/lib/formhub_ruby.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'formhub_ruby/version'
|
2
|
+
require 'formhub_ruby/configuration'
|
3
|
+
require 'formhub_ruby/api_connector'
|
4
|
+
|
5
|
+
module FormhubRuby
|
6
|
+
|
7
|
+
class << self
|
8
|
+
attr_writer :configuration
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.configuration
|
12
|
+
@configuration ||= Configuration.new
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.configure
|
16
|
+
yield(configuration)
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require 'json'
|
3
|
+
module FormhubRuby
|
4
|
+
class ApiConnector
|
5
|
+
attr_reader :formname, :filetype, :username, :password, :data
|
6
|
+
|
7
|
+
def initialize(args)
|
8
|
+
@username = args[:username] || FormhubRuby.configuration.username
|
9
|
+
@password = args[:password] || FormhubRuby.configuration.password
|
10
|
+
@formname = args[:formname]
|
11
|
+
@filetype = args[:filetype] || 'json'
|
12
|
+
end
|
13
|
+
|
14
|
+
def fetch
|
15
|
+
# CAN DO THAT A LATER STAGE: Define different url format
|
16
|
+
# for different data point formats
|
17
|
+
# uri = URI(form_uri)
|
18
|
+
uri = URI("http://formhub.org/#{@username}/forms/#{@formname}/api")
|
19
|
+
req = Net::HTTP::Get.new(uri)
|
20
|
+
req.basic_auth @username, @password
|
21
|
+
response = Net::HTTP.start(uri.hostname, uri.port) do |http|
|
22
|
+
http.request(req)
|
23
|
+
end
|
24
|
+
|
25
|
+
begin
|
26
|
+
@data = JSON.parse(response.body)
|
27
|
+
rescue
|
28
|
+
raise 'API connection error'
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
# private
|
33
|
+
|
34
|
+
# CAN DO THAT A LATER STAGE: Define different data point formats
|
35
|
+
# (now will default to JSON)
|
36
|
+
|
37
|
+
# def form_uri
|
38
|
+
# case @filetype.downcase
|
39
|
+
# when 'json' then "http://formhub.org/#{@username}/forms/#{@formname}/api"
|
40
|
+
# when 'csv' then "http://formhub.org/#{@username}/forms/#{@formname}/data.csv"
|
41
|
+
# when 'xls' then "http://formhub.org/#{@username}/forms/#{@formname}/data.xls"
|
42
|
+
# end
|
43
|
+
# end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'formhub_ruby'
|
2
|
+
require 'spec_helper'
|
3
|
+
require 'yaml'
|
4
|
+
|
5
|
+
describe FormhubRuby::ApiConnector do
|
6
|
+
|
7
|
+
before :each do
|
8
|
+
credentials = YAML.load_file('spec/fixtures/test_credentials.yml')
|
9
|
+
FormhubRuby.configure do |config|
|
10
|
+
config.username = credentials['username']
|
11
|
+
config.password = credentials['password']
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'successfully connects to the FormHub API and retrieve JSON Data' do
|
16
|
+
VCR.use_cassette 'successful_connection' do
|
17
|
+
connection = FormhubRuby::ApiConnector.new(formname: 'survey')
|
18
|
+
connection.fetch
|
19
|
+
connection.data.should be_a_kind_of(Array)
|
20
|
+
connection.data[0].should be_a_kind_of(Object)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
it "displays appropriate message if the JSON data is not successfully retrieved" do
|
25
|
+
stub_request(:any, "http://formhub.org/#{FormhubRuby.configuration.username}/forms/survey/api").to_return(:body => "NO JSON HERE")
|
26
|
+
connection = FormhubRuby::ApiConnector.new(formname: 'fake')
|
27
|
+
expect {connection.fetch}.to raise_error("API connection error")
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
data/spec/client_spec.rb
ADDED
File without changes
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'vcr'
|
2
|
+
require 'webmock'
|
3
|
+
require 'webmock/rspec'
|
4
|
+
require 'factory_girl'
|
5
|
+
|
6
|
+
RSpec.configure do |config|
|
7
|
+
FactoryGirl.find_definitions
|
8
|
+
|
9
|
+
config.include WebMock::API
|
10
|
+
config.before(:each) do
|
11
|
+
WebMock.reset!
|
12
|
+
VCR.configure do |c|
|
13
|
+
c.cassette_library_dir = 'spec/fixtures/vcr_cassettes'
|
14
|
+
c.allow_http_connections_when_no_cassette = true
|
15
|
+
c.hook_into :webmock
|
16
|
+
# c.before_record do |i|
|
17
|
+
|
18
|
+
# i.response.headers.delete('Set-Cookie')
|
19
|
+
# i.request.headers.delete('Authorization')
|
20
|
+
|
21
|
+
# u = URI.parse(i.request.uri)
|
22
|
+
# i.request.uri.sub!(%r{:\/\/.*#{Regexp.escape(u.host)}}, "://#{u.host}")
|
23
|
+
|
24
|
+
# end
|
25
|
+
|
26
|
+
# Matches authenticated requests regardless of their Basic
|
27
|
+
# auth string (https://user:pass@domain.tld)
|
28
|
+
# c.register_request_matcher :anonymized_uri do |request_1, request_2|
|
29
|
+
# (URI(request_1.uri).port == URI(request_2.uri).port) &&
|
30
|
+
# URI(request_1.uri).path == URI(request_2.uri).path
|
31
|
+
# end
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
end
|
metadata
ADDED
@@ -0,0 +1,215 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: formhub_ruby
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Loïc Seigland
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-05-21 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.5'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.5'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec-nc
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: guard
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: guard-rspec
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: pry
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: pry-remote
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: pry-nav
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
139
|
+
- !ruby/object:Gem::Dependency
|
140
|
+
name: vcr
|
141
|
+
requirement: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - ">="
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: '0'
|
146
|
+
type: :development
|
147
|
+
prerelease: false
|
148
|
+
version_requirements: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - ">="
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: '0'
|
153
|
+
- !ruby/object:Gem::Dependency
|
154
|
+
name: webmock
|
155
|
+
requirement: !ruby/object:Gem::Requirement
|
156
|
+
requirements:
|
157
|
+
- - ">="
|
158
|
+
- !ruby/object:Gem::Version
|
159
|
+
version: '0'
|
160
|
+
type: :development
|
161
|
+
prerelease: false
|
162
|
+
version_requirements: !ruby/object:Gem::Requirement
|
163
|
+
requirements:
|
164
|
+
- - ">="
|
165
|
+
- !ruby/object:Gem::Version
|
166
|
+
version: '0'
|
167
|
+
description:
|
168
|
+
email:
|
169
|
+
- loic@loicseigland.ie
|
170
|
+
executables: []
|
171
|
+
extensions: []
|
172
|
+
extra_rdoc_files: []
|
173
|
+
files:
|
174
|
+
- ".gitignore"
|
175
|
+
- Gemfile
|
176
|
+
- Guardfile
|
177
|
+
- LICENSE.txt
|
178
|
+
- README.md
|
179
|
+
- Rakefile
|
180
|
+
- formhub_ruby.gemspec
|
181
|
+
- lib/formhub_ruby.rb
|
182
|
+
- lib/formhub_ruby/api_connector.rb
|
183
|
+
- lib/formhub_ruby/configuration.rb
|
184
|
+
- lib/formhub_ruby/version.rb
|
185
|
+
- spec/api_connectior_spec.rb
|
186
|
+
- spec/client_spec.rb
|
187
|
+
- spec/spec_helper.rb
|
188
|
+
homepage: http://loicseigland.ie
|
189
|
+
licenses:
|
190
|
+
- MIT
|
191
|
+
metadata: {}
|
192
|
+
post_install_message:
|
193
|
+
rdoc_options: []
|
194
|
+
require_paths:
|
195
|
+
- lib
|
196
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
197
|
+
requirements:
|
198
|
+
- - ">="
|
199
|
+
- !ruby/object:Gem::Version
|
200
|
+
version: '0'
|
201
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
202
|
+
requirements:
|
203
|
+
- - ">="
|
204
|
+
- !ruby/object:Gem::Version
|
205
|
+
version: '0'
|
206
|
+
requirements: []
|
207
|
+
rubyforge_project:
|
208
|
+
rubygems_version: 2.2.0
|
209
|
+
signing_key:
|
210
|
+
specification_version: 4
|
211
|
+
summary: Simple Client for the Formhub API
|
212
|
+
test_files:
|
213
|
+
- spec/api_connectior_spec.rb
|
214
|
+
- spec/client_spec.rb
|
215
|
+
- spec/spec_helper.rb
|