TwentyFourTru 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in TwentyFourTru.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Cache Hamm
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.
@@ -0,0 +1,42 @@
1
+ # TwentyFourTru
2
+
3
+ Ruby wrapper for 24Tru API, no frameworks required.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'TwentyFourTru'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install TwentyFourTru
18
+
19
+ ## Usage
20
+ ```ruby
21
+ TwentyFourTru::Client.token = 'jkfduisj97823974j2kl24899234' #set api token
22
+
23
+ @studies = TwentyFourTru::Study.all #Load all studies you have access to
24
+ @study = TwentyFourTru::Study.find(9999) #Load single study
25
+
26
+ @assignments = TwentyFourTru::Assignment.all(study_id) #Load all assignments for the given study
27
+ @assignment = TwentyFourTru::Assignment.find(assignment_id) #Load assignment by the given id
28
+ @new_assignment = TwentyFourTru::Assignment.create(STUDY_ID, {name: 'Assignment Name', text: 'Assignment Body'})
29
+
30
+ @responses = TwentyFourTru::Assignment.all(assignment_id) #Load all responses for the given assignment
31
+ @response = TwentyFourTru::Assignment.find(response_id) #Load response by the given id
32
+ @new_text_response = TwentyFourTru::Response.create(ASSIGNMENT_ID, { "owner_id" => PARTICIPANT_ID, "title" => "Betty B. at the store", "text" => "I like to shop"})
33
+
34
+
35
+ ```
36
+ ## Contributing
37
+
38
+ 1. Fork it
39
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
40
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
41
+ 4. Push to the branch (`git push origin my-new-feature`)
42
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'TwentyFourTru/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "TwentyFourTru"
8
+ gem.version = TwentyFourTru::VERSION
9
+ gem.authors = ["Cache Hamm"]
10
+ gem.email = ["chamm@qualvu.com"]
11
+ gem.description = %q{24Tru Api}
12
+ gem.summary = %q{Login to 24tru.com/login and visit your profile to generate your api token}
13
+ gem.homepage = ""
14
+ gem.add_development_dependency 'rspec', '~> 2.13.0'
15
+ gem.add_runtime_dependency 'rest-client', '~> 1.6.7'
16
+ gem.add_runtime_dependency 'json', '~> 1.8.0'
17
+
18
+ gem.files = `git ls-files`.split($/)
19
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
20
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
21
+ gem.require_paths = ["lib"]
22
+ end
@@ -0,0 +1,10 @@
1
+ require 'rest_client'
2
+ require 'json'
3
+ require "TwentyFourTru/version"
4
+ require "TwentyFourTru/client.rb"
5
+ require "TwentyFourTru/study.rb"
6
+ require "TwentyFourTru/assignment.rb"
7
+ require "TwentyFourTru/response.rb"
8
+
9
+ module TwentyFourTru
10
+ end
@@ -0,0 +1,20 @@
1
+ module TwentyFourTru
2
+ class Assignment
3
+
4
+ class << self
5
+ def all(study_id, options={})
6
+ Client.get("/studies/#{study_id}/assignments")
7
+ end
8
+
9
+ def find(assignment_id)
10
+ Client.get("/assignments/#{assignment_id}")
11
+ end
12
+
13
+ def create(study_id, options)
14
+ Client.post("/studies/#{study_id}/assignments", options)
15
+ end
16
+ end
17
+
18
+ attr_accessor :study_id, :assignment_id
19
+ end
20
+ end
@@ -0,0 +1,80 @@
1
+ module TwentyFourTru
2
+
3
+ class Client
4
+
5
+ class NoToken < StandardError; end
6
+
7
+ class << self
8
+ attr_writer :use_ssl, :token, :api_host
9
+
10
+ def use_ssl
11
+ @use_ssl || false
12
+ end
13
+
14
+ # this is your connection for the entire module
15
+ def connection(options={})
16
+ raise NoToken if @token.to_s.empty?
17
+
18
+ @connections ||= {}
19
+
20
+ cached_connection? && !protocol_changed? ? cached_connection : new_connection
21
+ end
22
+
23
+ def get(url)
24
+ JSON.parse(RestClient.get "#{use_ssl ? api_ssl_url : api_url}" + url, :params => {:auth_token => @token}, :content_type => :json)
25
+ end
26
+
27
+ def post(url, options)
28
+ options.merge!({:auth_token => @token})
29
+ JSON.parse(RestClient.post "#{use_ssl ? api_ssl_url : api_url}" + url, options, :content_type => :json)
30
+ end
31
+
32
+ def clear_connections
33
+ @connections = nil
34
+ end
35
+
36
+ def api_host
37
+ @api_host ||= "www.24tru.com"
38
+ end
39
+
40
+ def api_ssl_url
41
+ "https://#{api_host}#{api_path}"
42
+ end
43
+
44
+ def api_url
45
+ "http://#{api_host}#{api_path}"
46
+ end
47
+
48
+ protected
49
+
50
+ def protocol
51
+ use_ssl ? 'https' : 'http'
52
+ end
53
+
54
+ def cached_connection?
55
+ !@connections[@token].nil?
56
+ end
57
+
58
+ def cached_connection
59
+ @connections[@token]
60
+ end
61
+
62
+ def new_connection
63
+ @connections[@token] = RestClient::Resource.new("#{use_ssl ? api_ssl_url : api_url}", :params => {auth_token: @token}, :headers => {'X-TrackerToken' => @token, 'Content-Type' => 'application/xml'})
64
+ end
65
+
66
+ def protocol_changed?
67
+ cached_connection? ? (cached_connection_protocol != protocol) : false
68
+ end
69
+
70
+ def cached_connection_protocol
71
+ cached_connection.url.match(/^(.*):\/\//).captures.first
72
+ end
73
+
74
+ def api_path
75
+ '/r/api/v2'
76
+ end
77
+ end
78
+
79
+ end
80
+ end
@@ -0,0 +1,20 @@
1
+ module TwentyFourTru
2
+ class Response
3
+
4
+ class << self
5
+ def all(assignment_id, options={})
6
+ Client.get("/assignments/#{assignment_id}/responses")
7
+ end
8
+
9
+ def find(response_id)
10
+ Client.get("/responses/#{response_id}")
11
+ end
12
+
13
+ def create(assignment_id, options)
14
+ Client.post("/assignments/#{assignment_id}/responses", options)
15
+ end
16
+ end
17
+
18
+ attr_accessor :assignment_id, :response_id
19
+ end
20
+ end
@@ -0,0 +1,17 @@
1
+ module TwentyFourTru
2
+ class Study
3
+
4
+ class << self
5
+ def all(options={})
6
+ Client.get("/studies")
7
+ end
8
+
9
+ def find(study_id)
10
+ Client.get("/studies/#{study_id}")
11
+ end
12
+ end
13
+
14
+ attr_accessor :study_id
15
+
16
+ end
17
+ end
@@ -0,0 +1,3 @@
1
+ module TwentyFourTru
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,18 @@
1
+ require 'TwentyFourTru'
2
+ require_relative 'spec_helper'
3
+
4
+ describe TwentyFourTru::Assignment do
5
+ it 'should be able load all assignments for a study' do
6
+ TwentyFourTru::Assignment.all(STUDY_ID).should be_a(Array)
7
+ end
8
+
9
+ it 'should be able to load a single assignment' do
10
+ TwentyFourTru::Assignment.find(ASSIGNMENT_ID).should be_kind_of(Hash)
11
+ end
12
+
13
+ it 'should be able to create new assignments' do
14
+ response = TwentyFourTru::Assignment.create(STUDY_ID, {name: 'Assignment Name', text: 'Assignment Body'})
15
+ expected_keys = ['id', 'name', 'text', 'url', 'video_url', 'responses_url']
16
+ expected_keys.each { |key| response.should have_key(key) }
17
+ end
18
+ end
@@ -0,0 +1,9 @@
1
+ require 'TwentyFourTru'
2
+
3
+ describe TwentyFourTru::Client do
4
+ it 'should be able to connect to the api' do
5
+ obj = Object.new
6
+ TwentyFourTru::Client.stub(:new_connection).and_return(obj)
7
+ TwentyFourTru::Client.connection.should eql(obj)
8
+ end
9
+ end
@@ -0,0 +1,22 @@
1
+ require 'TwentyFourTru'
2
+ require_relative 'spec_helper'
3
+
4
+ describe TwentyFourTru::Response do
5
+ it 'should be able load all responses for an assignment' do
6
+ TwentyFourTru::Response.all(ASSIGNMENT_ID).should be_a(Array)
7
+ end
8
+
9
+ it 'should be able to load a single response' do
10
+ TwentyFourTru::Response.find(RESPONSE_ID).should be_kind_of(Hash)
11
+ end
12
+
13
+ it 'should be able to create new text responses' do
14
+ response = TwentyFourTru::Response.create(ASSIGNMENT_ID, {"owner_id" => PARTICIPANT_ID, "title" => "Betty B. at the store", "text" => "this is awesome"})
15
+ expected_keys = ['id', 'name', 'owner', 'insert_dt', 'url', 'video_url', 'mobile_video_url', 'thumbnail_url']
16
+ expected_keys.each { |key| response.should have_key(key) }
17
+
18
+ response['thumbnail_url'].should match('text_response_default')
19
+ response['owner'].should have_key('name')
20
+ response['owner'].should have_key('avatar_url')
21
+ end
22
+ end
@@ -0,0 +1,9 @@
1
+ require 'rspec'
2
+
3
+ STUDY_ID = 3571
4
+ ASSIGNMENT_ID = 21410
5
+ RESPONSE_ID = 460688
6
+ PARTICIPANT_ID = 48651
7
+
8
+ TwentyFourTru::Client.token = 'hcZxQq5bEKndMzbgaqow'
9
+ TwentyFourTru::Client.api_host = 'web1.24tru.dev'
@@ -0,0 +1,12 @@
1
+ require 'TwentyFourTru'
2
+ require_relative 'spec_helper'
3
+
4
+ describe TwentyFourTru::Study do
5
+ it 'should be able to load a single study' do
6
+ TwentyFourTru::Study.find(STUDY_ID).should be_a(Object)
7
+ end
8
+
9
+ it 'should be able load all studies' do
10
+ TwentyFourTru::Study.all.should be_a(Array)
11
+ end
12
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: TwentyFourTru
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Cache Hamm
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-07-23 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 2.13.0
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 2.13.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: rest-client
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 1.6.7
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 1.6.7
46
+ - !ruby/object:Gem::Dependency
47
+ name: json
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: 1.8.0
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 1.8.0
62
+ description: 24Tru Api
63
+ email:
64
+ - chamm@qualvu.com
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - .gitignore
70
+ - Gemfile
71
+ - LICENSE.txt
72
+ - README.md
73
+ - Rakefile
74
+ - TwentyFourTru.gemspec
75
+ - lib/TwentyFourTru.rb
76
+ - lib/TwentyFourTru/assignment.rb
77
+ - lib/TwentyFourTru/client.rb
78
+ - lib/TwentyFourTru/response.rb
79
+ - lib/TwentyFourTru/study.rb
80
+ - lib/TwentyFourTru/version.rb
81
+ - spec/assignment_spec.rb
82
+ - spec/client_spec.rb
83
+ - spec/response_spec.rb
84
+ - spec/spec_helper.rb
85
+ - spec/study_spec.rb
86
+ homepage: ''
87
+ licenses: []
88
+ post_install_message:
89
+ rdoc_options: []
90
+ require_paths:
91
+ - lib
92
+ required_ruby_version: !ruby/object:Gem::Requirement
93
+ none: false
94
+ requirements:
95
+ - - ! '>='
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ none: false
100
+ requirements:
101
+ - - ! '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ requirements: []
105
+ rubyforge_project:
106
+ rubygems_version: 1.8.25
107
+ signing_key:
108
+ specification_version: 3
109
+ summary: Login to 24tru.com/login and visit your profile to generate your api token
110
+ test_files:
111
+ - spec/assignment_spec.rb
112
+ - spec/client_spec.rb
113
+ - spec/response_spec.rb
114
+ - spec/spec_helper.rb
115
+ - spec/study_spec.rb