Sha256: 032118df1b0c9296d9c2c9a96e31e4c955ca2d2e403134af11212258646a463c
Contents?: true
Size: 1.33 KB
Versions: 2
Compression:
Stored size: 1.33 KB
Contents
require "dendreo/version" require "json" require 'net/http' require "rest-client" module Dendreo class API attr_accessor :url attr_accessor :api_key def initialize(url, api_key) @url = url #exemple = https://pro.dendreo.com/my_company/api @api_key = api_key end def method_missing(method_name, *args) method_name_string = method_name.to_s request_method = args.first[:method] datas = args.any? ? args.first[:datas] : {} base_url = "#{@url}/#{method_name_string}.php?key=#{@api_key}" case request_method when "get" send_it(:get, "#{base_url}#{format_args_to_url(datas)}") when "post" send_it(:post, "#{base_url}", datas) when "delete" send_it(:delete, "#{base_url}#{format_args_to_url(datas)}") else raise "Méthode inconnue '#{request_method}' !" end end private def send_it(http_method, url, options = {}) hsh = {url: url, method: http_method } hsh.merge!(payload: options) if http_method == :post response_json(RestClient::Request.execute(hsh)) end def format_args_to_url(args = {}) args.any? ? args.map{|k, v| k == args.keys.first ? "&#{k}=#{v}" : "#{k}=#{v}" }.join("&") : "" end def response_json(result) JSON.parse(result == "" ? "[{}]" : result) end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
Dendreo-1.0.3 | lib/dendreo.rb |
Dendreo-1.0.2 | lib/dendreo.rb |