Module: FreshBooks

Defined in:
lib/freshbooks.rb

Defined Under Namespace

Classes: AuthenticationError, BaseObject, Client, InternalError, Invoice, Item, Line, Payment, Project, Recurring, Response, Task, TimeEntry, UnknownSystemError

Constant Summary collapse

VERSION =

Gem version

'2.2.1'
API_VERSION =

FreshBooks API version

'2.1'
SERVICE_URL =
"/api/#{API_VERSION}/xml-in"
@@response =
nil

Class Method Summary collapse

Class Method Details

.call_api(method, elems = []) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/freshbooks.rb', line 58

def self.call_api(method, elems = [])
  doc = Document.new '<?xml version="1.0" encoding="UTF-8"?>'
  request = doc.add_element 'request'
  request.attributes['method'] = method

  elems.each do |key, value|
    if value.is_a?(BaseObject)
      elem = value.to_xml
      request.add_element elem
    else
      request.add_element(Element.new(key)).text = value.to_s
    end
  end

  result = self.post(request.to_s)

  @@response = Response.new(result)

  #
  # Failure
  #
  if @@response.fail?
    error_msg = @@response.error_msg

    # Raise an exception for unexpected errors

    raise InternalError.new       if error_msg =~ /not formatted correctly/
    raise AuthenticationError.new if error_msg =~ /[Aa]uthentication failed/
    raise UnknownSystemError.new  if error_msg =~ /does not exist/
  end

  @@response
end

.last_responseObject



54
55
56
# File 'lib/freshbooks.rb', line 54

def self.last_response
  @@response
end

.post(body) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/freshbooks.rb', line 92

def self.post(body)
  connection = Net::HTTP.new(@@account_url, 443)
  connection.use_ssl = true
  connection.verify_mode = OpenSSL::SSL::VERIFY_NONE

  request = Net::HTTP::Post.new(FreshBooks::SERVICE_URL)
  request.basic_auth @@auth_token, 'X' 
  request.body = body
  request.content_type = 'application/xml'

  result = connection.start  { |http| http.request(request) }

  result.body
end

.setup(account_url, auth_token) ⇒ Object



47
48
49
50
51
52
# File 'lib/freshbooks.rb', line 47

def self.setup(, auth_token)
  @@account_url = 
  @@auth_token = auth_token

  true
end