Module: Osm

Defined in:
lib/osm.rb,
lib/osm.rb,
lib/osm/api.rb,
lib/osm/sms.rb,
lib/osm/term.rb,
lib/osm/badge.rb,
lib/osm/event.rb,
lib/osm/model.rb,
lib/osm/badges.rb,
lib/osm/budget.rb,
lib/osm/member.rb,
lib/osm/giftaid.rb,
lib/osm/invoice.rb,
lib/osm/meeting.rb,
lib/osm/section.rb,
lib/osm/activity.rb,
lib/osm/grouping.rb,
lib/osm/register.rb,
lib/osm/api_access.rb,
lib/osm/flexi_record.rb

Defined Under Namespace

Classes: Activity, ActivityBadge, Api, ApiAccess, ArgumentIsInvalid, Badge, Badges, Budget, ChallengeBadge, ConnectionError, CoreBadge, Error, Event, FlexiRecord, Forbidden, GiftAid, Grouping, Invoice, Meeting, Member, Model, NoActiveRoles, ObjectIsInvalid, Register, Section, Sms, StagedBadge, Term

Class Method Summary collapse

Class Method Details

.configure(options) ⇒ Object

Configure the options used by classes in the module

Parameters:

  • options (Hash)
  • options[:api] (Hash)

    a customizable set of options

  • options[:api][:osm] (Hash)

    a customizable set of options

  • options[:api][:ogm] (Hash)

    a customizable set of options

  • options[:cache] (Hash)

    a customizable set of options

Options Hash (options):

  • :api (Hash)

    Default options for accessing the API

  • :cache_config (Hash) — default: optional

    How classes in the module will cache data. Whilst this is optional you should remember that caching is required to use the OSM API.

Returns:

  • nil



84
85
86
87
88
# File 'lib/osm.rb', line 84

def self.configure(options)
  Osm::Model.configure(options[:cache])
  Osm::Api.configure(options[:api])
  nil
end

.epoch_date?(date) ⇒ Boolean

Returns:

  • (Boolean)


174
175
176
# File 'lib/osm.rb', line 174

def self.epoch_date?(date)
  [OSM_EPOCH, OSM_EPOCH_HUMAN].include?(date)
end

.inspect_instance(instance, options = {}) ⇒ Object



178
179
180
181
182
183
184
185
186
# File 'lib/osm.rb', line 178

def self.inspect_instance(instance, options={})
  replace_with = options[:replace_with] || {}

  values = instance.attributes.sort.map{ |(k,v)|
    (replace_with.keys.include?(k) && !v.nil?) ? "#{k}.#{replace_with[k]}: #{v.try(replace_with[k])}" : "#{k}: #{v.inspect}"
  }

  return "#<#{instance.class.name} #{values.join(', ')} >"
end

.make_array_of_symbols(array) ⇒ Object



92
93
94
95
96
# File 'lib/osm.rb', line 92

def self.make_array_of_symbols(array)
  array.each_with_index do |item, index|
    array[index] = item.to_sym
  end
end

.make_datetime(date, time, options = {}) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/osm.rb', line 98

def self.make_datetime(date, time, options={})
  date = nil if date.nil? || date.empty? || (!options[:ignore_epoch] && epoch_date?(date))
  time = nil if time.nil? || time.empty?
  if (!date.nil? && !time.nil?)
    begin
      return DateTime.strptime((date + ' ' + time), OSM_DATETIME_FORMAT)
    rescue ArgumentError
      return nil
    end
  elsif !date.nil?
    begin
      return DateTime.strptime(date, (date.include?('-') ? OSM_DATE_FORMAT : OSM_DATE_FORMAT_HUMAN))
    rescue ArgumentError
      return nil
    end
  else
    return nil
  end
end

.make_permissions_hash(permissions) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/osm.rb', line 156

def self.make_permissions_hash(permissions)
  return {} unless permissions.is_a?(Hash)

  permissions_map = {
    10  => [:read],
    20  => [:read, :write],
    100 => [:read, :write, :administer],
  }

  return permissions.inject({}) do |new_hash, (key, value)|
    if ["badge", "member", "user", "register", "contact", "programme","events", "flexi", "finance", "quartermaster"].include?(key)
      # This is a permission we care about
      new_hash[key.to_sym] = permissions_map[value.to_i]
    end
    new_hash
  end
end

.parse_date(date, options = {}) ⇒ Object



128
129
130
131
132
133
134
135
# File 'lib/osm.rb', line 128

def self.parse_date(date, options={})
  return nil if date.nil? || date.empty? || (!options[:ignore_epoch] && epoch_date?(date))
  begin
    return Date.strptime(date, (date.include?('-') ? OSM_DATE_FORMAT : OSM_DATE_FORMAT_HUMAN))
  rescue ArgumentError
    return nil
  end
end

.parse_datetime(date_time) ⇒ Object



118
119
120
121
122
123
124
125
# File 'lib/osm.rb', line 118

def self.parse_datetime(date_time)
  return nil if date_time.nil? || date_time.empty?
  begin
    return DateTime.strptime(date_time, OSM_DATETIME_FORMAT)
  rescue ArgumentError
    return nil
  end
end

.symbolize_hash(hash_in) ⇒ Object

Raises:

  • (ArgumentError)


146
147
148
149
150
151
152
153
154
# File 'lib/osm.rb', line 146

def self.symbolize_hash(hash_in)
  raise ArgumentError, 'You did not pass in a hash' unless hash_in.is_a?(Hash)

  hash_out = {}
  hash_in.each do |key, value|
    hash_out[key.to_sym] = value
  end
  hash_out
end

.to_i_or_nil(item) ⇒ Object



137
138
139
140
141
142
143
144
# File 'lib/osm.rb', line 137

def self.to_i_or_nil(item)
  return nil if item.nil?
  begin
    return item.to_i
  rescue
    return nil
  end
end