Class: Gateway::Quickbook

Inherits:
Result
  • Object
show all
Defined in:
lib/gateway/quickbook.rb

Defined Under Namespace

Modules: API, ENVIRONMENT, HTTP

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Result

#handle_query_result

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



7
8
9
# File 'lib/gateway/quickbook.rb', line 7

def access_token
  @access_token
end

#apiObject

Returns the value of attribute api.



6
7
8
# File 'lib/gateway/quickbook.rb', line 6

def api
  @api
end

#company_idObject

Returns the value of attribute company_id.



7
8
9
# File 'lib/gateway/quickbook.rb', line 7

def company_id
  @company_id
end

#consumerObject

Returns the value of attribute consumer.



7
8
9
# File 'lib/gateway/quickbook.rb', line 7

def consumer
  @consumer
end

Class Method Details

.connect(options) ⇒ Object

Connect to QuickBook online API gateway Options are as follow

String: Environment ENVIRONMENT::SANDBOX or ENVIRONMENT::PRODUCTION. AutoLoad QuickBook key, secret, access token, access secret, company ID from config/quickbook.yml corresponding to environment given.
Hash: 
  :client_id      =>   QuickBook App Client ID,
  :client_secret  =>   QuickBook App Client Secret,
  :refresh_token  =>   Can generate from playground https://developer.intuit.com/app/developer/playground, validate for 100 days
  :company_id     =>   QuickBook Company ID,
  :environment    =>   ENVIRONMENT::SANDBOX or ENVIRONMENT::PRODUCTION


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/gateway/quickbook.rb', line 40

def self.connect(options)   
  if(options.class == String)
    configuration = YAML.load_file("config/quickbook.yml")
    environment = options
    options = Hash.new
    options[:client_id] = configuration[environment]["client_id"]
    options[:client_secret] =   configuration[environment]["client_secret"]
    options[:refresh_token] =   configuration[environment]["refresh_token"]
    options[:company_id] =   configuration[environment]["company_id"]
    options[:environment] = environment
  end
  
  gateway = new
  gateway.consumer = OAuth2::Client.new(options[:client_id], options[:client_secret], {:token_url => 'https://oauth.platform.intuit.com/oauth2/v1/tokens/bearer', auth_scheme: :basic_auth})
  gateway.access_token = OAuth2::AccessToken.new(gateway.consumer, nil, {refresh_token: options[:refresh_token]})
  gateway.access_token = gateway.access_token.refresh!
  
  gateway.company_id = options[:company_id]
  
  case options[:environment]
  when ENVIRONMENT::PRODUCTION
    gateway.send("api=", "https://quickbooks.api.intuit.com")
  else
    gateway.send("api=", "https://sandbox-quickbooks.api.intuit.com")
  end   
  
  Service::Record.quickbook_gateway = gateway
end

Instance Method Details

#get(path, parameters = {}) ⇒ Object



76
77
78
# File 'lib/gateway/quickbook.rb', line 76

def get(path, parameters = {})
  return handle_query_result(self.access_token.get(url(path, parameters), {headers: {"Accept" => HTTP::JSON}}))
end

#post(path, parameters, body) ⇒ Object



84
85
86
# File 'lib/gateway/quickbook.rb', line 84

def post(path, parameters, body)
  return handle_query_result(self.access_token.post(url(path, parameters), {body: body, headers: {"Content-Type" => HTTP::JSON, "Accept" => HTTP::JSON}}))
end

#query_data(query) ⇒ Object



80
81
82
# File 'lib/gateway/quickbook.rb', line 80

def query_data(query)
  return get(API::QUERY, {:query => query})
end