module SFKB::REST

Some REST helpers

Public Instance Methods

apply_endpoint(obj, k, v) click to toggle source

applies an endpoint to obj, named k, which fetches v and makes it an endpoint if it looks like a URL

# File lib/sfkb/rest.rb, line 53
def apply_endpoint(obj, k, v)
  α = -> { endpoint(get(v).body) }
  β = -> { v }
  λ = url?(v) ? -> { α.call } : -> { β.call }
  obj.define_singleton_method(k, &λ) if url?(v)
  obj
end
endpoint(map) click to toggle source

endpoint takes a map, and for eack key/value pair, adds a singleton method to the map which will fetch that resource (if it looks like a URL).

# File lib/sfkb/rest.rb, line 46
def endpoint(map)
  map.each { |k, v| apply_endpoint(map, k, v) }
  map
end
index() click to toggle source

The REST API starts here

# File lib/sfkb/rest.rb, line 39
def index
  endpoint(get(services_url).body)
end
services_url() click to toggle source

The starting URL

# File lib/sfkb/rest.rb, line 34
def services_url
  @@url_pattern % options[:api_version]
end
url(path, params = {}) click to toggle source

Converts a path and params to a Salesforce-suitable URL.

# File lib/sfkb/rest.rb, line 26
def url(path, params = {})
  params = params.inject({}, &@@stringify)
  path = path.gsub(@@placeholder) { params.delete($1, &@@required) }
  params = params.inject('', &@@parameterize)
  [path, params].reject(&:nil?).reject(&:empty?).join('?')
end
url?(string) click to toggle source

Identifies a valid URL for this REST instance

# File lib/sfkb/rest.rb, line 62
def url?(string)
  return false unless string.to_s =~ url_pattern
  return false if     string.to_s =~ @@placeholder
  true
end
url_pattern() click to toggle source

Identifies a valid URL for this REST instance

# File lib/sfkb/rest.rb, line 69
def url_pattern
  %r{#{services_url}}
end