class Some::API

Constants

API_REGEX

Public Class Methods

new(options={}) click to toggle source
# File lib/someapi.rb, line 12
def initialize options={}
  @method = options[:method]
  @path = options[: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 = options[:stubbed]

  @options = options.delete_if do |k|
    [:method, :path, :stubbed].include? k
  end
end

Public Instance Methods

!(options = {}) click to toggle source

'calls' the API request (or makes the stub, if stubbed)

# File lib/someapi.rb, line 62
def ! options = {}
  merged_options = merge_headers_and_queries options
  unless @stubbed
    self.class.send(@method, @path || '/', merged_options)
  else
    uri = "#{self.class.base_uri}#{@path}"

    stub_request(@method.to_sym, uri.to_s).with merged_options
  end
end
<<(obj) click to toggle source

sort of an alias for 'posting' (or whatever) an object just syntactic sugar for {body: obj} really I would have used '=' but that would return the object you posted! >.<

# File lib/someapi.rb, line 51
def << obj
  self.! body: obj
end
>>(options) click to toggle source

seriously this could be alias_method :>>, :!

# File lib/someapi.rb, line 56
def >> options
  self.! options
end
[](thing) click to toggle source

chains 'thing' onto URL path

# File lib/someapi.rb, line 74
def [] thing
  make_new method: @method,
           path: "#{@path || ''}/#{thing}",
           stubbed: @stubbed
end
method_missing(meth, *args, &block) click to toggle source

this is where the fun begins…

Calls superclass method
# 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
respond_to_missing?(method) click to toggle source
# File lib/someapi.rb, line 98
def respond_to_missing? method
  @method && method.to_s =~ API_REGEX
end
stub() click to toggle source

use in the call chain to flag this request as a stub used in testing for setting up API-call stubs

# File lib/someapi.rb, line 40
def stub
  unless @method
    make_new method: @method, path: @path, stubbed: true
  else
    self['stub']
  end
end

Private Instance Methods

make_new(opts={}) click to toggle source
# File lib/someapi.rb, line 124
def make_new opts={}
  self.class.new @options.merge(opts)
end
merge_headers(options) click to toggle source
# File lib/someapi.rb, line 108
def merge_headers options
  options.merge({
    headers: (self.class.headers || {}).
      merge((@options[:headers] || {}).
      merge(options[:headers] || {}))
  })
end
merge_headers_and_queries(options) click to toggle source
# File lib/someapi.rb, line 104
def merge_headers_and_queries options
  merge_headers merge_queries options
end
merge_queries(options) click to toggle source
# File lib/someapi.rb, line 116
def merge_queries options
  options.merge({
    query: (self.class.default_options[:default_params] || {}).
      merge((@options[:query] || {}).
      merge(options[:query]|| {}))
  })
end