Class: Cmsso::Member

Inherits:
Object
  • Object
show all
Defined in:
app/models/cmsso/member.rb

Constant Summary collapse

@@type =
nil
@@note =
nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Member

Returns a new instance of Member.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/models/cmsso/member.rb', line 17

def initialize(args={})
  return nil unless args.class == Hash

  # convert keys to symbols
  new_args          = ActiveSupport::HashWithIndifferentAccess.new(args)

  # assign and format
  @id               = new_args[:person_id]
  @reference        = new_args[:person_reference]
  @first_name       = new_args[:first_name]
  @last_name        = new_args[:last_name]
  @email            = new_args[:email]
  @roles            = []
  @roles            = new_args[:roles].map{ |item| item.downcase.to_sym } if new_args[:roles]
  @status           = new_args[:status]
  @note             = new_args[:note]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method) ⇒ Object



35
36
37
# File 'app/models/cmsso/member.rb', line 35

def method_missing(method)
  @roles.include?(method[0..-2].to_sym)  
end

Instance Attribute Details

#emailObject

Returns the value of attribute email.



11
12
13
# File 'app/models/cmsso/member.rb', line 11

def email
  @email
end

#first_nameObject

Returns the value of attribute first_name.



9
10
11
# File 'app/models/cmsso/member.rb', line 9

def first_name
  @first_name
end

#idObject

Returns the value of attribute id.



8
9
10
# File 'app/models/cmsso/member.rb', line 8

def id
  @id
end

#last_nameObject

Returns the value of attribute last_name.



10
11
12
# File 'app/models/cmsso/member.rb', line 10

def last_name
  @last_name
end

#noteObject

Returns the value of attribute note.



15
16
17
# File 'app/models/cmsso/member.rb', line 15

def note
  @note
end

#referenceObject

Returns the value of attribute reference.



13
14
15
# File 'app/models/cmsso/member.rb', line 13

def reference
  @reference
end

#rolesObject

Returns the value of attribute roles.



12
13
14
# File 'app/models/cmsso/member.rb', line 12

def roles
  @roles
end

#statusObject

Returns the value of attribute status.



14
15
16
# File 'app/models/cmsso/member.rb', line 14

def status
  @status
end

Class Method Details

.create(headers) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'app/models/cmsso/member.rb', line 94

def self.create(headers)
  # get person reference from http header
  person_from_header = get_person_from_header(headers)
  return nil unless person_from_header
  
  # call service
  person = nil
  
  # set proxy as defined during configuration 
  ENV['http_proxy'] = Cmsso.configuration.proxy


  request = build_request(person_from_header)
  puts request

  begin
    RestClient.put Cmsso.configuration.base_uri, request, :content_type => :json do |response,request,result|
      # parse reponse and return person object
      person = Member.new(JSON.parse(response)["response"]["persons"][0])
     
      # check about invalid status
      unless person.status == 'valid'
        @@type, @@note = :role_or_person,person.note
        return nil
      end

    end
  
  # parsing error 
  rescue JSON::ParserError => e
    @@type, @@note = :parse,"Error while parsing role service response"  
    return nil

  # service specific
  rescue SocketError, Errno::ECONNREFUSED => e
    @@type, @@note = :service,"Error while calling role service: #{e.to_s}"
    return nil

  # unexpected exceptions
  rescue => e
    @@type, @@note = :unexpected,"Error while calling role service: #{e.to_s}"
    return nil
  end

  return person
end

.find(headers) ⇒ Object



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
88
89
90
91
92
# File 'app/models/cmsso/member.rb', line 51

def self.find(headers)
  # get person reference from http header
  person_from_header = get_person_from_header(headers)
  return nil unless person_from_header

  # call service
  person = nil
  
  # set proxy as defined during configuration 
  ENV['http_proxy'] = Cmsso.configuration.proxy if ENV

  begin
    RestClient.post Cmsso.configuration.base_uri + "/role", build_request(person_from_header), :content_type => :json do |response,request,result|
      # parse reponse and return person object
      person = Member.new(JSON.parse(response)["response"]["persons"][0])

      # check about invalid status
      unless person.status == 'valid'
        @@type, @@note = :role_or_person, person.note
        return nil
      end

    end

  # parsing error 
  rescue JSON::ParserError => e
    @@type, @@note = :parse,"Error while parsing role service response"  
    return nil

  # service specific
  rescue SocketError, Errno::ECONNREFUSED => e
    @@type, @@note = :service,"Error while calling role service: #{e.to_s}"
    return nil

  # unexpected exceptions
  rescue => e
    @@type, @@note = :unexpected,"Error while calling role service: #{e.to_s}"
    return nil
  end

  return person
end

.noteObject



47
48
49
# File 'app/models/cmsso/member.rb', line 47

def self.note
  @@note
end

.typeObject



43
44
45
# File 'app/models/cmsso/member.rb', line 43

def self.type
  @@type
end

Instance Method Details

#full_nameObject



39
40
41
# File 'app/models/cmsso/member.rb', line 39

def full_name
  [self.last_name,self.first_name].join(" ")
end