infuser 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,10 @@
1
+ module Infuser
2
+ class OrderItem < Infuser::Models::Base
3
+
4
+ define_schema :item_description, :item_name, :item_type, :notes, :order_id,
5
+ :product_id, :cpu, :ppu, :qty
6
+
7
+ has_many :invoice_items
8
+
9
+ end
10
+ end
@@ -0,0 +1,13 @@
1
+ module Infuser
2
+ class Phone < Infuser::Collections::Base
3
+
4
+ define_mappings( (1..5).each_with_object({}) do |i, hash|
5
+ hash[i] = {
6
+ "phone#{i}".to_sym => :number,
7
+ "phone#{i}_ext".to_sym => :extension,
8
+ "phone#{i}_type".to_sym => :type,
9
+ }
10
+ end )
11
+
12
+ end
13
+ end
@@ -0,0 +1,80 @@
1
+ require 'xmlrpc/client'
2
+
3
+ module Infuser
4
+ class Requester
5
+
6
+ def initialize access_token
7
+ @access_token = access_token
8
+ end
9
+
10
+ def get service_call, *args
11
+ reset!
12
+ connection service_call, *args
13
+ end
14
+
15
+
16
+ private
17
+
18
+ def connection service_call, *args
19
+ retry? ? retrying! : raise(Infuser::Error, 'Call failed and retries exhausted.')
20
+
21
+ begin
22
+ logger.info "CALL: #{service_call} at: #{Time.now} args: #{args.inspect} #{options.api_key}"
23
+ result = client.call(service_call, options.api_key, *args)
24
+ connection(service_call, *args) if result.nil?
25
+ rescue Timeout::Error => timeout
26
+ connection(service_call, *args)
27
+ rescue XMLRPC::FaultException => xmlrpc_error
28
+ Infuser::ExceptionHandler.new(xmlrpc_error)
29
+ rescue RuntimeError => e
30
+ e.message.include?('Authorization failed') ? raise(Infuser::ExpiredToken, "Access token is expired or invalid.") : raise
31
+ end
32
+
33
+ logger.info "RESULT: #{result.inspect}"
34
+ return result
35
+ end
36
+
37
+ def retry?
38
+ retry_count <= options.retry_count
39
+ end
40
+
41
+ def retrying!
42
+ @retry_count += 1
43
+ end
44
+
45
+ def retry_count
46
+ @retry_count ||= 0
47
+ end
48
+
49
+ def reset!
50
+ @retry_count = 0
51
+ end
52
+
53
+ def options
54
+ Infuser::Configuration
55
+ end
56
+
57
+ def logger
58
+ options.logger
59
+ end
60
+
61
+ def access_token
62
+ @access_token || raise(Infuser::ArgumentError, 'You must specify an access token. See documentation.')
63
+ end
64
+
65
+ def client
66
+ @client ||= begin
67
+ client = XMLRPC::Client.new3({
68
+ 'host' => 'api.infusionsoft.com',
69
+ 'path' => "/crm/xmlrpc/v1?access_token=#{access_token}",
70
+ 'port' => 443,
71
+ 'use_ssl' => true
72
+ })
73
+ client.http_header_extra = { 'User-Agent' => options.user_agent }
74
+ client.http_header_extra = { "accept-encoding" => "identity" }
75
+ client
76
+ end
77
+ end
78
+
79
+ end
80
+ end
@@ -0,0 +1,77 @@
1
+ module Infuser
2
+ module Tables
3
+ class Base
4
+ include Infuser::Helpers::Hashie
5
+
6
+ PAGINATION = 1000
7
+
8
+ def initialize client
9
+ @client = client
10
+ self
11
+ end
12
+
13
+ def build data
14
+ item = model_klass.new(data)
15
+ item.instance_variable_set(:@client, client)
16
+ item
17
+ end
18
+
19
+ def create data
20
+ build(data).save
21
+ end
22
+
23
+ def find id
24
+ item = model_klass.new
25
+ item.instance_variable_set(:@id, id)
26
+ item.instance_variable_set(:@client, client)
27
+ item.load
28
+ end
29
+
30
+ def find_by hash
31
+ page = 0
32
+ count = PAGINATION
33
+ records = []
34
+
35
+ begin
36
+ response = client.get("DataService.query", klass_name, PAGINATION, page, camelize_hash(hash), (model_klass.fieldset.dup << 'Id'))
37
+ page += 1
38
+ count = response.count
39
+
40
+ records << response.map do |hash|
41
+ item = model_klass.new
42
+ item.instance_variable_set(:@client, client)
43
+ item.instance_variable_set(:@id, hash['Id'])
44
+ item.populate(hash.reject { |k, v| k == 'Id'})
45
+ item
46
+ end
47
+ end while count == PAGINATION
48
+
49
+ records.flatten
50
+ end
51
+
52
+ def all
53
+ find_by id: '%'
54
+ end
55
+
56
+
57
+ private
58
+
59
+ def klass_name
60
+ self.class.name.split('::').last
61
+ end
62
+
63
+ def service_name
64
+ "#{klass_name.underscore}_service".classify
65
+ end
66
+
67
+ def model_klass
68
+ Infuser.const_get(klass_name)
69
+ end
70
+
71
+ def client
72
+ @client || raise(Infuser::Error, "Do not use the #{self.class.name} class directly. Use the Infuser::Client instead.")
73
+ end
74
+
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,6 @@
1
+ module Infuser
2
+ module Tables
3
+ class Company < Base
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ module Infuser
2
+ module Tables
3
+ class Contact < Base
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,20 @@
1
+ module Infuser
2
+ module Tables
3
+ class Invoice < Base
4
+
5
+ def build data
6
+ raise(NotImplementedError, 'You must use #create method for invoices. Infusionsoft is a delight :)')
7
+ end
8
+
9
+ def create *data
10
+ # Add in specific order:
11
+ # contact_id, description, date, then:
12
+ # lead_affiliate_id 0 should be used if none
13
+ # sale_affiliate_id 0 should be used if none
14
+ id = client.get("InvoiceService.createBlankOrder", *data)
15
+ find(id)
16
+ end
17
+
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,6 @@
1
+ module Infuser
2
+ module Tables
3
+ class InvoiceItem < Base
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ module Infuser
2
+ module Tables
3
+ class OrderItem < Base
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,35 @@
1
+ require 'rest_client'
2
+ require 'base64'
3
+ require 'json'
4
+
5
+ module Infuser
6
+ class TokenRefresher
7
+
8
+ attr_reader :refresh_token
9
+
10
+ def initialize refresh_token
11
+ @refresh_token = refresh_token
12
+ end
13
+
14
+ def refresh
15
+ resource = RestClient::Resource.new(endpoint, Infuser::Configuration.api_key, Infuser::Configuration.api_secret)
16
+ JSON.parse resource.post(params)
17
+ end
18
+
19
+
20
+ private
21
+
22
+ def endpoint
23
+ 'https://api.infusionsoft.com/token'
24
+ end
25
+
26
+ def grant_type
27
+ 'refresh_token'
28
+ end
29
+
30
+ def params
31
+ { grant_type: grant_type, refresh_token: refresh_token }
32
+ end
33
+
34
+ end
35
+ end
@@ -0,0 +1,5 @@
1
+ module Infuser
2
+
3
+ VERSION = '1.0.0'.freeze
4
+
5
+ end
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: infuser
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - David Lesches
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-03-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rest-client
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: A Ruby wrapper for the Infusionsoft API
56
+ email:
57
+ - david@lesches.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - README.md
64
+ - Rakefile
65
+ - infuser.gemspec
66
+ - lib/infuser.rb
67
+ - lib/infuser/address.rb
68
+ - lib/infuser/client.rb
69
+ - lib/infuser/collections/base.rb
70
+ - lib/infuser/collections/child_proxy.rb
71
+ - lib/infuser/collections/proxy.rb
72
+ - lib/infuser/company.rb
73
+ - lib/infuser/configuration.rb
74
+ - lib/infuser/contact.rb
75
+ - lib/infuser/email.rb
76
+ - lib/infuser/exceptions.rb
77
+ - lib/infuser/fax.rb
78
+ - lib/infuser/helpers/hashie.rb
79
+ - lib/infuser/invoice.rb
80
+ - lib/infuser/invoice_item.rb
81
+ - lib/infuser/logger.rb
82
+ - lib/infuser/models/base.rb
83
+ - lib/infuser/order_item.rb
84
+ - lib/infuser/phone.rb
85
+ - lib/infuser/requester.rb
86
+ - lib/infuser/tables/base.rb
87
+ - lib/infuser/tables/company.rb
88
+ - lib/infuser/tables/contact.rb
89
+ - lib/infuser/tables/invoice.rb
90
+ - lib/infuser/tables/invoice_item.rb
91
+ - lib/infuser/tables/order_item.rb
92
+ - lib/infuser/token_refresher.rb
93
+ - lib/infuser/version.rb
94
+ homepage: https://github.com/davidlesches/infuser
95
+ licenses: []
96
+ metadata: {}
97
+ post_install_message:
98
+ rdoc_options: []
99
+ require_paths:
100
+ - lib
101
+ required_ruby_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - '>='
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ required_rubygems_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '>='
109
+ - !ruby/object:Gem::Version
110
+ version: 1.3.6
111
+ requirements: []
112
+ rubyforge_project:
113
+ rubygems_version: 2.4.1
114
+ signing_key:
115
+ specification_version: 4
116
+ summary: Ruby wrapper for the Infusionsoft API
117
+ test_files: []