Class: Yesgraph::YesGraphAPI

Inherits:
Object
  • Object
show all
Defined in:
lib/yesgraph.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(secret_key, base_url = BASE_URL) ⇒ YesGraphAPI

Returns a new instance of YesGraphAPI.



15
16
17
18
# File 'lib/yesgraph.rb', line 15

def initialize(secret_key, base_url = BASE_URL)
  @secret_key = secret_key
  @base_url = base_url
end

Instance Attribute Details

#base_urlObject (readonly)

Wrapper Class



14
15
16
# File 'lib/yesgraph.rb', line 14

def base_url
  @base_url
end

#secret_keyObject (readonly)

Wrapper Class



14
15
16
# File 'lib/yesgraph.rb', line 14

def secret_key
  @secret_key
end

Instance Method Details

#build_url(endpoint, url_args = {}) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/yesgraph.rb', line 27

def build_url(endpoint, url_args = {})
  base_url_cleaned = base_url.sub(%r{\/+$}, '')
  endpoint_cleaned = endpoint.sub(%r{^\/+}, '')
  url = [base_url_cleaned, endpoint_cleaned].join('/')
  url_args.delete_if { |_, v| v.to_s.strip.empty? }
  unless url_args.empty?
    args = URI.encode_www_form(url_args)
    url = "#{url}?#{args}"
  end
  url
end

#delete_address_book(user_id) ⇒ Object



140
141
142
143
144
145
146
147
148
# File 'lib/yesgraph.rb', line 140

def delete_address_book(user_id)
  # Wrapped method for DELETE /address-book/:user_id endpoint
  #
  # Documentation - https://docs.yesgraph.com/docs/address-book#section-delete-address-bookuser_id

  user_id = CGI.escape(user_id.to_s)
  endpoint = "/address-book/#{user_id}"
  request(:delete, endpoint)
end

#get_address_book(user_id, filter_suggested_seen: nil, filter_existing_users: nil, filter_invites_sent: nil, promote_existing_users: nil, promote_matching_domain: nil, filter_blank_names: nil, limit: nil) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/yesgraph.rb', line 111

def get_address_book(user_id, filter_suggested_seen: nil,
                     filter_existing_users: nil,
                     filter_invites_sent: nil,
                     promote_existing_users: nil,
                     promote_matching_domain: nil,
                     filter_blank_names: nil,
                     limit: nil)
  # Wrapped method for GET of /address-book endpoint
  #
  # Documentation - https://docs.yesgraph.com/docs/address-book#section-get-address-bookuser_id

  raise('`limit` param is not an int') unless limit.nil? ||
                                              (limit.is_a? Integer)

  urlargs = {
    'filter_suggested_seen' => filter_suggested_seen,
    'filter_existing_users' => filter_existing_users,
    'filter_invites_sent' => filter_invites_sent,
    'filter_blank_names' => filter_blank_names,
    'promote_existing_users' => promote_existing_users,
    'promote_matching_domain' => promote_matching_domain,
    'limit' => limit
  }

  user_id = CGI.escape(user_id.to_s)
  endpoint = "/address-book/#{user_id}"
  request(:get, endpoint, {}, urlargs)
end

#get_client_key(user_id) ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'lib/yesgraph.rb', line 62

def get_client_key(user_id)
  # Wrapped method for POST of /client-key endpoint
  #
  # Documentation - https://docs.yesgraph.com/docs/create-client-keys

  data = JSON.dump('user_id': user_id.to_s)
  result = request(:post, '/client-key', data)
  result['client_key']
end

#get_domain_emails(domain, page: nil, batch_size: nil) ⇒ Object



192
193
194
195
196
197
198
199
200
201
202
# File 'lib/yesgraph.rb', line 192

def get_domain_emails(domain, page: nil, batch_size: nil)
  # Wrapped method for GET of /domain-emails/<domain> endpoint
  #
  # Documentation - https://docs.yesgraph.com/docs/domain-emails/

  urlargs = { 'page' => page, 'batch_size' => batch_size }

  domain = CGI.escape(domain.to_s)
  endpoint = "/domain-emails/#{domain}"
  request(:get, endpoint, {}, urlargs)
end

#post_address_book(user_id, entries, source_type, source_name: nil, source_email: nil, filter_suggested_seen: nil, filter_existing_users: nil, filter_invites_sent: nil, filter_blank_names: nil, promote_existing_users: nil, promote_matching_domain: nil, backfill: nil, limit: nil) ⇒ Object



72
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
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/yesgraph.rb', line 72

def post_address_book(user_id, entries, source_type, source_name: nil,
                      source_email: nil, filter_suggested_seen: nil,
                      filter_existing_users: nil,
                      filter_invites_sent: nil,
                      filter_blank_names: nil,
                      promote_existing_users: nil,
                      promote_matching_domain: nil,
                      backfill: nil,
                      limit: nil)
  # Wrapped method for POST of /address-book endpoint
  #
  # Documentation - https://docs.yesgraph.com/docs/address-book

  source = { 'type' => source_type }
  source['name'] = source_name if source_name
  source['email'] = source_email if source_email

  raise('`limit` param is not an int') unless limit.nil? ||
                                              (limit.is_a? Integer)
  raise('`backfill` param is not an int') unless backfill.nil? ||
                                                 (backfill.is_a? Integer)

  data = {
    'user_id' => user_id.to_s,
    'filter_suggested_seen' => filter_suggested_seen,
    'filter_existing_users' => filter_existing_users,
    'filter_invites_sent' => filter_invites_sent,
    'filter_blank_names' => filter_blank_names,
    'promote_existing_users' => promote_existing_users,
    'promote_matching_domain' => promote_matching_domain,
    'source' => source,
    'entries' => entries,
    'limit' => limit,
    'backfill' => backfill
  }
  data = JSON.dump(data)
  request(:post, '/address-book', data)
end

#post_invites_accepted(entries) ⇒ Object



150
151
152
153
154
155
156
157
158
159
# File 'lib/yesgraph.rb', line 150

def post_invites_accepted(entries)
  # Wrapped method for POST of /invites-accepted endpoint
  #
  #  Documentation - https://docs.yesgraph.com/docs/invites-accepted

  raise('An entry list is required') unless entries && (entries.is_a? Array)
  data = { 'entries' => entries }
  data = JSON.dump(data)
  request(:post, '/invites-accepted', data)
end

#post_invites_sent(entries) ⇒ Object



161
162
163
164
165
166
167
168
169
170
# File 'lib/yesgraph.rb', line 161

def post_invites_sent(entries)
  # Wrapped method for POST of /invites-sent endpoint
  #
  # Documentation - https://docs.yesgraph.com/docs/invites-sent

  raise('An entry list is required') unless entries && (entries.is_a? Array)
  data = { 'entries' => entries }
  data = JSON.dump(data)
  request(:post, '/invites-sent', data)
end

#post_suggested_seen(entries) ⇒ Object



172
173
174
175
176
177
178
179
180
181
# File 'lib/yesgraph.rb', line 172

def post_suggested_seen(entries)
  # Wrapped method for POST of /suggested-seen endpoint
  #
  # Documentation - https://docs.yesgraph.com/docs/suggested-seen

  raise('An entry list is required') unless entries && (entries.is_a? Array)
  data = { 'entries' => entries }
  data = JSON.dump(data)
  request(:post, '/suggested-seen', data)
end

#post_users(users) ⇒ Object



183
184
185
186
187
188
189
190
# File 'lib/yesgraph.rb', line 183

def post_users(users)
  # Wrapped method for POST of users endpoint
  #
  # Documentation - https://docs.yesgraph.com/docs/users

  data = JSON.dump(users)
  request(:post, '/users', data)
end

#request(method, endpoint, data = {}, url_args = {}) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/yesgraph.rb', line 39

def request(method, endpoint, data = {}, url_args = {})
  # Builds and sends the complete request.
  headers = {
    'Authorization' => "Bearer #{secret_key}",
    'Content-Type' => 'application/json',
    'User-Agent' => user_agent
  }

  url = build_url(endpoint, url_args)
  resp = RestClient::Request.execute(method: method, url: url,
                                     payload: data.to_s,
                                     headers: headers)
  JSON.parse(resp.body)
end

#testObject



54
55
56
57
58
59
60
# File 'lib/yesgraph.rb', line 54

def test
  # Wrapped method for GET of /test endpoint
  #
  # Documentation - https://docs.yesgraph.com/docs/test

  request(:get, '/test')
end

#user_agentObject



20
21
22
23
24
25
# File 'lib/yesgraph.rb', line 20

def user_agent
  client_info = ['ruby-yesgraph', Yesgraph::VERSION].join('-')
  host_info = RbConfig::CONFIG['host']
  language_info = RbConfig::CONFIG['RUBY_VERSION_NAME']
  [client_info, host_info, language_info].join(' ')
end