Class: Powncer::Base

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

Direct Known Subclasses

Note, User

Constant Summary collapse

FORMAT =
'json'

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_id, *args) ⇒ Object (private)



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

def method_missing(method_id, *args)
  attribute_name = method_id.to_s
  if @attributes.stringify_keys!.has_key?(attribute_name)
    return @attributes.fetch(method_id.to_s)
  else
    orig_method_missing(method_id, args)
  end
end

Class Method Details

.extract_options(args) ⇒ Object



55
56
57
# File 'lib/powncer/base.rb', line 55

def extract_options(args)
  args.last.is_a?(Hash) ? args.pop : {}
end

.instantiate(attributes) ⇒ Object



49
50
51
52
53
# File 'lib/powncer/base.rb', line 49

def instantiate(attributes)
  object = allocate
  object.instance_variable_set("@attributes", attributes)
  object
end

.parse(response) ⇒ Object



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

def parse(response)
  json = JSON.parse(response)
  if json.has_key? 'error'
    raise WebServiceError, json["error"]["message"]
  end
  return json
end

.post(url, *params) ⇒ Object



35
36
37
38
39
# File 'lib/powncer/base.rb', line 35

def post(url, *params)
  options = extract_options(params.flatten)
  response = Powncer.connect!.post(url, options)
  parse(response)
end

.request(url) ⇒ Object



30
31
32
33
# File 'lib/powncer/base.rb', line 30

def request(url)
  response = Powncer.connect!.get(url)
  parse(response)
end

Instance Method Details

#idObject



16
17
18
# File 'lib/powncer/base.rb', line 16

def id
  @attributes["id"] || nil
end

#post(url, *params) ⇒ Object



24
25
26
# File 'lib/powncer/base.rb', line 24

def post(url, *params)
  self.class.post(url, params)
end

#request(url) ⇒ Object



20
21
22
# File 'lib/powncer/base.rb', line 20

def request(url)
  self.class.request(url)
end