module Recras
Constants
- API_VERSION
- VERSION
Public Class Methods
make_request(endpoint, body: {}, http_method: :get, client: nil)
click to toggle source
communicates with the server and returns either:
# File lib/recras.rb, line 115 def self.make_request(endpoint, body: {}, http_method: :get, client: nil) url = "#{client.host}/api#{Recras::API_VERSION}/#{endpoint}" auth = client ? {username: client.username, password: client.password} : nil if http_method && http_method.to_s == 'post' response = HTTParty.post(url, basic_auth: auth, body: body) else response = HTTParty.get(url, basic_auth: auth, body: body) end json = response.parsed_response return json end
object_mappings()
click to toggle source
this method maps the recras API objects to the names used by this gem
# File lib/recras.rb, line 33 def self.object_mappings [["betalingen", Payment],["personeel", Person], ["arrangement", Combination], ["contactformulier", ContactForm], ["velden", ContactFormField], ["booking", Booking], ["facturen", Invoice], ["betaalmethoden", PaymentMethod]] end
parse_json(json: nil, endpoint: nil, client: nil)
click to toggle source
# File lib/recras.rb, line 89 def self.parse_json(json: nil, endpoint: nil, client: nil) if json.is_a?(Hash) && json["error"] if json["message"] raise RecrasError.new(self), json["message"] else raise RecrasError.new(self), json["error"]["message"] end else Recras.object_mappings.each do |key,klass| if endpoint.match(key) # puts "Making new #{klass.name} with data: #{json}" obj = klass.new_from_json(json) if client obj.client = client end return obj end end raise RecrasError.new(self), "Unknwon object '#{endpoint}'" end end
url()
click to toggle source
# File lib/recras.rb, line 37 def self.url "https://demo.recras.nl" end
version()
click to toggle source
returns the version number
# File lib/recras.rb, line 27 def self.version VERSION end
Public Instance Methods
new_from_json(json)
click to toggle source
initialize a new Person
from given JSON.
# File lib/recras.rb, line 42 def new_from_json(json) params = {} # set each attribute based on the mapping self.attribute_mapping.each do |o,n| # if o is a string, parse it if n.is_a?(String) # check if data type is specified (using @@ symbol) if n.include?("@@") if n.split("@@").last.downcase == 'time' data = Time.new(n.split("@@").first) elsif n.split("@@").last.downcase == 'float' data = n.split("@@").first.to_f else data = n.split("@@").first end params[n.split("@@").first] = data else # puts "parsing value: #{n} => #{json[o]}" params[n] = json[o.to_s] # puts "parsed value (#{params[n]})" end # assign boolean value elsif [FalseClass, TrueClass].include?(n.class) # puts "Parsing boolean value: #{n} = #{json[o]}" params[n] = json[o] # puts "#{n} => #{o}" # else, o is a class. Call the 'parse_children' method on it else # puts "n is a #{n.class.name}" # loop through all the children (for example: 'regels' on 'arrangementen') if json[o] children = [] for item in json[o] children << n.new_from_json(item) end params[n.plural_name] = children end end end params['json'] = json self.new(params) end