loquor 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,3 @@
1
+ module Loquor
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'loquor/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "loquor"
8
+ spec.version = Loquor::VERSION
9
+ spec.authors = ["Jeremy Walker"]
10
+ spec.email = ["jez.walker@gmail.com"]
11
+ spec.description = "An API dispatcher for Meducation"
12
+ spec.summary = "This library dispatches requests to Meducation"
13
+ spec.homepage = "https://www.meducation.net"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
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_dependency 'filum', '0.0.1'
22
+ spec.add_dependency 'rest-client', '1.6.7'
23
+ spec.add_dependency "api-auth", "1.0.3"
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.3"
26
+ spec.add_development_dependency "minitest", '~> 5.0.8'
27
+ spec.add_development_dependency "mocha"
28
+ spec.add_development_dependency "rake"
29
+ end
@@ -0,0 +1,72 @@
1
+ require File.expand_path('../../test_helper', __FILE__)
2
+
3
+ module Loquor
4
+ class ApiCall::IndexTest < Minitest::Test
5
+ def test_where_sets_criteria
6
+ criteria = {genre: 'Animation'}
7
+ searcher = ApiCall::Index.new('').where(criteria)
8
+ assert_equal({genre: 'Animation'}, searcher.criteria)
9
+ end
10
+
11
+ def test_where_merges_criteria
12
+ criteria1 = {genre: 'Animation'}
13
+ criteria2 = {foobar: 'Cat'}
14
+ searcher = ApiCall::Index.new('').where(criteria1).where(criteria2)
15
+ assert_equal({genre: 'Animation', foobar: 'Cat'}, searcher.criteria)
16
+ end
17
+
18
+ def test_where_overrides_criteria_with_same_key
19
+ criteria1 = {genre: 'Animation'}
20
+ criteria2 = {genre: 'Action'}
21
+ searcher = ApiCall::Index.new('').where(criteria1).where(criteria2)
22
+ assert_equal({genre: "Action"}, searcher.criteria)
23
+ end
24
+
25
+ def test_where_gets_correct_url
26
+ searcher = ApiCall::Index.new('').where(name: 'Star Wars')
27
+ assert searcher.send(:generate_url).include? "?name=Star%20Wars"
28
+ end
29
+
30
+ def test_where_works_with_array_in_a_hash
31
+ criteria = {thing: ['foo', 'bar']}
32
+ searcher = ApiCall::Index.new('').where(criteria)
33
+ assert_equal criteria, searcher.criteria
34
+ end
35
+
36
+ def test_that_iterating_calls_results
37
+ searcher = ApiCall::Index.new('').where(name: "star_wars")
38
+ searcher.expects(results: [])
39
+ searcher.each { }
40
+ end
41
+
42
+ def test_that_iterating_calls_each
43
+ Loquor.expects(:get).returns([{id: 8, name: "Star Wars"}])
44
+ searcher = ApiCall::Index.new('').where(name: "star_wars")
45
+ searcher.send(:results).expects(:each)
46
+ searcher.each { }
47
+ end
48
+
49
+ def test_that_select_calls_each
50
+ Loquor.expects(:get).returns([{id: 8, name: "Star Wars"}])
51
+ searcher = ApiCall::Index.new('').where(name: "star_wars")
52
+ searcher.send(:results).expects(:select)
53
+ searcher.select { }
54
+ end
55
+
56
+ def test_search_should_set_results
57
+ expected_results = [{id: 8, name: "Star Wars"}]
58
+ Loquor.expects(:get).returns(expected_results)
59
+
60
+ searcher = ApiCall::Index.new('').where(name: "star_wars")
61
+ searcher.to_a
62
+ assert_equal expected_results, searcher.instance_variable_get("@results")
63
+ end
64
+
65
+ def test_search_should_create_a_results_object
66
+ Loquor.expects(:get).returns([{id: 8, name: "Star Wars"}])
67
+ searcher = ApiCall::Index.new('').where(name: "star_wars")
68
+ searcher.to_a
69
+ assert Array, searcher.instance_variable_get("@results").class
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,29 @@
1
+ require File.expand_path('../test_helper', __FILE__)
2
+
3
+ module Loquor
4
+ class ClientTest < Minitest::Test
5
+ def test_initialize_should_create_config
6
+ Configuration.expects(:new)
7
+ Client.new
8
+ end
9
+
10
+ def test_get_calls_gets
11
+ url = "foobar"
12
+
13
+ client = Client.new
14
+ deps = {config: client.config}
15
+ HttpAction::Get.expects(:get).with(url, deps)
16
+ client.get(url)
17
+ end
18
+
19
+ def test_post_calls_posts
20
+ url = "foobar"
21
+ payload = {x: true}
22
+
23
+ client = Client.new
24
+ deps = {config: client.config}
25
+ HttpAction::Post.expects(:post).with(url, payload, deps)
26
+ client.post(url, payload)
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,58 @@
1
+ require File.expand_path('../test_helper', __FILE__)
2
+
3
+ module Loquor
4
+ class ConfigurationTest < Minitest::Test
5
+
6
+ def setup
7
+ Loquor.send(:loquor).instance_variable_set("@config", Configuration.new)
8
+ end
9
+
10
+ def test_obtaining_singletion
11
+ refute Loquor.config.nil?
12
+ end
13
+
14
+ def test_block_syntax
15
+ test_key = "foobar-123-access"
16
+ Loquor.config do |config|
17
+ config.access_id = test_key
18
+ end
19
+ assert_equal test_key, Loquor.config.access_id
20
+ end
21
+
22
+ def test_access_id
23
+ access_id = "test-access-key"
24
+ Loquor.config.access_id = access_id
25
+ assert_equal access_id, Loquor.config.access_id
26
+ end
27
+
28
+ def test_secret_key
29
+ secret_key = "test-secret-key"
30
+ Loquor.config.secret_key = secret_key
31
+ assert_equal secret_key, Loquor.config.secret_key
32
+ end
33
+
34
+ def test_endpoint
35
+ endpoint = "http://localhost:3000"
36
+ Loquor.config.endpoint = endpoint
37
+ assert_equal endpoint, Loquor.config.endpoint
38
+ end
39
+
40
+ def test_missing_access_id_throws_exception
41
+ assert_raises(LoquorConfigurationError) do
42
+ Loquor.config.access_id
43
+ end
44
+ end
45
+
46
+ def test_missing_secret_key_throws_exception
47
+ assert_raises(LoquorConfigurationError) do
48
+ Loquor.config.secret_key
49
+ end
50
+ end
51
+
52
+ def test_missing_endpoint_throws_exception
53
+ assert_raises(LoquorConfigurationError) do
54
+ Loquor.config.endpoint
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,49 @@
1
+ require File.expand_path('../../test_helper', __FILE__)
2
+
3
+ module Loquor
4
+ class HttpAction::GetTest < Minitest::Test
5
+ def test_get_should_call_new
6
+ url = "foobar"
7
+ deps = {x: true}
8
+ HttpAction::Get.expects(:new).with(url, deps).returns(mock(get: nil))
9
+ HttpAction::Get.get(url, deps)
10
+ end
11
+
12
+ def test_get_should_call_get
13
+ HttpAction::Get.any_instance.expects(:get)
14
+ HttpAction::Get.get("foobar", {})
15
+ end
16
+
17
+ def test_get_parses_request
18
+ output = {'foo' => 'bar'}
19
+ json = output.to_json
20
+ gets = HttpAction::Get.new("", {})
21
+ gets.expects(signed_request: mock(execute: json))
22
+ assert_equal output, gets.get
23
+ end
24
+
25
+ def test_request_is_generated_correctly
26
+ url = "/foobar"
27
+ endpoint = "http://thefoobar.com"
28
+ config = mock(endpoint: endpoint)
29
+ full_url = "#{endpoint}#{url}"
30
+ deps = {config: config}
31
+
32
+ RestClient::Request.expects(:new).with(url: full_url, method: :get)
33
+ HttpAction::Get.new(url, deps).send(:request)
34
+ end
35
+
36
+ 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
+ gets = HttpAction::Get.new("", deps)
43
+ request = RestClient::Request.new(url: "http://localhost:3000", method: :get)
44
+ gets.expects(request: request)
45
+ ApiAuth.expects(:sign!).with(request, access_id, secret_key)
46
+ gets.send(:signed_request)
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,59 @@
1
+ require File.expand_path('../../test_helper', __FILE__)
2
+
3
+ module Loquor
4
+ class HttpAction::PostTest < Minitest::Test
5
+ def test_post_should_call_new
6
+ url = "foobar"
7
+ payload = {y: false}
8
+ deps = {x: true}
9
+ HttpAction::Post.expects(:new).with(url, payload, deps).returns(mock(post: nil))
10
+ HttpAction::Post.post(url, payload, deps)
11
+ end
12
+
13
+ def test_post_should_call_post
14
+ HttpAction::Post.any_instance.expects(:post)
15
+ HttpAction::Post.post("foobar", {}, {})
16
+ end
17
+
18
+ def test_post_parses_request
19
+ output = {'foo' => 'bar'}
20
+ json = output.to_json
21
+ posts = HttpAction::Post.new("", {}, {})
22
+ posts.expects(signed_request: mock(execute: json))
23
+ assert_equal output, posts.post
24
+ end
25
+
26
+ def test_request_is_generated_correctly
27
+ url = "/foobar"
28
+ payload = {foo: true, bar: false}
29
+ endpoint = "http://thefoobar.com"
30
+ config = mock(endpoint: endpoint)
31
+ full_url = "#{endpoint}#{url}"
32
+ deps = {config: config}
33
+
34
+ RestClient::Request.expects(:new).with(
35
+ url: full_url,
36
+ accept: :json,
37
+ payload: payload.to_json,
38
+ headers: {'Content-type' => 'application/json'},
39
+ method: :post
40
+ )
41
+ HttpAction::Post.new(url, payload, deps).send(:request)
42
+ end
43
+
44
+ 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
+ posts = HttpAction::Post.new("", {}, deps)
51
+ request = RestClient::Request.new(url: "http://localhost:3000", method: :post)
52
+ posts.expects(request: request)
53
+ ApiAuth.expects(:sign!).with(request, access_id, secret_key)
54
+ posts.send(:signed_request)
55
+ end
56
+
57
+ end
58
+ end
59
+
@@ -0,0 +1,46 @@
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
+
@@ -0,0 +1,30 @@
1
+ require File.expand_path('../test_helper', __FILE__)
2
+
3
+ module Loquor
4
+ class RepresentationTest < Minitest::Test
5
+ class FoobarRepresentation
6
+ extend Representation::ClassMethods
7
+ include Representation::InstanceMethods
8
+
9
+ def build_path
10
+ "/foobar"
11
+ end
12
+
13
+ def self.path
14
+ "/foobar"
15
+ end
16
+ end
17
+
18
+ def test_find_should_get_correct_path_with_simple_path
19
+ id = 8
20
+ Loquor.expects(:get).with("/foobar/#{id}")
21
+ FoobarRepresentation.find(id)
22
+ end
23
+
24
+ def test_where_should_get_correct_path_with_simple_path
25
+ email = "foobar"
26
+ Loquor.expects(:get).with("/foobar?email=#{email}")
27
+ FoobarRepresentation.where(email: email).to_a
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,16 @@
1
+ require File.expand_path('../test_helper', __FILE__)
2
+
3
+ module Loquor
4
+ class RepresentationTest < Minitest::Test
5
+
6
+ {
7
+ MediaFile: "/media_files",
8
+ User: "/users"
9
+ }.each do |klass, path|
10
+ define_method "test_#{klass}_set_up_correctly" do
11
+ assert Loquor.const_defined?(klass)
12
+ end
13
+ end
14
+ end
15
+ end
16
+
@@ -0,0 +1,25 @@
1
+ gem "minitest"
2
+ require "minitest/autorun"
3
+ require "minitest/pride"
4
+ require "minitest/mock"
5
+ require "mocha/setup"
6
+
7
+ lib = File.expand_path('../../lib', __FILE__)
8
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
9
+
10
+ require "loquor"
11
+
12
+ class Minitest::Test
13
+ def setup
14
+ Loquor.config do |config|
15
+ config.access_id = "Sermo"
16
+ config.secret_key = "foobar"
17
+ config.endpoint = "http://localhost:3000"
18
+
19
+ config.logger = mock()
20
+ config.logger.stubs(:debug)
21
+ config.logger.stubs(:info)
22
+ config.logger.stubs(:error)
23
+ end
24
+ end
25
+ end
metadata ADDED
@@ -0,0 +1,181 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: loquor
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Jeremy Walker
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-11-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: filum
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '='
18
+ - !ruby/object:Gem::Version
19
+ version: 0.0.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '='
25
+ - !ruby/object:Gem::Version
26
+ version: 0.0.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: rest-client
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '='
32
+ - !ruby/object:Gem::Version
33
+ version: 1.6.7
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '='
39
+ - !ruby/object:Gem::Version
40
+ version: 1.6.7
41
+ - !ruby/object:Gem::Dependency
42
+ name: api-auth
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '='
46
+ - !ruby/object:Gem::Version
47
+ version: 1.0.3
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '='
53
+ - !ruby/object:Gem::Version
54
+ version: 1.0.3
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '1.3'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '1.3'
69
+ - !ruby/object:Gem::Dependency
70
+ name: minitest
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: 5.0.8
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: 5.0.8
83
+ - !ruby/object:Gem::Dependency
84
+ name: mocha
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: rake
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
+ description: An API dispatcher for Meducation
112
+ email:
113
+ - jez.walker@gmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - .gitignore
119
+ - CONTRIBUTING.md
120
+ - Gemfile
121
+ - LICENCE.md
122
+ - README.md
123
+ - Rakefile
124
+ - lib/loquor.rb
125
+ - lib/loquor/api_call.rb
126
+ - lib/loquor/api_calls/index.rb
127
+ - lib/loquor/api_calls/show.rb
128
+ - lib/loquor/client.rb
129
+ - lib/loquor/configuration.rb
130
+ - lib/loquor/http_action.rb
131
+ - lib/loquor/http_actions/get.rb
132
+ - lib/loquor/http_actions/post.rb
133
+ - lib/loquor/path_builder.rb
134
+ - lib/loquor/representation.rb
135
+ - lib/loquor/representations.rb
136
+ - lib/loquor/version.rb
137
+ - loquor.gemspec
138
+ - test/api_calls/index_test.rb
139
+ - test/client_test.rb
140
+ - test/configuration_test.rb
141
+ - test/http_actions/get_test.rb
142
+ - test/http_actions/post_test.rb
143
+ - test/path_builder_test.rb
144
+ - test/representation_test.rb
145
+ - test/representations_test.rb
146
+ - test/test_helper.rb
147
+ homepage: https://www.meducation.net
148
+ licenses:
149
+ - MIT
150
+ metadata: {}
151
+ post_install_message:
152
+ rdoc_options: []
153
+ require_paths:
154
+ - lib
155
+ required_ruby_version: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - '>='
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ required_rubygems_version: !ruby/object:Gem::Requirement
161
+ requirements:
162
+ - - '>='
163
+ - !ruby/object:Gem::Version
164
+ version: '0'
165
+ requirements: []
166
+ rubyforge_project:
167
+ rubygems_version: 2.1.9
168
+ signing_key:
169
+ specification_version: 4
170
+ summary: This library dispatches requests to Meducation
171
+ test_files:
172
+ - test/api_calls/index_test.rb
173
+ - test/client_test.rb
174
+ - test/configuration_test.rb
175
+ - test/http_actions/get_test.rb
176
+ - test/http_actions/post_test.rb
177
+ - test/path_builder_test.rb
178
+ - test/representation_test.rb
179
+ - test/representations_test.rb
180
+ - test/test_helper.rb
181
+ has_rdoc: