module PlatformLib::ServiceBase
Public: Useful methods for working with thePlatform’s This module was intended to be mixed in
Examples:
class MyClass include PlatformLib::ServiceBase ... ... end
Protected Instance Methods
get_entries(end_point, params = {}, &block)
click to toggle source
Protected: Performs a get request and returns the “entries” array
end_point - the https? end point to connect to params - an optional hash of parameter values (query string) block - an optional block to be called for each returned entry
Examples:
arr = get_entries("http://www.somedomain.com/service", { uid: 10 }) get_entries("http://www.domain.com") do |single_item| # do something with the item end
# File lib/platform_lib/service_base.rb, line 33 def get_entries(end_point, params = {}, &block) ensure_auth_param(params) uri = URI.parse("#{end_point}?#{URI.encode_www_form(params)}") raw = WebHelper::get(uri, token: @auth_token) items = JSON.parse(raw)["entries"].map { |item| Hashie::Mash.new(item) } if block.nil? items else items.each { |item| block.call(item) } end end
post_data(end_point, params = {}, data)
click to toggle source
# File lib/platform_lib/service_base.rb, line 68 def post_data(end_point, params = {}, data) ensure_auth_param(params) uri = URI.parse("#{end_point}?#{URI.encode_www_form(params)}") puts uri.to_s body = "#{JSON.generate(data)}" puts body WebHelper::post(uri, body) end
put_entries(end_point, params = {}, entries)
click to toggle source
Protected: Performs a put request, updating the entries
end_point - the https? end point to connect to params - an optional hash of parameter values (query string) entries - the array of entries to be added to the request body
Examples:
arr = get_entries("http://www.somedomain.com/service", { uid: 10 }) put_entries(MY_END_POINT, nil, arr)
# File lib/platform_lib/service_base.rb, line 58 def put_entries(end_point, params = {}, entries) ensure_auth_param(params) uri = URI.parse("#{end_point}?#{URI.encode_www_form(params)}") puts uri.to_s body = "{ \"entries\": #{JSON.generate(entries)} }" WebHelper::put(uri, body) end
Private Instance Methods
ensure_auth_param(params)
click to toggle source
# File lib/platform_lib/service_base.rb, line 83 def ensure_auth_param(params) # ensure the token parameter is there params[:token] = @auth_token if not params.has_key?(:token) end