Class: SifiApi::Resource

Inherits:
Object
  • Object
show all
Includes:
ActiveSupport::Rescuable
Defined in:
lib/sifi_api/resource.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(json, connection, user_key) ⇒ Resource

Returns a new instance of Resource.



6
7
8
9
10
11
12
# File 'lib/sifi_api/resource.rb', line 6

def initialize(json, connection, user_key)
  @json = json
  @connection = connection
  @user_key = user_key
  @cache = {}
  @paging = false
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/sifi_api/resource.rb', line 78

def method_missing(sym, *args, &block)
  if @json.respond_to?(sym)
    @json.send(sym, *args, &block)
  elsif val = (@json["resources"] || []).select { |r| r[sym.to_s] }.first
    return get_resource(sym.to_s, val[sym.to_s], *args)
  elsif @json.has_key?(sym.to_s)
    return @json[sym.to_s]
  elsif val = (@json["actions"] || []).select { |a| a[sym.to_s] }.first
    return do_action(val[sym.to_s])
  else
    super(sym, *args, &block)
  end
end

Class Method Details

.get_via_uri(connection, user_key, uri = '', params = {}) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/sifi_api/resource.rb', line 66

def self.get_via_uri(connection, user_key, uri='', params={})
  a = []
  response = connection.get(user_key, uri, params)
  if response && response.body
    response.body[resource_name].each do |record|
      resource = self.new(record, connection, user_key)
      a << resource
    end
    SifiApi::ResourceCollection.new(resource_name, a, response.body["paging"], connection, user_key)
  end
end

.resource_nameObject



115
116
117
# File 'lib/sifi_api/resource.rb', line 115

def self.resource_name
  self.name.split("::").last.underscore.pluralize.downcase
end

Instance Method Details

#create(resource, params = {}) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/sifi_api/resource.rb', line 29

def create(resource, params={})
  params = { resource => params }
  execute_with_rescue do
    resource_name = resource.to_s.pluralize
    response = @connection.post(@user_key, self.resource + "/#{resource_name}", params)
    if response && response.body
      record = response.body[resource_name].first
      SifiApi.const_get(resource.to_s.classify).new(record, @connection, @user_key)
    end
  end
end

#deleteObject



49
50
51
52
53
# File 'lib/sifi_api/resource.rb', line 49

def delete
  execute_with_rescue do
    @connection.delete(@user_key, self.resource)
  end
end

#do_action(action) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/sifi_api/resource.rb', line 92

def do_action(action)
  execute_with_rescue do
    response = @connection.send(action["method"].downcase, @user_key, action["href"])
    if response && response.status == 200
      if (response[:content_type] || "").include?("application/json")
        @json = response.body[resource_name].first
        return self
      elsif ["text/csv", "application/zip"].include?(response[:content_type])
        filename = response.headers[:content_disposition].match(/filename="(.+)"/)[1]
        file = Tempfile.new(filename)
        file.write(response.body)
        file.rewind
        return file
      end
    end
    response
  end
end

#find(resource, id) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/sifi_api/resource.rb', line 41

def find(resource, id)
  execute_with_rescue do
    resource_name = resource.to_s.pluralize
    record = @connection.get(@user_key, self.resource + "/#{resource_name}").body[resource_name].select{|x| x["id"].to_i == id.to_i }.first
    record ? SifiApi.const_get(resource.to_s.classify).new(record, @connection, @user_key) : []
  end
end

#raw_jsonObject



14
15
16
# File 'lib/sifi_api/resource.rb', line 14

def raw_json
  @json
end

#reload(params = {}) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/sifi_api/resource.rb', line 55

def reload(params={})
  execute_with_rescue do
    response = @connection.get(@user_key, self.resource, params)
    if response && response.body
      @json = response.body[resource_name].first
      @cache = {}
    end
  end
  self
end

#resource_nameObject



111
112
113
# File 'lib/sifi_api/resource.rb', line 111

def resource_name
  self.class.name.split("::").last.underscore.pluralize.downcase
end

#to_sObject



119
120
121
# File 'lib/sifi_api/resource.rb', line 119

def to_s
  @json.inspect
end

#update(params = {}) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/sifi_api/resource.rb', line 18

def update(params={})
  params = { resource_name.singularize => params }
  execute_with_rescue do
    response = @connection.put(@user_key, self.resource, params)
    if response && response.body
      @json = response.body[resource_name].first
    end
  end
  self
end