Class: FreshBooks::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/freshbooks/connection.rb

Constant Summary collapse

@@logger =
Logger.new(STDOUT)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(account_url, auth_token, request_headers = {}, options = {}) ⇒ Connection

Returns a new instance of Connection.


23
24
25
26
27
28
29
30
31
# File 'lib/freshbooks/connection.rb', line 23

def initialize(, auth_token, request_headers = {}, options = {})
  raise InvalidAccountUrlError.new("account_url is expected to be in the form www.example.com without any protocol string or trailing query parameters") unless  =~ /^[0-9a-zA-Z\-_]+\.(freshbooks|billingarm)\.com$/

  @account_url = 
  @auth_token = auth_token
  @request_headers = request_headers
  @utc_offset = options[:utc_offset] || -4
  @start_session_count = 0
end

Instance Attribute Details

#account_urlObject (readonly)

Returns the value of attribute account_url


7
8
9
# File 'lib/freshbooks/connection.rb', line 7

def 
  @account_url
end

#auth_tokenObject (readonly)

Returns the value of attribute auth_token


7
8
9
# File 'lib/freshbooks/connection.rb', line 7

def auth_token
  @auth_token
end

#request_headersObject (readonly)

Returns the value of attribute request_headers


7
8
9
# File 'lib/freshbooks/connection.rb', line 7

def request_headers
  @request_headers
end

#utc_offsetObject (readonly)

Returns the value of attribute utc_offset


7
8
9
# File 'lib/freshbooks/connection.rb', line 7

def utc_offset
  @utc_offset
end

Class Method Details

.log_level=(level) ⇒ Object


18
19
20
# File 'lib/freshbooks/connection.rb', line 18

def self.log_level=(level)
  @@logger.level = level
end

Instance Method Details

#call_api(method, elements = []) ⇒ Object


33
34
35
36
37
# File 'lib/freshbooks/connection.rb', line 33

def call_api(method, elements = [])
  request = create_request(method, elements)
  result = post(request)
  Response.new(result)
end

#direct_post(xml) ⇒ Object


39
40
41
42
# File 'lib/freshbooks/connection.rb', line 39

def direct_post(xml)
  result = post(xml)
  Response.new(result)
end

#loggerObject


10
11
12
# File 'lib/freshbooks/connection.rb', line 10

def logger
  @@logger
end

#logger=(value) ⇒ Object


14
15
16
# File 'lib/freshbooks/connection.rb', line 14

def logger=(value)
  @@logger = value
end

#start_session(&block) ⇒ Object


44
45
46
47
48
49
50
51
52
53
54
# File 'lib/freshbooks/connection.rb', line 44

def start_session(&block)
  @connection = obtain_connection if @start_session_count == 0
  @start_session_count = @start_session_count + 1

  begin
    block.call(@connection)
  ensure
    @start_session_count = @start_session_count - 1
    close if @start_session_count == 0
  end
end