Class: Rubill::Session

Inherits:
Object
  • Object
show all
Includes:
HTTParty, Singleton
Defined in:
lib/rubill/session.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSession

Returns a new instance of Session.



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/rubill/session.rb', line 16

def initialize
  config = self.class.configuration
  if missing = (!config.missing_keys.empty? && config.missing_keys)
    raise "Missing key(s) in configuration: #{missing}"
  end

  if config.sandbox
    self.class.base_uri "https://api-stage.bill.com/api/v2"
  end

  
end

Instance Attribute Details

#idObject

Returns the value of attribute id.



12
13
14
# File 'lib/rubill/session.rb', line 12

def id
  @id
end

Class Method Details

._post(url, options) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/rubill/session.rb', line 73

def self._post(url, options)
  if options.key?(:fileName)
    file = StringIO.new(options.delete(:content))
  end

  post_options = {
    body: options,
    headers: default_headers,
  }

  post_options[:file] = file if file

  if self.configuration.debug
    post_options[:debug_output] = $stdout
  end

  response = post(url, post_options)
  result = JSON.parse(response.body, symbolize_names: true)

  unless result[:response_status] == 0
    raise APIError.new(result[:response_data][:error_message])
  end

  result[:response_data]
end

.configurationObject



99
100
101
# File 'lib/rubill/session.rb', line 99

def self.configuration
  Rubill::configuration
end

.default_headersObject



56
57
58
# File 'lib/rubill/session.rb', line 56

def self.default_headers
  {"Content-Type" => "application/x-www-form-urlencoded"}
end

.loginObject



37
38
39
40
41
42
43
44
45
46
# File 'lib/rubill/session.rb', line 37

def self.
   = {
    password: configuration.password,
    userName: configuration.user_name,
    devKey: configuration.dev_key,
    orgId: configuration.org_id,
  }
   = _post("/Login.json", )
  [:sessionId]
end

Instance Method Details

#_post(url, data, retries = 0) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/rubill/session.rb', line 60

def _post(url, data, retries=0)
  begin
    self.class._post(url, options(data))
  rescue APIError => e
    if e.message =~ /Session is invalid/ && retries < 3
      
      _post(url, data, retries + 1)
    else
      raise
    end
  end
end

#execute(query) ⇒ Object



29
30
31
# File 'lib/rubill/session.rb', line 29

def execute(query)
  _post(query.url, query.options)
end

#loginObject



33
34
35
# File 'lib/rubill/session.rb', line 33

def 
  self.id = self.class.
end

#options(data = {}) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/rubill/session.rb', line 48

def options(data={})
  {
    sessionId: id,
    devKey: self.class.configuration.dev_key,
    data: data.to_json,
  }
end