Module: Recras

Included in:
Booking, Combination, CombinationItem, ContactForm, ContactFormField, Person
Defined in:
lib/recras.rb,
lib/recras/api.rb,
lib/recras/client.rb,
lib/recras/person.rb,
lib/recras/booking.rb,
lib/recras/version.rb,
lib/recras/combination.rb,
lib/recras/contact_form.rb,
lib/recras/combination_item.rb,
lib/recras/contact_form_field.rb

Defined Under Namespace

Modules: Config Classes: Api, Booking, Client, Combination, CombinationItem, ContactForm, ContactFormField, Person

Constant Summary collapse

API_VERSION =
2
VERSION =
"0.1.0"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.make_request(endpoint, body: {}, http_method: :get, client: nil) ⇒ Object

communicates with the server and returns either:

  • an object (Person, Booking)

  • an error

  • a json object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/recras.rb', line 101

def self.make_request(endpoint, body: {}, http_method: :get, client: nil)
  url = "#{Recras.url}/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_mappingsObject

this method maps the recras API objects to the names used by this gem



29
30
31
# File 'lib/recras.rb', line 29

def self.object_mappings
  [["personeel", Person], ["arrangement", Combination], ["contactformulier", ContactForm], ["velden", ContactFormField], ["booking", Booking]]
end

.parse_json(json: nil, endpoint: nil, client: nil) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/recras.rb', line 75

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

.urlObject



33
34
35
# File 'lib/recras.rb', line 33

def self.url
  "https://demo.recras.nl"
end

.versionObject

returns the version number



23
24
25
# File 'lib/recras.rb', line 23

def self.version
  VERSION
end

Instance Method Details

#new_from_json(json) ⇒ Object

initialize a new Person from given JSON.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/recras.rb', line 38

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)
         # puts data.inspect
         # raise ERR
        else
          data = n.split("@@").first
        end
        params[n.split("@@").first] = data
      else
        params[n] = json[o.to_s]
      end
      
    # 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
  self.new(params)
end