loquor 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +8 -10
- data/lib/loquor.rb +10 -2
- data/lib/loquor/api_call.rb +2 -2
- data/lib/loquor/api_calls/create.rb +14 -0
- data/lib/loquor/api_calls/index.rb +1 -1
- data/lib/loquor/api_calls/show.rb +1 -1
- data/lib/loquor/http_action.rb +1 -0
- data/lib/loquor/http_actions/get.rb +4 -2
- data/lib/loquor/http_actions/post.rb +4 -1
- data/lib/loquor/representation.rb +5 -2
- data/lib/loquor/representations.rb +6 -9
- data/lib/loquor/version.rb +1 -1
- data/test/http_action_test.rb +22 -0
- data/test/http_actions/get_test.rb +6 -13
- data/test/http_actions/post_test.rb +3 -11
- data/test/representations_test.rb +6 -0
- metadata +4 -4
- data/lib/loquor/path_builder.rb +0 -44
- data/test/path_builder_test.rb +0 -46
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 070654de10baeadaf8cdb835f456d178456077c0
|
4
|
+
data.tar.gz: 0d3e50f2eabba9f9aa1e16f5b4cf56fe14cb5afe
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8a5ced6b3668a0ad27d4c00c3c7294737b593e909a3663b147565d2b628b0528912d385705a00e8419440b1f3c0b10953f0c95fec583e5d8375ec8e464ea8bbf
|
7
|
+
data.tar.gz: 74d3922223e408c2a89cc5134593df7cfc2316a83d6873f968392e8a26fda795a73b15aaf3b12b6ecf21abc8d42ac3ff789c3b9556c33d1fdec79c7744635d85
|
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# Loquor
|
1
|
+
# Loquor - Meducation API Dispatcher
|
2
2
|
|
3
3
|
[![Build Status](https://travis-ci.org/meducation/loquor.png)](https://travis-ci.org/meducation/loquor)
|
4
4
|
[![Dependencies](https://gemnasium.com/meducation/loquor.png?travis)](https://gemnasium.com/meducation/loquor)
|
@@ -10,16 +10,14 @@ Handles calls to the Meducation API via an ActiveRecord-style interface
|
|
10
10
|
|
11
11
|
Add this line to your application's Gemfile:
|
12
12
|
|
13
|
-
|
13
|
+
```ruby
|
14
|
+
gem 'loquor'
|
15
|
+
```
|
14
16
|
|
15
17
|
And then execute:
|
16
18
|
|
17
19
|
$ bundle
|
18
20
|
|
19
|
-
Or install it yourself as:
|
20
|
-
|
21
|
-
$ gem install loquor
|
22
|
-
|
23
21
|
|
24
22
|
## Usage
|
25
23
|
|
@@ -32,16 +30,16 @@ Loquor.config do |config|
|
|
32
30
|
end
|
33
31
|
```
|
34
32
|
|
35
|
-
Now you make requests to get, create, update, destroy and list a range of objects, like this:
|
33
|
+
Now you can make requests to get, create, update, destroy and list a range of objects, like this:
|
36
34
|
|
37
35
|
```ruby
|
38
|
-
User.where(email: "jeremy@meducation.net").where(name: "Jeremy").each do |user|
|
36
|
+
Loquor::User.where(email: "jeremy@meducation.net").where(name: "Jeremy").each do |user|
|
39
37
|
p "The user with id ##{user['id']} is #{user['name']}."
|
40
38
|
end
|
41
39
|
|
42
|
-
User.find(2) # => {id: 2, name: "Jeremy Walker"}
|
40
|
+
Loquor::User.find(2) # => {id: 2, name: "Jeremy Walker"}
|
43
41
|
|
44
|
-
User.create(name: "Jeremy Walker", email: "jeremy@meducation.net") # => {id: 2, name: "Jeremy Walker", email "jeremy@meducation.net"}
|
42
|
+
Loquor::User.create(name: "Jeremy Walker", email: "jeremy@meducation.net") # => {id: 2, name: "Jeremy Walker", email "jeremy@meducation.net"}
|
45
43
|
```
|
46
44
|
|
47
45
|
### Supported Objects
|
data/lib/loquor.rb
CHANGED
@@ -5,14 +5,20 @@ require 'filum'
|
|
5
5
|
require "loquor/version"
|
6
6
|
require "loquor/configuration"
|
7
7
|
require "loquor/client"
|
8
|
-
require 'loquor/path_builder'
|
9
8
|
require 'loquor/representation'
|
10
|
-
require 'loquor/representations'
|
11
9
|
|
12
10
|
require 'loquor/api_call'
|
13
11
|
require "loquor/http_action"
|
14
12
|
|
15
13
|
module Loquor
|
14
|
+
|
15
|
+
Representations = {
|
16
|
+
"GroupDiscussion" => "/group_discussions",
|
17
|
+
"GroupDiscussionPost" => "/group_discussion_posts",
|
18
|
+
"MediaFile" => "/media_files",
|
19
|
+
"User" => "/users"
|
20
|
+
}
|
21
|
+
|
16
22
|
def self.config
|
17
23
|
if block_given?
|
18
24
|
yield loquor.config
|
@@ -35,3 +41,5 @@ module Loquor
|
|
35
41
|
@loquor ||= Client.new
|
36
42
|
end
|
37
43
|
end
|
44
|
+
|
45
|
+
require 'loquor/representations'
|
data/lib/loquor/api_call.rb
CHANGED
data/lib/loquor/http_action.rb
CHANGED
@@ -9,7 +9,10 @@ module Loquor
|
|
9
9
|
end
|
10
10
|
|
11
11
|
def get
|
12
|
-
|
12
|
+
@config.logger.info "Making GET request to: #{@url}"
|
13
|
+
response = JSON.parse(signed_request.execute)
|
14
|
+
@config.logger.info "Signed request executed. Response: #{response}"
|
15
|
+
response
|
13
16
|
end
|
14
17
|
|
15
18
|
private
|
@@ -19,4 +22,3 @@ module Loquor
|
|
19
22
|
end
|
20
23
|
end
|
21
24
|
end
|
22
|
-
|
@@ -10,7 +10,10 @@ module Loquor
|
|
10
10
|
end
|
11
11
|
|
12
12
|
def post
|
13
|
-
|
13
|
+
@config.logger.info "Making POST request to: #{@url}"
|
14
|
+
response = JSON.parse(signed_request.execute)
|
15
|
+
@config.logger.info "Signed request executed. Response: #{response}"
|
16
|
+
response
|
14
17
|
end
|
15
18
|
|
16
19
|
private
|
@@ -1,8 +1,7 @@
|
|
1
1
|
module Loquor
|
2
2
|
module Representation
|
3
3
|
module ClassMethods
|
4
|
-
|
5
|
-
[:find, :where].each do |proxy|
|
4
|
+
[:find, :where, :create].each do |proxy|
|
6
5
|
define_method proxy do |*args|
|
7
6
|
new.send proxy, *args
|
8
7
|
end
|
@@ -17,6 +16,10 @@ module Loquor
|
|
17
16
|
def where(*args)
|
18
17
|
ApiCall::Index.new(self.class.path).where(*args)
|
19
18
|
end
|
19
|
+
|
20
|
+
def create(payload)
|
21
|
+
ApiCall::Create.new(self.class.path, payload).execute
|
22
|
+
end
|
20
23
|
end
|
21
24
|
end
|
22
25
|
end
|
@@ -1,16 +1,13 @@
|
|
1
|
-
|
2
|
-
"Group::Discussion" => "/group/:group_id/discussions",
|
3
|
-
"Group::DiscussionPost" => "/group/:group_id/discussion",
|
4
|
-
"MediaFile" => "/media_files",
|
5
|
-
"User" => "/users"
|
6
|
-
}.each do |name, path|
|
1
|
+
Loquor::Representations.each do |name, path|
|
7
2
|
klass = Class.new(Object) do
|
8
3
|
extend Loquor::Representation::ClassMethods
|
9
4
|
include Loquor::Representation::InstanceMethods
|
10
5
|
|
11
|
-
|
12
|
-
path
|
13
|
-
|
6
|
+
instance_eval <<-EOS
|
7
|
+
def path
|
8
|
+
"#{path}"
|
9
|
+
end
|
10
|
+
EOS
|
14
11
|
end
|
15
12
|
|
16
13
|
# Split off the Group and Discussion parts
|
data/lib/loquor/version.rb
CHANGED
@@ -0,0 +1,22 @@
|
|
1
|
+
module Loquor
|
2
|
+
class HttpAction::Test < Minitest::Test
|
3
|
+
def setup
|
4
|
+
super
|
5
|
+
@access_id = "123"
|
6
|
+
@secret_key = "Foobar132"
|
7
|
+
@endpoint = "http://www.thefoobar.com"
|
8
|
+
end
|
9
|
+
|
10
|
+
def deps
|
11
|
+
logger = mock()
|
12
|
+
logger.stubs(info: nil)
|
13
|
+
|
14
|
+
config = mock()
|
15
|
+
config.stubs(logger: logger)
|
16
|
+
config.stubs(access_id: @access_id)
|
17
|
+
config.stubs(secret_key: @secret_key)
|
18
|
+
config.stubs(endpoint: @endpoint)
|
19
|
+
{config: config}
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -1,7 +1,8 @@
|
|
1
1
|
require File.expand_path('../../test_helper', __FILE__)
|
2
2
|
|
3
3
|
module Loquor
|
4
|
-
class HttpAction::GetTest <
|
4
|
+
class HttpAction::GetTest < HttpAction::Test
|
5
|
+
|
5
6
|
def test_get_should_call_new
|
6
7
|
url = "foobar"
|
7
8
|
deps = {x: true}
|
@@ -17,32 +18,24 @@ module Loquor
|
|
17
18
|
def test_get_parses_request
|
18
19
|
output = {'foo' => 'bar'}
|
19
20
|
json = output.to_json
|
20
|
-
|
21
|
+
|
22
|
+
gets = HttpAction::Get.new("", deps)
|
21
23
|
gets.expects(signed_request: mock(execute: json))
|
22
24
|
assert_equal output, gets.get
|
23
25
|
end
|
24
26
|
|
25
27
|
def test_request_is_generated_correctly
|
26
28
|
url = "/foobar"
|
27
|
-
|
28
|
-
config = mock(endpoint: endpoint)
|
29
|
-
full_url = "#{endpoint}#{url}"
|
30
|
-
deps = {config: config}
|
31
|
-
|
29
|
+
full_url = "#{@endpoint}#{url}"
|
32
30
|
RestClient::Request.expects(:new).with(url: full_url, method: :get)
|
33
31
|
HttpAction::Get.new(url, deps).send(:request)
|
34
32
|
end
|
35
33
|
|
36
34
|
def test_request_is_signed_correctly
|
37
|
-
access_id = "foobar1"
|
38
|
-
secret_key = "foobar2"
|
39
|
-
config = mock(access_id: access_id, secret_key: secret_key)
|
40
|
-
deps = {config: config}
|
41
|
-
|
42
35
|
gets = HttpAction::Get.new("", deps)
|
43
36
|
request = RestClient::Request.new(url: "http://localhost:3000", method: :get)
|
44
37
|
gets.expects(request: request)
|
45
|
-
ApiAuth.expects(:sign!).with(request, access_id, secret_key)
|
38
|
+
ApiAuth.expects(:sign!).with(request, @access_id, @secret_key)
|
46
39
|
gets.send(:signed_request)
|
47
40
|
end
|
48
41
|
end
|
@@ -1,7 +1,7 @@
|
|
1
1
|
require File.expand_path('../../test_helper', __FILE__)
|
2
2
|
|
3
3
|
module Loquor
|
4
|
-
class HttpAction::PostTest <
|
4
|
+
class HttpAction::PostTest < HttpAction::Test
|
5
5
|
def test_post_should_call_new
|
6
6
|
url = "foobar"
|
7
7
|
payload = {y: false}
|
@@ -26,10 +26,7 @@ module Loquor
|
|
26
26
|
def test_request_is_generated_correctly
|
27
27
|
url = "/foobar"
|
28
28
|
payload = {foo: true, bar: false}
|
29
|
-
|
30
|
-
config = mock(endpoint: endpoint)
|
31
|
-
full_url = "#{endpoint}#{url}"
|
32
|
-
deps = {config: config}
|
29
|
+
full_url = "#{@endpoint}#{url}"
|
33
30
|
|
34
31
|
RestClient::Request.expects(:new).with(
|
35
32
|
url: full_url,
|
@@ -42,15 +39,10 @@ module Loquor
|
|
42
39
|
end
|
43
40
|
|
44
41
|
def test_request_is_signed_correctly
|
45
|
-
access_id = "foobar1"
|
46
|
-
secret_key = "foobar2"
|
47
|
-
config = mock(access_id: access_id, secret_key: secret_key)
|
48
|
-
deps = {config: config}
|
49
|
-
|
50
42
|
posts = HttpAction::Post.new("", {}, deps)
|
51
43
|
request = RestClient::Request.new(url: "http://localhost:3000", method: :post)
|
52
44
|
posts.expects(request: request)
|
53
|
-
ApiAuth.expects(:sign!).with(request, access_id, secret_key)
|
45
|
+
ApiAuth.expects(:sign!).with(request, @access_id, @secret_key)
|
54
46
|
posts.send(:signed_request)
|
55
47
|
end
|
56
48
|
|
@@ -6,10 +6,16 @@ module Loquor
|
|
6
6
|
{
|
7
7
|
MediaFile: "/media_files",
|
8
8
|
User: "/users"
|
9
|
+
GroupDiscussion: "/group_discussions"
|
10
|
+
GroupDiscussionPost: "/group_discussion_posts"
|
9
11
|
}.each do |klass, path|
|
10
12
|
define_method "test_#{klass}_set_up_correctly" do
|
11
13
|
assert Loquor.const_defined?(klass)
|
12
14
|
end
|
15
|
+
|
16
|
+
define_method "test_#{klass}_stores_path_up_correctly" do
|
17
|
+
assert_equal path, Loquor.const_get(klass).path
|
18
|
+
end
|
13
19
|
end
|
14
20
|
end
|
15
21
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: loquor
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jeremy Walker
|
@@ -123,6 +123,7 @@ files:
|
|
123
123
|
- Rakefile
|
124
124
|
- lib/loquor.rb
|
125
125
|
- lib/loquor/api_call.rb
|
126
|
+
- lib/loquor/api_calls/create.rb
|
126
127
|
- lib/loquor/api_calls/index.rb
|
127
128
|
- lib/loquor/api_calls/show.rb
|
128
129
|
- lib/loquor/client.rb
|
@@ -130,7 +131,6 @@ files:
|
|
130
131
|
- lib/loquor/http_action.rb
|
131
132
|
- lib/loquor/http_actions/get.rb
|
132
133
|
- lib/loquor/http_actions/post.rb
|
133
|
-
- lib/loquor/path_builder.rb
|
134
134
|
- lib/loquor/representation.rb
|
135
135
|
- lib/loquor/representations.rb
|
136
136
|
- lib/loquor/version.rb
|
@@ -138,9 +138,9 @@ files:
|
|
138
138
|
- test/api_calls/index_test.rb
|
139
139
|
- test/client_test.rb
|
140
140
|
- test/configuration_test.rb
|
141
|
+
- test/http_action_test.rb
|
141
142
|
- test/http_actions/get_test.rb
|
142
143
|
- test/http_actions/post_test.rb
|
143
|
-
- test/path_builder_test.rb
|
144
144
|
- test/representation_test.rb
|
145
145
|
- test/representations_test.rb
|
146
146
|
- test/test_helper.rb
|
@@ -172,9 +172,9 @@ test_files:
|
|
172
172
|
- test/api_calls/index_test.rb
|
173
173
|
- test/client_test.rb
|
174
174
|
- test/configuration_test.rb
|
175
|
+
- test/http_action_test.rb
|
175
176
|
- test/http_actions/get_test.rb
|
176
177
|
- test/http_actions/post_test.rb
|
177
|
-
- test/path_builder_test.rb
|
178
178
|
- test/representation_test.rb
|
179
179
|
- test/representations_test.rb
|
180
180
|
- test/test_helper.rb
|
data/lib/loquor/path_builder.rb
DELETED
@@ -1,44 +0,0 @@
|
|
1
|
-
module Loquor
|
2
|
-
|
3
|
-
class MissingUrlComponentError < LoquorError
|
4
|
-
def initialize(url_component)
|
5
|
-
@url_component = url_component
|
6
|
-
end
|
7
|
-
|
8
|
-
def message
|
9
|
-
"#{url_component} has not been set. Use Object.for_#{url_component}"
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
module PathBuilder
|
14
|
-
PATH_PART_REGEX = /:[a-z0-9_]+/
|
15
|
-
|
16
|
-
def setup_path_builder(path)
|
17
|
-
path.split('/').each do |path_part|
|
18
|
-
next unless path_part =~ PATH_PART_REGEX
|
19
|
-
path_part = path_part[1..-1]
|
20
|
-
method_name = "for_#{path_part}"
|
21
|
-
|
22
|
-
self.class.send :define_method, method_name do |id|
|
23
|
-
@path_parts ||= {}
|
24
|
-
@path_parts[path_part.to_sym] = id
|
25
|
-
self
|
26
|
-
end
|
27
|
-
|
28
|
-
self.class.class_eval <<-EOS
|
29
|
-
def self.#{method_name}(*args)
|
30
|
-
new.#{method_name}(*args)
|
31
|
-
end
|
32
|
-
EOS
|
33
|
-
end
|
34
|
-
|
35
|
-
self.class.send :define_method, :build_path do
|
36
|
-
path.gsub(PATH_PART_REGEX) do |path_part|
|
37
|
-
path_part = path_part[1..-1].to_sym
|
38
|
-
@path_parts ||= {}
|
39
|
-
@path_parts.fetch(path_part) { raise MissingUrlComponentError.new(path_part) }
|
40
|
-
end
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|
44
|
-
end
|
data/test/path_builder_test.rb
DELETED
@@ -1,46 +0,0 @@
|
|
1
|
-
require File.expand_path('../test_helper', __FILE__)
|
2
|
-
|
3
|
-
module Loquor
|
4
|
-
class PathBuilderTest < Minitest::Test
|
5
|
-
class SimplePathRepresentation
|
6
|
-
include PathBuilder
|
7
|
-
|
8
|
-
def initialize
|
9
|
-
setup_path_builder("/foobar123")
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
class ComplexPathRepresentation
|
14
|
-
include PathBuilder
|
15
|
-
|
16
|
-
def initialize
|
17
|
-
setup_path_builder("/groups/:group_id/discussions")
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
def test_find_should_get_correct_path_with_simple_path
|
22
|
-
id = 8
|
23
|
-
assert_equal "/foobar123", SimplePathRepresentation.new.send(:build_path)
|
24
|
-
end
|
25
|
-
|
26
|
-
def test_path_part_methods_are_created
|
27
|
-
rep = ComplexPathRepresentation.new
|
28
|
-
assert rep.respond_to?(:for_group_id)
|
29
|
-
end
|
30
|
-
|
31
|
-
def test_find_should_get_correct_path_with_complex_path
|
32
|
-
group_id = 5
|
33
|
-
rep = ComplexPathRepresentation.new
|
34
|
-
rep.for_group_id(5)
|
35
|
-
assert_equal "/groups/#{group_id}/discussions", rep.send(:build_path)
|
36
|
-
end
|
37
|
-
|
38
|
-
def test_find_should_get_raise_exception_without_path_parts
|
39
|
-
rep = ComplexPathRepresentation.new
|
40
|
-
assert_raises(Loquor::MissingUrlComponentError) do
|
41
|
-
rep.send :build_path
|
42
|
-
end
|
43
|
-
end
|
44
|
-
end
|
45
|
-
end
|
46
|
-
|