Class: Beanie::Api

Inherits:
Object
  • Object
show all
Defined in:
lib/beanie/api.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.all(opts = {}) ⇒ Object

Find all the objects



36
37
38
39
40
41
42
43
# File 'lib/beanie/api.rb', line 36

def self.all(opts = {})
  data = get(opts)
  objlist = []
  data[new.object_name.pluralize].each do |objdata|
    objlist << new.populate(objdata)
  end
  objlist
end

.build_url(opts = {}) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/beanie/api.rb', line 131

def self.build_url(opts = {})
  if opts[:url]
    url = "#{Beanie.base_uri}#{opts[:url]}"
    opts.delete(:url)
  else
    url = "#{Beanie.base_uri}/#{new.construct_path(opts)}"
  end
  if opts[:id]
    url += "/#{opts[:id]}"
    opts.delete(:id)
  end
  if opts[:extension]
    url += "/#{opts[:extension]}"
    opts.delete(:extension)
  end
  url += ".json"
  optstr = ""
  opts.each_key do |key|
    optstr += (optstr.length == 0) ? "?" : "&"
    optstr += "#{key.to_s}=#{opts[key]}"
  end
  url + optstr
end

.delete(data, opts = {}) ⇒ Object

Run a REST DELETE against the Beanie API.



124
125
126
127
# File 'lib/beanie/api.rb', line 124

def self.delete(data, opts = {})
  response = RestClient.delete(build_url(opts), headers=set_headers)
  JSON.parse(response.body)
end

.find(id) ⇒ Object

Find an object based on its ID



47
48
49
50
51
# File 'lib/beanie/api.rb', line 47

def self.find(id)
  data = get(:id => id)
  obj = new
  obj.populate(data[obj.object_name])
end

.get(opts = {}) ⇒ Object

Run a REST GET against the Beanie API.



99
100
101
102
# File 'lib/beanie/api.rb', line 99

def self.get(opts = {})
  response = RestClient.get(build_url(opts), headers=set_headers)
  JSON.parse(response.body)
end

.post(data, opts = {}) ⇒ Object

Run a REST POST against the Beanie API.



117
118
119
120
# File 'lib/beanie/api.rb', line 117

def self.post(data, opts = {})
  response = RestClient.post(build_url(opts), data.to_json, headers=set_headers)
  JSON.parse(response.body)
end

.put(data, opts = {}) ⇒ Object

Run a REST PUT against the Beanie API.



106
107
108
109
110
111
112
113
# File 'lib/beanie/api.rb', line 106

def self.put(data, opts = {})
  response = RestClient.put(build_url(opts), data, headers=set_headers)
  if response.body and response.body.length > 0
    JSON.parse(response.body)
  else
    ""
  end
end

.set_headersObject

Set the default API headers.



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

def self.set_headers
  #
  # Make sure we have a token for this.
  headers = {:content_type => :json, :accept => :json}
  headers[:x_auth_token] = Beanie.get_token
  headers
end

Instance Method Details

#construct_path(opts = {}) ⇒ Object

Default mechanism for constructing a path



167
168
169
# File 'lib/beanie/api.rb', line 167

def construct_path(opts = {})
  object_name.pluralize
end

#extractObject

Populate the instance variables from the provided hash.



83
84
85
86
87
88
89
# File 'lib/beanie/api.rb', line 83

def extract
  data = {}
  self.instance_variables.each do |var|
    data[field_name(var)] = self.instance_variable_get(var)
  end
  data
end

#field_name(var) ⇒ Object

Determine the field name



93
94
95
# File 'lib/beanie/api.rb', line 93

def field_name(var)
  var.to_s.gsub(/^@/, '')
end

#object_nameObject

Compute a Beanie object name from the class name.



173
174
175
176
177
178
179
180
# File 'lib/beanie/api.rb', line 173

def object_name
  object_name = self.class.name.
      gsub(/.*::/, '').
      gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
      gsub(/([a-z\d])([A-Z])/,'\1_\2').
      tr("-", "_").
      downcase
end

#populate(data) ⇒ Object

Populate the instance variables from the provided hash.



74
75
76
77
78
79
# File 'lib/beanie/api.rb', line 74

def populate(data)
  self.instance_variables.each do |var|
    self.instance_variable_set(var, data[field_name(var)])
  end
  self
end

#save!(opts = {}) ⇒ Object

Save the customer data



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/beanie/api.rb', line 55

def save!(opts = {})
  data = extract()
  data.delete("id")
  objdata = {object_name => data}
  if @id
    #
    # Needs an update...
    opts[:id] = @id
    response = self.class.put(objdata, opts)
else
    #
    # Needs a create!
    response = self.class.post(objdata, opts)
  end
  self.populate(response[self.object_name])
end