riq 0.9.1 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,148 +0,0 @@
1
- require 'httparty'
2
- require 'json'
3
- require_relative 'error'
4
-
5
- module RIQ
6
- # HTTP client responsible for actually handling the HTTP requests.
7
- # @note Utility class to perform http requests. This shouldn't be
8
- # instantiated directy but used by children of RIQObject instead.
9
- class Client
10
- attr_accessor :cache
11
-
12
- def initialize(key, secret)
13
- raise 'Missing credentials' if key.nil? || secret.nil?
14
-
15
- @root_url = 'https://api.relateiq.com/v2'
16
-
17
- @auth = {username: key, password: secret}
18
- @headers = {
19
- 'Content-type' => 'application/json',
20
- 'Accept' => 'application/json'
21
- }
22
- @cache = {}
23
- end
24
-
25
- # for caching, used with #next ?
26
- # def store(endpoint, objects)
27
- # @cache[endpoint] = objects unless objects.nil?
28
- # end
29
-
30
- # Makes a GET request, used for fetching existing objects.
31
- # @return (see #request)
32
- def get(endpoint, options: nil)
33
- request(endpoint, :get, nil, options)
34
- end
35
-
36
- # Makes a POST request, used for creating new objects.
37
- # @return (see #request)
38
- def post(endpoint, body, options: nil)
39
- request(endpoint, :post, body, options)
40
- end
41
-
42
- # Makes a PUT request, used for updating existing objects.
43
- # @return (see #request)
44
- def put(endpoint, body, options: nil)
45
- request(endpoint, :put, body, options)
46
- end
47
-
48
- # Makes a DELETE request, used for deleting existing objects.
49
- # @return (see #request)
50
- def delete(endpoint, options: nil)
51
- request(endpoint, :delete, nil, options)
52
- end
53
-
54
- # I'm not sure why this would return an empty object instead of raising a notfound
55
- # or really, why it exists at all
56
- # def fetch(endpoint = nil, options: {})
57
- # begin
58
- # get(endpoint, options: options)
59
- # rescue NotFoundError
60
- # {}
61
- # end
62
- # end
63
-
64
- private
65
-
66
- # actually does the requesting
67
- # @return (see #process_response)
68
- def request(endpoint, method = :get, body = nil, options = nil)
69
- # there may be a better way to do this, but this is close
70
- url = "#{@root_url}/#{endpoint}"
71
- # pp "#{method} request to #{url} with body: #{body} and options: #{options}"
72
- if [:get, :delete].include? method
73
- resp = HTTParty.method(method).call(
74
- url,
75
- headers: @headers,
76
- basic_auth: @auth,
77
- query: options
78
- )
79
- elsif [:post, :put].include? method
80
- resp = HTTParty.method(method).call(
81
- url,
82
- headers: @headers,
83
- basic_auth: @auth,
84
- query: options,
85
- body: body
86
- )
87
- else
88
- # this shouldn't ever get hit?
89
- raise RIQError, 'Invalid method'
90
- end
91
-
92
- # HTTParty response object
93
- process_response(resp)
94
- # resp
95
- end
96
-
97
- # Checks for errors and returns the parsed object
98
- # @return [RIQObject] The result of the request
99
- def process_response(resp)
100
- # pp "processing #{resp}, code: #{resp.code}"
101
- if resp.code == 503
102
- raise NotImplementedError, 'This function is not currently supported by RelateIQ'
103
- elsif resp.code == 404
104
- raise NotFoundError, 'Object Not Found'
105
- elsif resp.code >= 400
106
- # Record Response Data
107
- raise HTTPError, resp
108
- end
109
- # 404 Object not found
110
- # if List, have them check if it is shared
111
- # 500
112
- # 422
113
- # 400 Bad Request - pass on internal message
114
- # 502 Bad Gateway - Reattempt
115
-
116
- # if resp.include? 'objects'
117
- # resp['objects']
118
- # else
119
- resp.parsed_response
120
- # end
121
- end
122
-
123
- # Configuration Methods
124
- # def fetchConfig
125
- # return get('configs')
126
- # end
127
- end
128
-
129
- class << self
130
- # @param key [String] RelateIQ API key
131
- # @param secret [String] RelateIQ API secret
132
- # @return [RIQ] The module, in case you want it.
133
- def init(key = nil, secret = nil)
134
- key ||= ENV['RIQ_TEST_API_KEY']
135
- secret ||= ENV['RIQ_TEST_API_SECRET']
136
-
137
- @@client = Client.new(key, secret)
138
- self
139
- end
140
-
141
- # Always use RIQ.client to retrieve the client object.
142
- # @return [Client] The client object
143
- def client
144
- raise RIQError, 'Client not initialized' unless @@client
145
- @@client
146
- end
147
- end
148
- end
@@ -1,158 +0,0 @@
1
- require_relative 'riq_obj'
2
- using RIQExtensions
3
-
4
- module RIQ
5
- # Contacts represent people in an Organization’s address book.
6
- class Contact < RIQObject
7
- attr_accessor :properties
8
-
9
- # (see RIQObject#node)
10
- def node
11
- self.class.node(@id)
12
- end
13
-
14
- # (see RIQObject.node)
15
- def self.node(id = nil)
16
- "contacts/#{id}"
17
- end
18
-
19
- # (see RIQObject#data)
20
- def data
21
- {
22
- id: @id,
23
- properties: @properties
24
- # modified_date: @modified_date
25
- }
26
- end
27
-
28
- # @!macro [new] prop
29
- # @return [Array] all of the values for $0
30
- def name
31
- get_prop(:name)
32
- end
33
-
34
- # @macro prop
35
- def phone
36
- get_prop(:phone)
37
- end
38
-
39
- # @macro prop
40
- def email
41
- get_prop(:email)
42
- end
43
-
44
- # @macro prop
45
- def address
46
- get_prop(:address)
47
- end
48
-
49
- # Adds property with correct scaffolding
50
- # @param prop [Symbol] Type Property to add, such as :name or :email
51
- # @param val [String, Array] Value(s) to add
52
- # @return [Array] Values for that property after the add
53
- def add(prop, val)
54
- # validate(prop)
55
- prop = prop.to_sym
56
- @properties[prop] = [] unless @properties.include? prop
57
-
58
- if val.is_a? Array
59
- val.each do |i|
60
- add(prop, i)
61
- end
62
- return
63
- end
64
-
65
- raise RIQError, 'Values must be strings' unless val.is_a? String
66
-
67
- # don't add duplicate
68
- if @properties[prop].select{|p| p[:value] == val}.empty?
69
- @properties[prop] << {value: val, metadata: {}}
70
- end
71
- send(prop)
72
- end
73
-
74
- # Removes property from hash
75
- # @param prop [Symbol] Type Property to remove, such as :name or :email
76
- # @param val [String, Array] Value(s) to remove
77
- # @return [Array] Values for that property after the remove
78
- def remove(prop, val)
79
- # validate(prop)
80
- prop = prop.to_sym
81
-
82
- if val.is_a? Array
83
- val.each do |i|
84
- remove(prop, i)
85
- end
86
- end
87
-
88
- if @properties.include? prop
89
- @properties[prop] = @properties[prop].reject{|p| p[:value] == val}
90
- end
91
- send(prop)
92
- end
93
-
94
- # Edits an existing object based on matching email(s) or saves a new object
95
- # @see RIQObject#save
96
- def upsert
97
- save({_upsert: 'email'})
98
- end
99
-
100
- # @param prop [Symbol] The property to fetch. One of [:name, :phone, :email, :address]
101
- # @param val [String] The value to get info on, such as 'name@domain.com'
102
- # @return [Hash] metadata and other info about a given property
103
- def info(prop, val)
104
- # validate(prop)
105
-
106
- @properties[prop].select{|p| p[:value] == val}.first
107
- end
108
-
109
- private
110
-
111
- def init(obj = nil)
112
- unless obj.nil?
113
- @id = obj[:id]
114
- @modified_date = obj[:modified_date].cut_milis if obj[:modified_date]
115
- @properties = {}
116
- obj[:properties].each do |k,v|
117
- raise RIQError, "Properties must be arrays, #{k} wasn't" if !v.is_a?(Array)
118
-
119
- v.each do |i|
120
- unless i.is_a?(Hash)
121
- add(k, i)
122
- else
123
- @properties[k] = [] unless @properties.include?(k)
124
- @properties[k] << i
125
- end
126
- end
127
- end
128
- else
129
- @id = nil
130
- @properties = {}
131
- end
132
- self
133
- end
134
-
135
- # unused because we actually put all sorts of stuff in there
136
- def validate(prop)
137
- raise RIQError, %q(Invalid property. Use [:name | :phone | :email | :address] instead) unless [:name, :phone, :email, :address].include? prop
138
- end
139
-
140
- def get_prop(prop)
141
- if @properties.include? prop
142
- @properties[prop].map{|p| p[:value]}
143
- else
144
- []
145
- end
146
- end
147
- end
148
-
149
- class << self
150
- # Convenince method to create new Contacts
151
- # @param id [String, nil] create a blank Contact object or
152
- # fetch an existing one by id.
153
- # @return [Contact]
154
- def contact(id = nil)
155
- Contact.new(id)
156
- end
157
- end
158
- end
@@ -1,56 +0,0 @@
1
- module RIQ
2
- # Base exception class for our errors.
3
- class RIQError < StandardError
4
- end
5
-
6
- # Hasn't been implemented yet. Doesn't get used often.
7
- class NotImplementedError < RIQError
8
- end
9
-
10
- # Raised when an objectid fetch doesn't return anything.
11
- class NotFoundError < RIQError
12
- end
13
-
14
- # Main error that includes info about the request and what went wrong.
15
- class HTTPError < RIQError
16
- attr_accessor :code
17
- attr_accessor :message
18
- attr_accessor :response
19
-
20
- def initialize(resp)
21
- # build response message
22
- message = "\n[#{resp.code}] #{resp.message} : "
23
- unless resp.parsed_response.nil?
24
- begin
25
- m = resp.parsed_response.fetch('errorMessage', 'no message')
26
- rescue
27
- # parse returned html
28
- reg = resp.parsed_response.match(/<pre>(.*)<\/pre>/)
29
- unless reg.nil?
30
- m = reg[1].strip
31
- else
32
- m = 'no message'
33
- end
34
- end
35
- message += "<#{m}>"
36
- end
37
-
38
- # pull out request info
39
- req = resp.request
40
- unless req.nil?
41
- message += "\n Request: #{req.http_method} #{req.last_uri.to_s}"
42
- unless req.raw_body.nil?
43
- message += "\n #{req.raw_body}"
44
- end
45
- end
46
-
47
- @message = message
48
- @response = resp
49
- end
50
-
51
- # used to print the error nicely
52
- def to_s
53
- @message || super
54
- end
55
- end
56
- end
@@ -1,165 +0,0 @@
1
- require_relative 'riq_obj'
2
- using RIQExtensions
3
-
4
- module RIQ
5
- # Events represent interactions involving a Contact associated with a List Item.
6
- class Event < RIQObject
7
- attr_accessor :subject
8
- attr_accessor :body
9
- # attr_reader :participant_ids
10
-
11
- # (see RIQObject#node)
12
- def node
13
- self.class.node
14
- end
15
-
16
- # (see RIQObject#node)
17
- def self.node
18
- "events"
19
- end
20
-
21
- # (see RIQObject#data)
22
- def data
23
- {
24
- subject: @subject,
25
- body: @body,
26
- participant_ids: @participant_ids
27
- }
28
- end
29
-
30
- # @param type [Symbol] One of :email or :phone
31
- # @param value [String] An email or phone number for the contact
32
- def add_participant(type, value)
33
- raise RIQError, 'Type must be :email or :phone' unless [:email, :phone].include?(type)
34
- @participant_ids << {type: type, value: value}
35
- end
36
-
37
- # @return [Array] Immutable copy of participant_ids
38
- def participant_ids
39
- @participant_ids.dup
40
- end
41
-
42
- # @return [Hash] Success message, if successul.
43
- def save
44
- # there are no options to pass for event save
45
- @client.put(node, payload, options: nil)
46
- {status: 204, message: 'No Content'}
47
- end
48
-
49
- private
50
- def init(obj = nil)
51
- unless obj.nil?
52
- @subject = obj[:subject]
53
- @body = obj[:body]
54
- @participant_ids = []
55
- obj[:participant_ids].each{|o| add_participant(o[:type], o[:value])}
56
- else
57
- @subject = nil
58
- @body = nil
59
- @participant_ids = []
60
- end
61
- self
62
- end
63
- end
64
-
65
- class << self
66
- # Convenience method to create new Events
67
- # @param obj [Hash] Info to parse into event
68
- # @return [Event]
69
- def event(obj = nil)
70
- Event.new(obj)
71
- end
72
- end
73
- end
74
-
75
- # @id = nil
76
- # @modifiedDate = nil
77
- # @participantIds = nil
78
- # @subject = nil
79
- # @body = nil
80
-
81
- # def initialize(
82
- # _id=nil,
83
- # subject=nil,
84
- # body=nil,
85
- # participantIds=nil,
86
- # data:nil,
87
- # hash:nil
88
- # )
89
- # if !data.nil?
90
- # parse(data)
91
- # elsif !hash.nil?
92
- # process_hash(hash)
93
- # elsif !id(_id).nil?
94
- # Event::get(self)
95
- # end
96
- # participantIds(participantIds)
97
- # subject(subject)
98
- # body(body)
99
- # end
100
-
101
- # class << self
102
- # def node
103
- # 'events'
104
- # end
105
-
106
- # def factory(data = nil, hash = nil)
107
- # return Event.new(data, hash)
108
- # end
109
-
110
- # def update(instance, options = {})
111
- # response = Client::put(self.endpoint(), instance.payload(), options)
112
- # return instance
113
- # end
114
- # end
115
-
116
- # def parse(data)
117
- # obj = JSON.parse(data.body)
118
- # id(obj['id'])
119
- # modifiedDate(obj['modifiedDate'])
120
- # end
121
-
122
- # def process_hash(hash)
123
- # id(hash['id'])
124
- # modifiedDate(hash['modifiedDate'])
125
- # end
126
-
127
- # # Data Payload
128
- # def payload()
129
- # payload = {:participantIds => @participantIds,
130
- # :subject => subject(),
131
- # :body => body()
132
- # }
133
- # if modifiedDate() then
134
- # payload['modifiedDate'] = modifiedDate()
135
- # end
136
- # payload['id'] = id() if id()
137
- # return payload
138
- # end
139
-
140
- # # Hybrid
141
- # def id(value = nil)
142
- # @id = value unless value.nil?
143
- # end
144
-
145
- # def modifiedDate(value = nil)
146
- # @modifiedDate = value unless value.nil?
147
- # end
148
-
149
- # def subject(value = nil)
150
- # @subject = value unless value.nil?
151
- # end
152
-
153
- # # value should either be a string or a list of strings
154
- # # returns: if multiple values exist for key, a list of strings of values
155
- # # if a single value exists, return the single string value (not in a list)
156
- # # if no values exist, return nil
157
- # def participantIds(value = nil)
158
- # # set value if passed in
159
- # @participantIds = value unless value.nil?
160
- # end
161
-
162
- # def body(value=nil)
163
- # @body = value unless value.nil?
164
- # end
165
- # end