Class: RSpreedly::Base

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/rspreedly/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ Base

Returns a new instance of Base.



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

def initialize(attrs={})
  @errors = []
  self.attributes = attrs
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



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

def errors
  @errors
end

Class Method Details

.api_request(type, path, options = {}) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/rspreedly/base.rb', line 10

def self.api_request(type, path, options={})
  site_name = RSpreedly::Config.site_name
  api_key = RSpreedly::Config.api_key
  path = "/#{site_name}#{path}"

  options.merge!({
    :basic_auth => {:username => api_key, :password => 'X'},
    :headers    => {"Content-Type" => 'application/xml'}
  })
  self.do_request(type, path, options)
end

.do_request(type, path, options) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rspreedly/base.rb', line 22

def self.do_request(type, path, options)
  begin
    response = self.send(type.to_s, path, options)      
  rescue SocketError
    raise(RSpreedly::Error::ConnectionFailed.new, "Failed to connect to payment gateway.")
  end
  
  case response.code.to_i
  when 401
    raise(RSpreedly::Error::AccessDenied.new(response), response.body)
  when 403
    raise(RSpreedly::Error::Forbidden.new(response), response.body)
  when 422
    raise(RSpreedly::Error::BadRequest.new(response), response.body)
  when 404
    raise(RSpreedly::Error::NotFound.new(response), response.body)
  when 504
    raise(RSpreedly::Error::GatewayTimeout.new(response), response.body)
  end      

  response
end

Instance Method Details

#api_request(type, path, options = {}) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/rspreedly/base.rb', line 56

def api_request(type, path, options={})
  @errors = []
  begin
    self.class.api_request(type, path, options)
  rescue RSpreedly::Error::Base => e
    if e.response.is_a?(Hash)
      if e.response.has_key?("errors")
        @errors = [*e.response["errors"]["error"]]
      else
        @errors = [e.response.body]
      end      
    else
      @errors = [e.message]
    end
    raise
  end
end

#attributes=(attrs) ⇒ Object



50
51
52
53
54
# File 'lib/rspreedly/base.rb', line 50

def attributes=(attrs)
  attrs.each do |k, v|
    self.send(:"#{k}=", v) if self.respond_to?(:"#{k}=")
  end      
end

#to_xml(opts = {}) ⇒ Object

TODO - do this nicer rather eew at the moment and hand made XML is not nice



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/rspreedly/base.rb', line 76

def to_xml(opts={})
  exclude = opts[:exclude] || []
  exclude << :errors
  
  tag     = opts[:tag] || RSpreedly.underscore(self.class.to_s)
  inner   = opts[:inner]
  outer   = opts[:outer]      
  no_tag  = opts[:no_tag]
  
  xml = ""
  xml << "<#{outer}>" if outer
  xml << "<#{tag}>" unless no_tag
  xml << "<#{inner}>" if inner
  self.instance_variables.each do |var|
    name = var.to_s.gsub('@', '')
    next if exclude.include?(name.to_sym)
    value = self.instance_variable_get(var)
    if value.respond_to?(:to_xml)
      value = value.to_xml(:no_tag => (RSpreedly.underscore(value.class.to_s) == name)) 
    end
    xml << "<#{name}>#{value}</#{name}>"
  end
  xml << "</#{inner}>" if inner      
  xml << "</#{tag}>" unless no_tag
  xml << "</#{outer}>" if outer      
  xml
end