smarteru 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +46 -0
- data/Rakefile +21 -0
- data/lib/smarteru.rb +11 -0
- data/lib/smarteru/client.rb +98 -0
- data/lib/smarteru/response.rb +39 -0
- data/lib/smarteru/version.rb +3 -0
- data/test/helper.rb +29 -0
- data/test/smarteru_test.rb +45 -0
- data/test/support/requests/getGroup.xml +7 -0
- data/test/support/responses/getGroup.xml +26 -0
- metadata +144 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 811c7b26440c9f22c4b5d41f6ec6a460e361d75d
|
4
|
+
data.tar.gz: 548cbb83653d4cee166a5ca60050b12fc4a065c6
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a5f2c67db38f87430695c4df2181f4661ddf56ee62dcb35263e44b466465377606058ce4974559730de273c3209b10cf349ed2e90208f971201aadbfa8fe29c1
|
7
|
+
data.tar.gz: 6581fb66f399816bf60e23a88696b3737c2d37122658c0ce18c04d9a47700365ea9e789a666f46d673766ffbe8980cef6e5d03de62eb22210bea9781d1beb42d
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2015 Eyecuelab
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
# SmarterU API wrapper
|
2
|
+
|
3
|
+
Ruby wrapper for a SmarterU API.
|
4
|
+
Allows access to a Smarteru API operations: [api documentation](http://help.smarteru.com/)
|
5
|
+
|
6
|
+
|
7
|
+
## Basic Usage
|
8
|
+
|
9
|
+
API wrapper can be used from the command line or as part of a Ruby web framework. First install the gem:
|
10
|
+
|
11
|
+
gem install smarteru
|
12
|
+
|
13
|
+
Instantiate a client with your SmarterU credentials. You can get it [here](http://smarteru.com/)
|
14
|
+
|
15
|
+
require 'smarteru'
|
16
|
+
client = Smarteru::Client.new(account_api_key: "ACCOUNT_API_KEY", user_api_key: "USER_API_KEY")
|
17
|
+
|
18
|
+
Make api calls. You can find a list of API endpoints [here](http://help.smarteru.com/)
|
19
|
+
|
20
|
+
response = client.request('getGroup', {group: {name: 'MyGroup'}})
|
21
|
+
response.result =>
|
22
|
+
{
|
23
|
+
group: {
|
24
|
+
name: "MyGroup",
|
25
|
+
group_id: nil,
|
26
|
+
created_date: "2015-01-22 23:42:55.553",
|
27
|
+
modified_date: "2015-01-23 01:25:02.013",
|
28
|
+
description: {
|
29
|
+
p: "Group Description ..."
|
30
|
+
},
|
31
|
+
home_group_message: {
|
32
|
+
p: "Welcome to The Group"
|
33
|
+
},
|
34
|
+
notification_emails: nil,
|
35
|
+
user_count: "2",
|
36
|
+
learning_module_count: "2",
|
37
|
+
tags: nil,
|
38
|
+
status: "Active"
|
39
|
+
}
|
40
|
+
}
|
41
|
+
|
42
|
+
|
43
|
+
## Testing
|
44
|
+
|
45
|
+
bundle install
|
46
|
+
rake
|
data/Rakefile
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
require 'rake'
|
13
|
+
|
14
|
+
require 'rake/testtask'
|
15
|
+
Rake::TestTask.new(:test) do |test|
|
16
|
+
test.libs << 'test'
|
17
|
+
test.pattern = 'test/**/*_test.rb'
|
18
|
+
test.verbose = true
|
19
|
+
end
|
20
|
+
|
21
|
+
task :default => :test
|
data/lib/smarteru.rb
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
module Smarteru
|
2
|
+
class Client
|
3
|
+
attr_reader :account_api_key, :user_api_key,
|
4
|
+
:use_ssl, :verify_ssl, :ssl_ca_file,
|
5
|
+
:api_url
|
6
|
+
|
7
|
+
# Create an instance of an API client
|
8
|
+
#
|
9
|
+
# ==== Attributes
|
10
|
+
# * +options+ - Access credentials and options hash, required keys are: account_api_key, user_api_key
|
11
|
+
# ==== Example
|
12
|
+
# client = Smarteru::Client.new({account_api_key: 'abc', user_api_key: 'abc'})
|
13
|
+
def initialize options = {}
|
14
|
+
@account_api_key = options[:account_api_key]
|
15
|
+
@user_api_key = options[:user_api_key]
|
16
|
+
@use_ssl = options[:use_ssl] || true
|
17
|
+
@verify_ssl = options[:verify_ssl]
|
18
|
+
@ssl_ca_file = options[:ssl_ca_file]
|
19
|
+
@api_url = "#{use_ssl ? 'https' : 'http'}://#{API_HOST}/#{API_VERSION}/"
|
20
|
+
end
|
21
|
+
|
22
|
+
# Make an API request
|
23
|
+
#
|
24
|
+
# ==== Attributes
|
25
|
+
# * +operation+ - Operation method eg getGroup
|
26
|
+
# * +data+ - Data hash
|
27
|
+
# ==== Example
|
28
|
+
# client.request("getGroup", {
|
29
|
+
# group: {
|
30
|
+
# name: 'MyGroup'
|
31
|
+
# }
|
32
|
+
# })
|
33
|
+
def request(operation, data)
|
34
|
+
opts = {
|
35
|
+
method: :post,
|
36
|
+
url: api_url,
|
37
|
+
payload: {'Package' => body(operation, data)},
|
38
|
+
content_type: :xml,
|
39
|
+
verify_ssl: verify_ssl,
|
40
|
+
ssl_ca_file: ssl_ca_file
|
41
|
+
}
|
42
|
+
Response.new(RestClient::Request.execute(opts))
|
43
|
+
end
|
44
|
+
|
45
|
+
private
|
46
|
+
|
47
|
+
# Build xml request body
|
48
|
+
#
|
49
|
+
# ==== Attributes
|
50
|
+
# * +operation+ - Operation method
|
51
|
+
# * +parameters+ - Parameters hash
|
52
|
+
# ==== Example
|
53
|
+
# client.body("createUser", {email: 'email@example.com', ...})
|
54
|
+
def body(operation, param_data)
|
55
|
+
%{<?xml version="1.0" encoding="UTF-8"?>
|
56
|
+
<SmarterU>
|
57
|
+
<AccountAPI>#{account_api_key}</AccountAPI>
|
58
|
+
<UserAPI>#{user_api_key}</UserAPI>
|
59
|
+
<Method>#{operation}</Method>
|
60
|
+
<Parameters>#{body_parameters(param_data)}</Parameters>
|
61
|
+
</SmarterU>}
|
62
|
+
end
|
63
|
+
|
64
|
+
# Build body parameteres xml
|
65
|
+
#
|
66
|
+
# ==== Attributes
|
67
|
+
# * +parameters+ - Parameters hash
|
68
|
+
def body_parameters(parameters)
|
69
|
+
parameters_xml = ''
|
70
|
+
parameters.each_pair do |k,v|
|
71
|
+
key = parameter_key(k)
|
72
|
+
|
73
|
+
case v
|
74
|
+
when Hash
|
75
|
+
val = body_parameters(v)
|
76
|
+
when Array
|
77
|
+
val = v.map {|i| body_parameters(i) }.join('')
|
78
|
+
else
|
79
|
+
val = "<![CDATA[#{v}]]>"
|
80
|
+
end
|
81
|
+
|
82
|
+
parameters_xml << "<#{key}>#{val}</#{key}>"
|
83
|
+
end
|
84
|
+
parameters_xml
|
85
|
+
end
|
86
|
+
|
87
|
+
# Prepare parameter key
|
88
|
+
#
|
89
|
+
# ==== Attributes
|
90
|
+
# * +parameters+ - Parameters hash
|
91
|
+
def parameter_key(term)
|
92
|
+
string = term.to_s
|
93
|
+
string = string.sub(/^[a-z\d]*/) { $&.capitalize }
|
94
|
+
string.gsub!(/(?:_|(\/))([a-z\d]*)/i) { "#{$1}#{$2.capitalize}" }
|
95
|
+
string
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module Smarteru
|
2
|
+
class Response
|
3
|
+
attr_reader :data, :hash, :parser
|
4
|
+
|
5
|
+
# Initializes an API response
|
6
|
+
#
|
7
|
+
# ==== Attributes
|
8
|
+
# * +resp+ - RestClient response from the API
|
9
|
+
def initialize(res)
|
10
|
+
@data = res
|
11
|
+
@parser = XmlHasher::Parser.new(
|
12
|
+
snakecase: true,
|
13
|
+
ignore_namespaces: true,
|
14
|
+
string_keys: false
|
15
|
+
)
|
16
|
+
end
|
17
|
+
|
18
|
+
# Hash representation of response data
|
19
|
+
def to_hash
|
20
|
+
return @hash if @hash
|
21
|
+
@hash = parser.parse(data.to_s.gsub(/\<!\[CDATA\[(.+)\]\]\>/) {$1})
|
22
|
+
end
|
23
|
+
|
24
|
+
# Return true/false based on the API response status
|
25
|
+
def success?
|
26
|
+
to_hash[:smarter_u][:result] == 'Success'
|
27
|
+
rescue
|
28
|
+
false
|
29
|
+
end
|
30
|
+
|
31
|
+
def result
|
32
|
+
to_hash[:smarter_u][:info]
|
33
|
+
end
|
34
|
+
|
35
|
+
def error
|
36
|
+
to_hash[:smarter_u][:errors]
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
data/test/helper.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'webmock/test_unit'
|
3
|
+
|
4
|
+
require 'smarteru'
|
5
|
+
|
6
|
+
class Test::Unit::TestCase
|
7
|
+
ACCESS_CREDENTIALS = {
|
8
|
+
account_api_key: "abc",
|
9
|
+
user_api_key: "abc"
|
10
|
+
}
|
11
|
+
|
12
|
+
def stub_v2_api_call(operation, payload_package_xml)
|
13
|
+
filename = "#{operation}.xml"
|
14
|
+
stub_request(:post, 'https://api.smarteru.com/apiv2/')
|
15
|
+
.with(:body => {"Package"=>"#{payload_package_xml}"})
|
16
|
+
.to_return(
|
17
|
+
body: expected_body(filename),
|
18
|
+
status: 200
|
19
|
+
)
|
20
|
+
end
|
21
|
+
|
22
|
+
def request_payload(filename)
|
23
|
+
File.read(File.join(File.dirname(__FILE__), 'support/requests/' << filename))
|
24
|
+
end
|
25
|
+
|
26
|
+
def expected_body(filename)
|
27
|
+
File.read(File.join(File.dirname(__FILE__), 'support/responses/' << filename))
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class TestSmarteru < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def setup
|
6
|
+
@client = Smarteru::Client.new(ACCESS_CREDENTIALS)
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_request_get_group
|
10
|
+
stub_request = stub_v2_api_call('getGroup', request_payload('getGroup.xml'))
|
11
|
+
response = @client.request('getGroup', {group: {name: 'MyGroup'}})
|
12
|
+
assert_equal(response.data, expected_body('getGroup.xml'))
|
13
|
+
assert_requested(stub_request)
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_response_result_hash
|
17
|
+
stub_request = stub_v2_api_call('getGroup', request_payload('getGroup.xml'))
|
18
|
+
response = @client.request('getGroup', {group: {name: 'MyGroup'}})
|
19
|
+
assert_equal(response.result, {
|
20
|
+
group: {
|
21
|
+
name: "MyGroup",
|
22
|
+
group_id: nil,
|
23
|
+
created_date: "2015-01-22 23:42:55.553",
|
24
|
+
modified_date: "2015-01-23 01:25:02.013",
|
25
|
+
description: {
|
26
|
+
p: "Group Description ..."
|
27
|
+
},
|
28
|
+
home_group_message: {
|
29
|
+
p: "Welcome to The Group"
|
30
|
+
},
|
31
|
+
notification_emails: nil,
|
32
|
+
user_count: "2",
|
33
|
+
learning_module_count: "2",
|
34
|
+
tags: nil,
|
35
|
+
status: "Active"
|
36
|
+
}
|
37
|
+
})
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_response_success_method
|
41
|
+
stub_request = stub_v2_api_call('getGroup', request_payload('getGroup.xml'))
|
42
|
+
response = @client.request('getGroup', {group: {name: 'MyGroup'}})
|
43
|
+
assert_equal(true, response.success?)
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
2
|
+
<SmarterU>
|
3
|
+
<Result>Success</Result>
|
4
|
+
<Info>
|
5
|
+
<Group>
|
6
|
+
<Name><![CDATA[MyGroup]]></Name>
|
7
|
+
<GroupID/>
|
8
|
+
<Created_Date><![CDATA[2015-01-22 23:42:55.553]]></Created_Date>
|
9
|
+
<Modified_Date><![CDATA[2015-01-23 01:25:02.013]]></Modified_Date>
|
10
|
+
<Description><![CDATA[<p>Group Description ...</p>]]></Description>
|
11
|
+
<HomeGroupMessage><![CDATA[<p>Welcome to The Group</p>]]></HomeGroupMessage>
|
12
|
+
<NotificationEmails>
|
13
|
+
|
14
|
+
</NotificationEmails>
|
15
|
+
<UserCount>2</UserCount>
|
16
|
+
<LearningModuleCount>2</LearningModuleCount>
|
17
|
+
<Tags>
|
18
|
+
|
19
|
+
</Tags>
|
20
|
+
<Status><![CDATA[Active]]></Status>
|
21
|
+
</Group>
|
22
|
+
</Info>
|
23
|
+
<Errors>
|
24
|
+
|
25
|
+
</Errors>
|
26
|
+
</SmarterU>
|
metadata
ADDED
@@ -0,0 +1,144 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: smarteru
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Sasha Shamne
|
8
|
+
- EyecueLab
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2015-01-27 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rest-client
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - "~>"
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '1.7'
|
21
|
+
type: :runtime
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - "~>"
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '1.7'
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: libxml-ruby
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - "~>"
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '2.8'
|
35
|
+
type: :runtime
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - "~>"
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '2.8'
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: xmlhasher
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - "~>"
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: 0.0.6
|
49
|
+
type: :runtime
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - "~>"
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: 0.0.6
|
56
|
+
- !ruby/object:Gem::Dependency
|
57
|
+
name: rdoc
|
58
|
+
requirement: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - "~>"
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '4.2'
|
63
|
+
type: :development
|
64
|
+
prerelease: false
|
65
|
+
version_requirements: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - "~>"
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '4.2'
|
70
|
+
- !ruby/object:Gem::Dependency
|
71
|
+
name: rake
|
72
|
+
requirement: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - "~>"
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '10.4'
|
77
|
+
type: :development
|
78
|
+
prerelease: false
|
79
|
+
version_requirements: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - "~>"
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '10.4'
|
84
|
+
- !ruby/object:Gem::Dependency
|
85
|
+
name: webmock
|
86
|
+
requirement: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - "~>"
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '1.8'
|
91
|
+
type: :development
|
92
|
+
prerelease: false
|
93
|
+
version_requirements: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - "~>"
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '1.8'
|
98
|
+
description: Ruby wrapper for Smarteru API
|
99
|
+
email:
|
100
|
+
- alexander.shamne@gmail.com
|
101
|
+
executables: []
|
102
|
+
extensions: []
|
103
|
+
extra_rdoc_files: []
|
104
|
+
files:
|
105
|
+
- MIT-LICENSE
|
106
|
+
- README.md
|
107
|
+
- Rakefile
|
108
|
+
- lib/smarteru.rb
|
109
|
+
- lib/smarteru/client.rb
|
110
|
+
- lib/smarteru/response.rb
|
111
|
+
- lib/smarteru/version.rb
|
112
|
+
- test/helper.rb
|
113
|
+
- test/smarteru_test.rb
|
114
|
+
- test/support/requests/getGroup.xml
|
115
|
+
- test/support/responses/getGroup.xml
|
116
|
+
homepage: http://github.com/eyecuelab/smarteru
|
117
|
+
licenses:
|
118
|
+
- MIT
|
119
|
+
metadata: {}
|
120
|
+
post_install_message:
|
121
|
+
rdoc_options: []
|
122
|
+
require_paths:
|
123
|
+
- lib
|
124
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
125
|
+
requirements:
|
126
|
+
- - ">="
|
127
|
+
- !ruby/object:Gem::Version
|
128
|
+
version: '0'
|
129
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
130
|
+
requirements:
|
131
|
+
- - ">="
|
132
|
+
- !ruby/object:Gem::Version
|
133
|
+
version: '0'
|
134
|
+
requirements: []
|
135
|
+
rubyforge_project:
|
136
|
+
rubygems_version: 2.4.4
|
137
|
+
signing_key:
|
138
|
+
specification_version: 4
|
139
|
+
summary: Allows access to a Smarteru API operations http://help.smarteru.com/
|
140
|
+
test_files:
|
141
|
+
- test/helper.rb
|
142
|
+
- test/smarteru_test.rb
|
143
|
+
- test/support/requests/getGroup.xml
|
144
|
+
- test/support/responses/getGroup.xml
|