restclient_api_base 0.0.1 → 0.0.2
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 +4 -4
- data/lib/concern.rb +142 -0
- data/lib/restclient_api_base/version.rb +1 -1
- data/lib/restclient_api_base.rb +7 -1
- data/test/test_helper.rb +1 -1
- data/test/test_restclient_api_base.rb +8 -5
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d0e7611148ac41483a29424a93722a812e6145f8
|
4
|
+
data.tar.gz: 67e70e6fbcfe73146b35638a26a5aaa42bacbb9c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ec04065543d39890702faf28564ade8cf3d8a4dc922af9d4ad20766979897103f35eaa0147b0dc362d0ba09d055b0f579f68a8cf820356f9e2839ec5162e52d4
|
7
|
+
data.tar.gz: 4b655321c95e2796e37ab18a14024821039e8cd0d46213ce649be9811cbf3ec1fe48849e4061a8d2d9acfb84944c7c1b2dd264b6e8f586aa94c2c8d190cbd283
|
data/lib/concern.rb
ADDED
@@ -0,0 +1,142 @@
|
|
1
|
+
module ActiveSupport
|
2
|
+
# A typical module looks like this:
|
3
|
+
#
|
4
|
+
# module M
|
5
|
+
# def self.included(base)
|
6
|
+
# base.extend ClassMethods
|
7
|
+
# base.class_eval do
|
8
|
+
# scope :disabled, -> { where(disabled: true) }
|
9
|
+
# end
|
10
|
+
# end
|
11
|
+
#
|
12
|
+
# module ClassMethods
|
13
|
+
# ...
|
14
|
+
# end
|
15
|
+
# end
|
16
|
+
#
|
17
|
+
# By using <tt>ActiveSupport::Concern</tt> the above module could instead be
|
18
|
+
# written as:
|
19
|
+
#
|
20
|
+
# require 'active_support/concern'
|
21
|
+
#
|
22
|
+
# module M
|
23
|
+
# extend ActiveSupport::Concern
|
24
|
+
#
|
25
|
+
# included do
|
26
|
+
# scope :disabled, -> { where(disabled: true) }
|
27
|
+
# end
|
28
|
+
#
|
29
|
+
# class_methods do
|
30
|
+
# ...
|
31
|
+
# end
|
32
|
+
# end
|
33
|
+
#
|
34
|
+
# Moreover, it gracefully handles module dependencies. Given a +Foo+ module
|
35
|
+
# and a +Bar+ module which depends on the former, we would typically write the
|
36
|
+
# following:
|
37
|
+
#
|
38
|
+
# module Foo
|
39
|
+
# def self.included(base)
|
40
|
+
# base.class_eval do
|
41
|
+
# def self.method_injected_by_foo
|
42
|
+
# ...
|
43
|
+
# end
|
44
|
+
# end
|
45
|
+
# end
|
46
|
+
# end
|
47
|
+
#
|
48
|
+
# module Bar
|
49
|
+
# def self.included(base)
|
50
|
+
# base.method_injected_by_foo
|
51
|
+
# end
|
52
|
+
# end
|
53
|
+
#
|
54
|
+
# class Host
|
55
|
+
# include Foo # We need to include this dependency for Bar
|
56
|
+
# include Bar # Bar is the module that Host really needs
|
57
|
+
# end
|
58
|
+
#
|
59
|
+
# But why should +Host+ care about +Bar+'s dependencies, namely +Foo+? We
|
60
|
+
# could try to hide these from +Host+ directly including +Foo+ in +Bar+:
|
61
|
+
#
|
62
|
+
# module Bar
|
63
|
+
# include Foo
|
64
|
+
# def self.included(base)
|
65
|
+
# base.method_injected_by_foo
|
66
|
+
# end
|
67
|
+
# end
|
68
|
+
#
|
69
|
+
# class Host
|
70
|
+
# include Bar
|
71
|
+
# end
|
72
|
+
#
|
73
|
+
# Unfortunately this won't work, since when +Foo+ is included, its <tt>base</tt>
|
74
|
+
# is the +Bar+ module, not the +Host+ class. With <tt>ActiveSupport::Concern</tt>,
|
75
|
+
# module dependencies are properly resolved:
|
76
|
+
#
|
77
|
+
# require 'active_support/concern'
|
78
|
+
#
|
79
|
+
# module Foo
|
80
|
+
# extend ActiveSupport::Concern
|
81
|
+
# included do
|
82
|
+
# def self.method_injected_by_foo
|
83
|
+
# ...
|
84
|
+
# end
|
85
|
+
# end
|
86
|
+
# end
|
87
|
+
#
|
88
|
+
# module Bar
|
89
|
+
# extend ActiveSupport::Concern
|
90
|
+
# include Foo
|
91
|
+
#
|
92
|
+
# included do
|
93
|
+
# self.method_injected_by_foo
|
94
|
+
# end
|
95
|
+
# end
|
96
|
+
#
|
97
|
+
# class Host
|
98
|
+
# include Bar # works, Bar takes care now of its dependencies
|
99
|
+
# end
|
100
|
+
module Concern
|
101
|
+
class MultipleIncludedBlocks < StandardError #:nodoc:
|
102
|
+
def initialize
|
103
|
+
super "Cannot define multiple 'included' blocks for a Concern"
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
def self.extended(base) #:nodoc:
|
108
|
+
base.instance_variable_set(:@_dependencies, [])
|
109
|
+
end
|
110
|
+
|
111
|
+
def append_features(base)
|
112
|
+
if base.instance_variable_defined?(:@_dependencies)
|
113
|
+
base.instance_variable_get(:@_dependencies) << self
|
114
|
+
return false
|
115
|
+
else
|
116
|
+
return false if base < self
|
117
|
+
@_dependencies.each { |dep| base.send(:include, dep) }
|
118
|
+
super
|
119
|
+
base.extend const_get(:ClassMethods) if const_defined?(:ClassMethods)
|
120
|
+
base.class_eval(&@_included_block) if instance_variable_defined?(:@_included_block)
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
def included(base = nil, &block)
|
125
|
+
if base.nil?
|
126
|
+
raise MultipleIncludedBlocks if instance_variable_defined?(:@_included_block)
|
127
|
+
|
128
|
+
@_included_block = block
|
129
|
+
else
|
130
|
+
super
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
def class_methods(&class_methods_module_definition)
|
135
|
+
mod = const_defined?(:ClassMethods) ?
|
136
|
+
const_get(:ClassMethods) :
|
137
|
+
const_set(:ClassMethods, Module.new)
|
138
|
+
|
139
|
+
mod.module_eval(&class_methods_module_definition)
|
140
|
+
end
|
141
|
+
end
|
142
|
+
end
|
data/lib/restclient_api_base.rb
CHANGED
@@ -1,7 +1,13 @@
|
|
1
1
|
require "restclient_api_base/version"
|
2
2
|
require "json"
|
3
3
|
require "rest-client"
|
4
|
-
|
4
|
+
|
5
|
+
# For other framework except Rails
|
6
|
+
begin
|
7
|
+
require "active_support/concern"
|
8
|
+
rescue LoadError => e
|
9
|
+
require "./lib/concern"
|
10
|
+
end
|
5
11
|
|
6
12
|
module RestclientApiBase
|
7
13
|
extend ActiveSupport::Concern
|
data/test/test_helper.rb
CHANGED
@@ -3,15 +3,18 @@ require File.dirname(__FILE__) + '/test_helper.rb'
|
|
3
3
|
class TestRestclientApiBase < Minitest::Test
|
4
4
|
|
5
5
|
def setup
|
6
|
+
@list_url = '/gists'
|
6
7
|
end
|
7
8
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
module Client
|
9
|
+
module TestClient
|
10
|
+
include RestclientApiBase
|
12
11
|
|
12
|
+
self.base_url = 'https://api.github.com'
|
13
|
+
self.private_params = {}
|
13
14
|
end
|
14
15
|
|
15
|
-
def
|
16
|
+
def test_get
|
17
|
+
list = TestClient.get(@list_url)
|
18
|
+
assert_equal true, JSON.parse(list).is_a?(Array)
|
16
19
|
end
|
17
20
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: restclient_api_base
|
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
|
- Spirit
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-08-
|
11
|
+
date: 2014-08-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rest-client
|
@@ -64,6 +64,7 @@ files:
|
|
64
64
|
- LICENSE.txt
|
65
65
|
- README.md
|
66
66
|
- Rakefile
|
67
|
+
- lib/concern.rb
|
67
68
|
- lib/restclient_api_base.rb
|
68
69
|
- lib/restclient_api_base/version.rb
|
69
70
|
- restclinet_api_base.gemspec
|