Class: Phocoder::Base

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

Direct Known Subclasses

HTTP, Job, Response

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.api_keyObject



4
5
6
# File 'lib/phocoder/base.rb', line 4

def self.api_key
  Phocoder.api_key
end

.base_urlObject



8
9
10
# File 'lib/phocoder/base.rb', line 8

def self.base_url
  Phocoder.base_url
end

.decode(content, format = nil) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/phocoder/base.rb', line 30

def self.decode(content, format=nil)
  begin
    if content.is_a?(String)
      if format.to_s == 'xml'
        Hash.from_xml(content)
      else
        ActiveSupport::JSON.decode(content)
      end
    else
      content
    end
  rescue Exception => e
    content
  end   
end

.encode(content, format = nil) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/phocoder/base.rb', line 12

def self.encode(content, format=nil)
  if content.is_a?(String)
    content
  elsif format.to_s == 'xml'
    if content.is_a?(Hash) && content.keys.size == 1
      content[content.keys.first].to_xml(:root => content.keys.first)
    else
      content.to_xml
    end
  else
    content.to_json
  end
end

Instance Method Details

#decode(content, format = nil) ⇒ Object



46
47
48
# File 'lib/phocoder/base.rb', line 46

def decode(content, format=nil)
  self.class.decode(content, format)
end

#encode(content, format = nil) ⇒ Object



26
27
28
# File 'lib/phocoder/base.rb', line 26

def encode(content, format=nil)
  self.class.encode(content, format)
end