Class: BigDoor::Resource

Inherits:
OpenStruct
  • Object
show all
Defined in:
lib/big_door/resource.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ Resource

Initialize new Resource object with optional Hash

Parameters:

  • hash (Hash) (defaults to: {})

    Optional fields to assign to object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/big_door/resource.rb', line 18

def initialize( hash = {}) 
    $log.debug( "Resource init with hash = #{hash.inspect}")
    default_values = {
        'pub_title'            => '',
        'pub_description'      => '',
        'end_user_title'       => '',
        'end_user_description' => '',
    }
    default_values.merge!( hash )
    $log.debug( "Resource default_values = #{default_values.inspect}")

    if default_values.key?('id')
        default_values['resource_id'] = default_values['id']
        default_values.delete('id')
    end
    super( default_values )
end

Class Method Details

.all(client) ⇒ Object

Loads all Resource objects by this type

@param [BigDoor::Client] client
  Initialized BigDoor::Client object

@return [Array] Array of BigDoor::Resource derieved objects


193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/big_door/resource.rb', line 193

def self.all( client )
    uri = end_point
    $log.debug( "end_point = #{uri}")
    response = client.get( uri, { 'format' => 'json' } )
    $log.debug( "response to all = #{response}")
    allobj = response.map{ |obj| 
        $log.debug("obj = #{obj.inspect}")
        self.new(obj) 
    }
    $log.debug( "allobj = #{allobj.inspect}")
    allobj
end

.end_pointObject

Derieve API end point form Class name

@return [String] resource name


77
78
79
# File 'lib/big_door/resource.rb', line 77

def self.end_point
    end_point_from_classname( self.name )
end

.end_point_from_classname(name) ⇒ Object

Derieve API end point form Class name

@param [String] Class name

@return [String] resource name


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/big_door/resource.rb', line 43

def self.end_point_from_classname( name )
    # FIXME should fail if name = Resource
    $log.debug("end_point_from_classname called with name = #{name}")
    if name =~ /BigDoor::(.+)$/
        resource_name = $1

        $log.debug("resource_name = #{resource_name}")

        resource_name.gsub!(/([A-Z]+)([A-Z][a-z])/, '\1_\2');
        resource_name.gsub!(/([a-z\d])([A-Z])/, '\1_\2');
        # FIXME if no match
        resource_name.downcase!

    else
        # FIXME if no match
    end
    $log.debug("resource_name = #{resource_name}")
    resource_name
end

Instance Method Details

#delete(client, id = nil) ⇒ Object

Deletes Resource object identified by optional ID parameter or by object own resource_id

@param [BigDoor::Client] client
  Initialized BigDoor::Client object

@param [String] id
  Object resource ID

@return nothing


177
178
179
180
181
182
183
# File 'lib/big_door/resource.rb', line 177

def delete( client, id = nil)
    # FIXME if id defined - should use class method
    id = self.resource_id unless id
    # FIXME if no id defined
    client.delete( (sprintf "%s/%s", end_point, id), { 'format' => 'json' })
    self.resource_id = nil
end

#end_pointObject

Derieve API end point form Class name

@return [String] resource name


68
69
70
# File 'lib/big_door/resource.rb', line 68

def end_point
    Resource.end_point_from_classname( self.class.name )
end

#get_idObject

Get Resource id for Resource Object. To be overriden in child classes.

@return [String] resource ID


86
87
88
# File 'lib/big_door/resource.rb', line 86

def get_id 
    self.resource_id
end

#instance_to_payloadHash

Convert object content to Hash

Returns:

  • (Hash)

    Object fields



95
96
97
98
99
100
101
# File 'lib/big_door/resource.rb', line 95

def instance_to_payload
    payload = {}
    @table.each do |key, value|
        payload[key] = value
    end
    payload
end

#load(client, id = nil) ⇒ Object

Loads Resource object identified by optional ID parameter or by object own resource_id

@param [BigDoor::Client] client
  Initialized BigDoor::Client object

@param [String] id
  Object resource ID

@return nothing

Raises:

  • (ArgumentError)


157
158
159
160
161
162
163
# File 'lib/big_door/resource.rb', line 157

def load( client, id = nil) 
    id = self.get_id unless id
    raise ArgumentError.new('Pass id as param or set resource_id for object ') unless id
    $log.debug('GET');
    response = client.get( sprintf("%s/%s", end_point, id), { 'format' => 'json' })
    response_to_instance( response )
end

#response_to_instance(response) ⇒ Object

Sets object fields to values from response Hash

@param [Hash] response
  Hash representing decoded JSON response from API

@return nothing


111
112
113
114
115
116
117
118
119
120
# File 'lib/big_door/resource.rb', line 111

def response_to_instance( response )
    $log.debug('update instance from response')
    response.each do |key, value|
        key = 'resource_id' if key == 'id' 
        value = value.to_s
        value.gsub!(/'/, "\\\\'")
        self.instance_eval("self.#{key} = \'#{value}\'")
        $log.debug(sprintf "%s = %s", key, self.instance_eval("self.#{key}") )
    end
end

#save(client) ⇒ Object

Saves Resource object. If object has id attribute defined than PUT method is used, otherwise POST is used.

@param [BigDoor::Client] client
  Initialized BigDoor::Client object

@return nothing


131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/big_door/resource.rb', line 131

def save( client )
    uri = end_point
    $log.debug( "end_point = #{uri}")
    payload = instance_to_payload
    if payload.key?(:resource_id)
        $log.debug('PUT');
        response = client.put( sprintf("%s/%s", uri, self.get_id ), { 'format' => 'json' }, payload )
    else
        $log.debug('POST');
        response = client.post( "#{uri}", { 'format' => 'json' }, payload )
    end
    response_to_instance( response )
end