Class: Some::API
Constant Summary collapse
- API_REGEX =
/^[a-zA-Z0-9_]+[!]?$/
Instance Method Summary collapse
-
#!(options = {}) ⇒ Object
‘calls’ the API request (or makes the stub, if stubbed).
-
#<<(obj) ⇒ Object
sort of an alias for ‘posting’ (or whatever) an object just syntactic sugar for obj really I would have used ‘=’ but that would return the object you posted! >.<.
-
#>>(options) ⇒ Object
seriously this could be alias_method :>>, :!.
-
#[](thing) ⇒ Object
chains ‘thing’ onto URL path.
-
#initialize(options = {}) ⇒ API
constructor
A new instance of API.
-
#method_missing(meth, *args, &block) ⇒ Object
this is where the fun begins…
- #respond_to_missing?(method) ⇒ Boolean
-
#stub ⇒ Object
use in the call chain to flag this request as a stub used in testing for setting up API-call stubs.
Constructor Details
#initialize(options = {}) ⇒ API
Returns a new instance of API.
12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/someapi.rb', line 12 def initialize ={} @method = [:method] @path = [:path] # stubbed is a flag set when you're stubbing the API call # used in testing # just call 'stub' in the call chain before the http_method method @stubbed = [:stubbed] @options = .delete_if do |k| [:method, :path, :stubbed].include? k end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args, &block) ⇒ Object
this is where the fun begins…
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/someapi.rb', line 81 def method_missing meth, *args, &block meth_s = meth.to_s if @method && meth_s =~ API_REGEX if meth_s.end_with?('!') # `foo! bar' is syntactic sugar for `foo.! bar' self[meth_s[0...-1]].!(args[0] || {}) else # chain the method name onto URL path self[meth_s] end else super end end |
Instance Method Details
#!(options = {}) ⇒ Object
‘calls’ the API request (or makes the stub, if stubbed)
62 63 64 65 66 67 68 69 70 71 |
# File 'lib/someapi.rb', line 62 def ! = {} = merge_headers_and_queries unless @stubbed self.class.send(@method, @path || '/', ) else uri = "#{self.class.base_uri}#{@path}" stub_request(@method.to_sym, uri.to_s).with end end |
#<<(obj) ⇒ Object
sort of an alias for ‘posting’ (or whatever) an object just syntactic sugar for obj really I would have used ‘=’ but that would return the object you posted! >.<
51 52 53 |
# File 'lib/someapi.rb', line 51 def << obj self.! body: obj end |
#>>(options) ⇒ Object
seriously this could be alias_method :>>, :!
56 57 58 |
# File 'lib/someapi.rb', line 56 def >> self.! end |
#[](thing) ⇒ Object
chains ‘thing’ onto URL path
74 75 76 77 78 |
# File 'lib/someapi.rb', line 74 def [] thing make_new method: @method, path: "#{@path || ''}/#{thing}", stubbed: @stubbed end |
#respond_to_missing?(method) ⇒ Boolean
98 99 100 |
# File 'lib/someapi.rb', line 98 def respond_to_missing? method @method && method.to_s =~ API_REGEX end |
#stub ⇒ Object
use in the call chain to flag this request as a stub used in testing for setting up API-call stubs
40 41 42 43 44 45 46 |
# File 'lib/someapi.rb', line 40 def stub unless @method make_new method: @method, path: @path, stubbed: true else self['stub'] end end |