Module: Recras

Included in:
Booking, Combination, CombinationItem, ContactForm, ContactFormField, Invoice, Itinerary, Payment, PaymentMethod, Person
Defined in:
lib/recras.rb,
lib/recras/api.rb,
lib/recras/client.rb,
lib/recras/person.rb,
lib/recras/booking.rb,
lib/recras/invoice.rb,
lib/recras/payment.rb,
lib/recras/product.rb,
lib/recras/version.rb,
lib/recras/itinerary.rb,
lib/recras/combination.rb,
lib/recras/contact_form.rb,
lib/recras/payment_method.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, Invoice, Itinerary, Payment, PaymentMethod, Person

Constant Summary collapse

API_VERSION =
2
VERSION =
"0.1.7"

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



115
116
117
118
119
120
121
122
123
124
125
126
127
128
# 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_mappingsObject

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



33
34
35
# 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) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# 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

.urlObject



37
38
39
# File 'lib/recras.rb', line 37

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

.versionObject

returns the version number



27
28
29
# File 'lib/recras.rb', line 27

def self.version
  VERSION
end

Instance Method Details

#new_from_json(json) ⇒ Object

initialize a new Person from given JSON.



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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# 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