Class: Socialcastr::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/socialcastr/base.rb

Direct Known Subclasses

Comment, Like, Message

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arguments = {}) ⇒ Base

Returns a new instance of Base.



5
6
7
8
9
10
# File 'lib/socialcastr/base.rb', line 5

def initialize(arguments={})
  @data = {}
  arguments.map do |k,v|
    @data[k.to_s] = v
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



68
69
70
71
72
73
74
# File 'lib/socialcastr/base.rb', line 68

def method_missing(method, *args, &block)
  if method.to_s =~ /=$/
    @data[method_name(method.to_s.sub(/=$/,''))] = *args
  else
    return @data[method_name(method)] unless @data[method_name(method)].nil?
  end
end

Class Method Details

.all(*arguments) ⇒ Object



121
122
123
# File 'lib/socialcastr/base.rb', line 121

def all(*arguments)
  find(:all, *arguments)
end

.apiObject



92
93
94
# File 'lib/socialcastr/base.rb', line 92

def api
  @api ||= Socialcastr.api
end

.collection_nameObject



160
161
162
# File 'lib/socialcastr/base.rb', line 160

def collection_name
  model_name.downcase + "s"
end

.collection_path(options = {}) ⇒ Object



137
138
139
# File 'lib/socialcastr/base.rb', line 137

def collection_path(options = {})
  "#{prefix(options)}#{collection_name}"
end

.element_path(id, prefix_options = {}) ⇒ Object



133
134
135
# File 'lib/socialcastr/base.rb', line 133

def element_path(id, prefix_options = {})
  "#{collection_path(prefix_options)}/#{URI.escape id.to_s}"
end

.find(*arguments) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/socialcastr/base.rb', line 96

def find(*arguments)
  args = arguments.dup
  scope = args.slice!(0)
  options = args.slice!(0) || {}
  case scope
    when :all   then find_every(options)
    when :first then find_every(options).first
    when :last  then find_every(options).to_a.last
    #when :one   then find_one(options)
    else             find_single(scope, options)
  end
end

.find_every(options) ⇒ Object



115
116
117
118
119
# File 'lib/socialcastr/base.rb', line 115

def find_every(options)
  (prefix_options, query_options) = parse_options(options)
  path = collection_path(prefix_options)
  parse(api.get(path, query_options))
end

.find_single(id, options) ⇒ Object



109
110
111
112
113
# File 'lib/socialcastr/base.rb', line 109

def find_single(id, options)
  (prefix_options, query_options) = parse_options(options)
  path = element_path(id, prefix_options)
  parse(api.get(path, query_options))
end

.first(*arguments) ⇒ Object



125
126
127
# File 'lib/socialcastr/base.rb', line 125

def first(*arguments)
  find(:first, *arguments)
end

.from_hash(h) ⇒ Object



88
89
90
# File 'lib/socialcastr/base.rb', line 88

def from_hash(h)
  new.tap { |object| object.instance_variable_set("@data", h) }
end

.last(*arguments) ⇒ Object



129
130
131
# File 'lib/socialcastr/base.rb', line 129

def last(*arguments)
  find(:last, *arguments)
end

.model_nameObject



156
157
158
# File 'lib/socialcastr/base.rb', line 156

def model_name
  self.to_s.gsub(/^.*::/, '')
end

.parse(xml = "") ⇒ Object



77
78
79
80
81
82
83
84
85
86
# File 'lib/socialcastr/base.rb', line 77

def parse(xml="")
  source = SAX::ActiveResource.new
  Nokogiri::XML::SAX::Parser.new(source).parse(xml)
  case source.data
  when Hash
    return from_hash(source.data)
  else
    return source.data
  end
end

.parse_options(options) ⇒ Object



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

def parse_options(options)
  prefix_options = {}
  options.each_pair do |k,v|
    if k.to_s.match(/_id$/)
      prefix_options[k] = v
      options.delete(k)
    end
  end
  return [prefix_options, options]
end

.prefix(options) ⇒ Object



141
142
143
# File 'lib/socialcastr/base.rb', line 141

def prefix(options)
  options.map { |k,v| k.to_s.gsub("_id", 's') + "/" + v.to_s }.join("/") + "/"
end

Instance Method Details

#apiObject



36
37
38
# File 'lib/socialcastr/base.rb', line 36

def api
  self.class.api
end

#collection_pathObject



44
45
46
# File 'lib/socialcastr/base.rb', line 44

def collection_path
  self.class.collection_path
end

#copy_attributes_from_object(object = nil) ⇒ Object



28
29
30
# File 'lib/socialcastr/base.rb', line 28

def copy_attributes_from_object(object=nil)
  @data = object.instance_variable_get("@data")
end

#createObject



16
17
18
19
20
# File 'lib/socialcastr/base.rb', line 16

def create
    api.post(collection_path, to_params).tap do |xml|
      copy_attributes_from_object(self.class.parse(xml))
    end
end

#element_pathObject



40
41
42
# File 'lib/socialcastr/base.rb', line 40

def element_path
  self.class.element_path(self.id)
end

#idObject



64
65
66
# File 'lib/socialcastr/base.rb', line 64

def id
  return @data["id"]
end

#method_name(s) ⇒ Object



60
61
62
# File 'lib/socialcastr/base.rb', line 60

def method_name(s)
  s.to_s.gsub("_","-")
end

#new?Boolean

Returns:



32
33
34
# File 'lib/socialcastr/base.rb', line 32

def new?
  id.nil?
end

#param_name(variable_name) ⇒ Object



56
57
58
# File 'lib/socialcastr/base.rb', line 56

def param_name(variable_name)
  "#{self.class.model_name.downcase}[#{variable_name}]"
end

#saveObject



12
13
14
# File 'lib/socialcastr/base.rb', line 12

def save
  new? ? create : update
end

#to_paramsObject



48
49
50
51
52
53
54
# File 'lib/socialcastr/base.rb', line 48

def to_params
  params = {}
  @data.each_pair do |k,v| 
    params[param_name(k)] = v
  end
  params
end

#updateObject



22
23
24
25
26
# File 'lib/socialcastr/base.rb', line 22

def update
    api.put(element_path, to_params).tap do |xml|
      copy_attributes_from_object(self.class.parse(xml))
    end
end