riq 1.0.1 → 1.0.2

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,13 @@
1
+ require_relative 'batch_manager'
2
+ require_relative 'list_item'
3
+
4
+ module RIQ
5
+ # Special child for initializing list items, who need to include extra info.
6
+ class ListItemManager < BatchManager
7
+ def initialize(lid, opts = {})
8
+ raise RIQError, 'List id can\'t be nil' if lid.nil?
9
+ @list_id = lid
10
+ super(RIQ::ListItem, opts)
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,105 @@
1
+ require_relative 'client'
2
+ using RIQExtensions
3
+
4
+ module RIQ
5
+ # @abstract This class should not be used directly.
6
+ # Instead, use a child such as {Contact} or {List}.
7
+ class RIQObject
8
+
9
+
10
+ attr_accessor :id
11
+ attr_reader :modified_date
12
+
13
+ # @param id [String, Hash] ObjectId or well-formatted hash of data (usually provided by another object
14
+ # @return [RIQObject] Self
15
+ def initialize(id = nil)
16
+ @client = RIQ.client
17
+ @id = id
18
+
19
+ unless @id.nil?
20
+ # data hash
21
+ if @id.is_a? Hash
22
+ # this looks dumb, could name variables better
23
+ data = @id
24
+ else
25
+ data = @client.get(node)
26
+ end
27
+ init(data.symbolize)
28
+ else
29
+ init
30
+ end
31
+ self
32
+ end
33
+
34
+ # @return [String] endpoint
35
+ def node
36
+ raise RIQError, 'This should be overwritten'
37
+ end
38
+
39
+ # @param id [String] ObjectId
40
+ # @return [String] endpoint
41
+ def self.node(id = nil)
42
+ raise RIQError, 'This should be overwritten'
43
+ end
44
+
45
+ # @return [Hash] all relevant stored data
46
+ def data
47
+ raise RIQError, 'This should be overwritten'
48
+ end
49
+
50
+ # @return [String] the JSON representation of {#data}
51
+ def payload
52
+ pld = {}
53
+ data.each do |k, v|
54
+ if k['_']
55
+ pld[k.to_cam] = v
56
+ else
57
+ pld[k] = v
58
+ end
59
+ end
60
+ pld.to_json
61
+ end
62
+
63
+ # Creates or updates the object
64
+ def save(options = nil)
65
+ if @id.nil?
66
+ # create
67
+ init(@client.post(node, payload, options: options).symbolize)
68
+ else
69
+ # update
70
+ init(@client.put(node, payload, options: options).symbolize)
71
+ end
72
+ end
73
+
74
+ # Deletes the object
75
+ # @note This is IRREVERSIBLE
76
+ def delete
77
+ @client.delete(node)
78
+ end
79
+
80
+ # can't decide how I want objects displayed
81
+ # when you print the object
82
+ # def to_s
83
+ # JSON.pretty_generate(data)
84
+ # data
85
+ # end
86
+
87
+ # When the object itself is called or returned
88
+ # def inspect
89
+ # data
90
+ # end
91
+
92
+ private
93
+ def init
94
+ raise RIQError, 'This should be overwritten'
95
+ end
96
+
97
+ # def exists
98
+ # if @id.nil?
99
+ # false
100
+ # else
101
+ # @client.fetch(node)
102
+ # end
103
+ # end
104
+ end
105
+ end
@@ -0,0 +1,47 @@
1
+ require_relative 'riq_obj'
2
+
3
+ module RIQ
4
+ # Users are represented by owners and contacts in your RelateIQ organization
5
+ class User < RIQObject
6
+ attr_accessor :name
7
+ attr_accessor :email
8
+
9
+ # (see RIQObject#node)
10
+ def node
11
+ "users/#{@id}"
12
+ end
13
+
14
+ # (see RIQObject#data)
15
+ def data
16
+ {
17
+ id: @id,
18
+ name: @name,
19
+ email: @email
20
+ }
21
+ end
22
+
23
+ private
24
+ def init(obj = nil)
25
+ unless obj.nil?
26
+ @id = obj[:id]
27
+ @name = obj[:name]
28
+ @email = obj[:email]
29
+ else
30
+ @id = nil
31
+ @name = nil
32
+ @email = nil
33
+ end
34
+ self
35
+ end
36
+ end
37
+
38
+ class << self
39
+ # Convenience method to create new Users
40
+ # @param id [String, nil] create a blank User object or
41
+ # fetch an existing one by id.
42
+ # @return [User]
43
+ def user(id = nil)
44
+ RIQ::User.new(id)
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ # lib = File.expand_path('../lib', __FILE__)
3
+ # $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'riq'
7
+ spec.version = '1.0.2'
8
+ spec.authors = ['David Brownman']
9
+ spec.email = ['david@relateiq.com']
10
+ spec.homepage = "https://github.com/relateiq/ruby-sdk"
11
+ spec.summary = 'Ruby RIQ API client'
12
+ spec.description = 'Full featured ruby client for interacting with the RelateIQ API'
13
+ spec.license = 'MIT'
14
+ spec.files = `git ls-files -z`.split("\x0")
15
+ # spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
16
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
17
+ spec.require_paths = ['lib']
18
+
19
+ # 2.0 is min becuase use of refinements
20
+ spec.required_ruby_version = '>= 2.0.0'
21
+ # spec.post_install_message = 'The power of relationship intelligence is in your hands!'
22
+
23
+ # prod dependencies
24
+ spec.add_dependency 'httparty', '0.13.3'
25
+
26
+ # dev dependencies
27
+ spec.add_development_dependency 'bundler', '~> 1.7'
28
+ # spec.add_development_dependency 'rake', '~> 10.0'
29
+ end
@@ -0,0 +1,4 @@
1
+ # testing!
2
+
3
+ # require everything to run all tests
4
+ Dir["#{__dir__}/*_*.rb"].each{|f| require_relative f}
@@ -0,0 +1,44 @@
1
+ require 'minitest/autorun'
2
+ require_relative '../lib/riq'
3
+
4
+ describe RIQ::Account do
5
+ before do
6
+ RIQ.init
7
+ # netflix account ID
8
+ @netflix = RIQ.account('54e6542fe4b01ad3b7362bc4')
9
+ @a = RIQ.account
10
+ @dat = RIQ.account({name: 'Glengarry', field_values: {'0' => 3}})
11
+ end
12
+
13
+ describe '#new' do
14
+ it 'should get account' do
15
+ @netflix.name.must_equal 'Netflix'
16
+ end
17
+
18
+ it 'should make blank account' do
19
+ @a.wont_be_nil
20
+ end
21
+
22
+ it 'should take a data hash' do
23
+ @dat = RIQ.account({name: 'David'})
24
+ @dat.name.wont_be_nil
25
+ end
26
+ end
27
+
28
+ describe '#save' do
29
+ it 'should create new account' do
30
+ @a.name = 'Delete Test Inc'
31
+ @a.field_value(2, '1')
32
+ @a.save
33
+
34
+ @a.id.wont_be_nil
35
+ end
36
+ end
37
+
38
+ describe "#field_value" do
39
+ it 'should fetch a field value' do
40
+ @netflix.field_value(2).wont_be_nil
41
+ @dat.field_value(0).wont_be_nil
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,72 @@
1
+ require 'minitest/autorun'
2
+ require_relative '../lib/riq'
3
+
4
+ describe RIQ::BatchManager do
5
+ before do
6
+ RIQ.init
7
+ @c = 0
8
+ end
9
+
10
+ describe '#accounts' do
11
+ it 'should get all accounts' do
12
+ accounts = RIQ.accounts
13
+ accounts.each do |a|
14
+ a.id.wont_be_nil
15
+ a.name.wont_be_nil
16
+ @c += 1
17
+ break if @c >= 20
18
+ end
19
+ @c.wont_equal 0
20
+ end
21
+ end
22
+
23
+ describe '#contacts' do
24
+ it 'should get all contacts' do
25
+ contacts = RIQ.contacts
26
+ contacts.each do |con|
27
+ con.id.wont_be_nil
28
+ @c += 1
29
+ break if @c >= 20
30
+ end
31
+ @c.wont_equal 0
32
+ end
33
+ end
34
+
35
+ describe '#lists' do
36
+ it 'should get all lists' do
37
+ lists = RIQ.lists
38
+ lists.each do |l|
39
+ l.id.wont_be_nil
40
+ l.title.wont_be_nil
41
+ l.fields.wont_be_nil
42
+ @c += 1
43
+ lic = 0
44
+ l.list_items.each do |li|
45
+ lic += 1
46
+ break if lic >= 5
47
+ end
48
+ lic.wont_equal 0
49
+ break if @c >= 20
50
+ end
51
+ @c.wont_equal 0
52
+ end
53
+ end
54
+
55
+ describe '#first' do
56
+ it 'should get one contact' do
57
+ c = nil
58
+ c = RIQ.contacts.first
59
+ c.must_be_instance_of RIQ::Contact
60
+ end
61
+ end
62
+
63
+ describe '#fetch_options' do
64
+ it 'should respect limits' do
65
+ b = RIQ.contacts({_ids: ['53a0bc44e4b0d7993870bcf4','5550fd35e4b0da744884f69d']})
66
+ b.each do |i|
67
+ @c += 1
68
+ end
69
+ @c.must_equal 2
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,62 @@
1
+ require 'minitest/autorun'
2
+ require_relative '../lib/riq'
3
+
4
+ describe RIQ::Contact do
5
+ before do
6
+ RIQ.init
7
+ # sammy's contact ID
8
+ @sammy = RIQ.contact('542b205be4b04cd81270dff9')
9
+ @c = RIQ.contact
10
+ @dat = RIQ.contact({properties: {'name' => ['david'], email: ['dab@relateiq.com']}})
11
+ end
12
+
13
+ describe '#new' do
14
+ it 'should get account' do
15
+ @sammy.name.must_include 'Sammy Nammari'
16
+ end
17
+
18
+ it 'make blank contact' do
19
+ @c.wont_be_nil
20
+ end
21
+ end
22
+
23
+ describe '#save' do
24
+ it 'should create new contact and delete it' do
25
+ @c.add(:name, 'Ron Mexico')
26
+ @c.save
27
+
28
+ @c.id.wont_be_nil
29
+
30
+ assert(@c.delete)
31
+ end
32
+ end
33
+
34
+ describe 'properties' do
35
+ it 'should add new emails only if they\'re new' do
36
+ @sammy.email.must_equal @sammy.add(:email, 'nammari@stanford.edu')
37
+
38
+ @sammy.email.wont_equal @sammy.add(:email, 'jammari@stanford.edu')
39
+ end
40
+
41
+ it 'should only take strings as properties' do
42
+ @c.add(:phone, '867-5309').wont_be_empty
43
+
44
+ begin
45
+ @c.add(:name, {value: 'Jenny'})
46
+ rescue RIQ::RIQError
47
+ nil.must_be_nil
48
+ else
49
+ 1.must_be_nil
50
+ end
51
+ end
52
+ end
53
+
54
+ describe '#upsert' do
55
+ it 'should upsert' do
56
+ # this could be better
57
+ @sammy.email.must_equal @sammy.upsert.email
58
+
59
+ # should add another assertion with the blank contact
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,56 @@
1
+ require 'minitest/autorun'
2
+ require_relative '../lib/riq'
3
+
4
+ describe RIQ::Event do
5
+ before do
6
+ RIQ.init
7
+ # sammy's contact ID
8
+ @e = RIQ.event
9
+ @ev = RIQ.event({subject: "My Sub", body: "Very intersting body", 'participantIds' => [{type: :email, value: 'fake@fakerelateiq.com'}]})
10
+ end
11
+
12
+ describe '#new' do
13
+ it 'should start blank' do
14
+ @e.subject.must_be_nil
15
+ end
16
+
17
+ it 'should take a hash' do
18
+ @ev.subject.wont_be_nil
19
+ @ev.participant_ids.wont_be_empty
20
+ end
21
+ end
22
+
23
+ describe '#save' do
24
+ it 'should fail without subject'do
25
+ begin
26
+ @e.save
27
+ rescue RIQ::HTTPError
28
+ nil.must_be_nil
29
+ else
30
+ 1.must_be_nil
31
+ end
32
+ end
33
+
34
+ it 'should save with data' do
35
+ @ev.save.must_be_kind_of Hash
36
+ end
37
+ end
38
+
39
+ describe '#add_participant' do
40
+ it 'should add a participant' do
41
+ p = {type: :email, value: 'good'}
42
+ @e.add_participant(p[:type], p[:value])
43
+ @e.participant_ids.must_include p
44
+ end
45
+
46
+ it 'should reject bad types' do
47
+ begin
48
+ @e.add_participant(:blarg, 'bad type')
49
+ rescue RIQ::RIQError
50
+ nil.must_be_nil
51
+ else
52
+ 1.must_be_nil
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,42 @@
1
+ require 'minitest/autorun'
2
+ require_relative '../lib/riq'
3
+
4
+ describe RIQ::ListItem do
5
+ before do
6
+ RIQ.init
7
+ lid = '54ca9b25e4b0b29d80ce4b4e'
8
+ @l = RIQ.list(lid)
9
+ @li = @l.list_items.first
10
+ @blank = @l.list_item
11
+ end
12
+
13
+ describe '#field_value' do
14
+ it 'should return a value' do
15
+ @li.field_value(0).wont_be_nil
16
+ @blank.field_value(0).must_be_nil
17
+ end
18
+ end
19
+
20
+ describe '#save' do
21
+ it 'should create and delete' do
22
+ @blank.field_value(0, 1)
23
+ @blank.contact_ids << RIQ.contacts.first.id
24
+ @blank.save
25
+ @blank.id.wont_be_nil
26
+
27
+ assert(@blank.delete)
28
+ end
29
+
30
+ it 'should update' do
31
+ start = @li.field_value(0)
32
+ if start == '1'
33
+ @li.field_value(0, 0)
34
+ else
35
+ @li.field_value(0, 1)
36
+ end
37
+ @li.save
38
+
39
+ @li.field_value(0).wont_equal start
40
+ end
41
+ end
42
+ end