Skip to content

Commit

Permalink
Merge pull request #18 from messagebird/add-contacts
Browse files Browse the repository at this point in the history
Add contacts
  • Loading branch information
Artur Khabibullin authored Sep 10, 2018
2 parents 04c1e28 + 003d457 commit d500b21
Show file tree
Hide file tree
Showing 11 changed files with 407 additions and 12 deletions.
54 changes: 54 additions & 0 deletions examples/contact_create.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/usr/bin/env ruby

$:.unshift File.expand_path(File.dirname(__FILE__) + '/../lib/')
require 'messagebird'

# ACCESS_KEY = 'YOUR KEY HERE'
# PHONE_NUMBER = 'YOUR PHONE NUMBER HERE'

unless defined?(ACCESS_KEY)
puts 'You need to set an ACCESS_KEY constant in this file'
exit 1
end

unless defined?(PHONE_NUMBER)
puts 'You need to set an PHONE_NUMBER constant in this file'
exit 1
end

begin
# Create a MessageBird client with the specified ACCESS_KEY.
client = MessageBird::Client.new(ACCESS_KEY)

# Create a new Contact object.
contact = client.contact_create(PHONE_NUMBER, { :firstName => 'Foo', :lastName => 'Bar'})

# Print the object information.
puts
puts " Contact :"
puts
puts " id : #{contact.id}"
puts " href : #{contact.href}"
puts " msisdn : #{contact.msisdn}"
puts " firstName : #{contact.firstName}"
puts " lastName : #{contact.lastName}"
puts " groups : #{contact.groups.href}" # contact.groups.totalCount is also available.
puts " messages : #{contact.messages.href}" # contact.messages.totalCount is also available.
puts " custom1 : #{contact.customDetails.custom1}"
puts " custom2 : #{contact.customDetails.custom2}"
puts " custom3 : #{contact.customDetails.custom3}"
puts " custom4 : #{contact.customDetails.custom4}"
puts

rescue MessageBird::ErrorException => ex
puts
puts 'An error occurred while creating a contact:'
puts

ex.errors.each do |error|
puts " code : #{error.code}"
puts " description : #{error.description}"
puts " parameter : #{error.parameter}"
puts
end
end
61 changes: 61 additions & 0 deletions examples/contact_list.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/usr/bin/env ruby

$:.unshift File.expand_path(File.dirname(__FILE__) + '/../lib/')
require 'messagebird'

ACCESS_KEY = 'YOUR KEY HERE'

unless defined?(ACCESS_KEY)
puts 'You need to set an ACCESS_KEY constant in this file'
exit 1
end

begin
# Create a MessageBird client with the specified ACCESS_KEY.
client = MessageBird::Client.new(ACCESS_KEY)

# Fetch the Contact list with pagination options (skip the first 5 objects and take 10).
limit = 10
offset = 5
contacts = client.contact_list(limit, offset)

# Print the object information.
puts
puts "The following information was returned as a Contact list:"
puts
puts " count : #{contacts.count}"
puts " limit : #{contacts.limit}"
puts " offset : #{contacts.offset}"
puts " totalCount : #{contacts.totalCount}"
puts " links : #{contacts.links}"

unless contacts.items.empty?
contact = contacts[0] # Equivalent to contacts.items[0]

puts " Contact :"
puts " id : #{contact.id}"
puts " href : #{contact.href}"
puts " msisdn : #{contact.msisdn}"
puts " firstName : #{contact.firstName}"
puts " lastName : #{contact.lastName}"
puts " groups : #{contact.groups.href}" # contact.groups.totalCount is also available.
puts " messages : #{contact.messages.href}" # contact.messages.totalCount is also available.
puts " custom1 : #{contact.customDetails.custom1}"
puts " custom2 : #{contact.customDetails.custom2}"
puts " custom3 : #{contact.customDetails.custom3}"
puts " custom4 : #{contact.customDetails.custom4}"

end

rescue MessageBird::ErrorException => ex
puts
puts 'An error occurred while listing your contacts:'
puts

ex.errors.each do |error|
puts " code : #{error.code}"
puts " description : #{error.description}"
puts " parameter : #{error.parameter}"
puts
end
end
4 changes: 4 additions & 0 deletions lib/messagebird.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ module MessageBird

require 'messagebird/balance'
require 'messagebird/client'
require 'messagebird/contact'
require 'messagebird/error'
require 'messagebird/group_reference'
require 'messagebird/hlr'
require 'messagebird/http_client'
require 'messagebird/message_reference'
require 'messagebird/verify'
require 'messagebird/message'
require 'messagebird/voicemessage'
31 changes: 29 additions & 2 deletions lib/messagebird/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
require 'uri'

require 'messagebird/balance'
require 'messagebird/contact'
require 'messagebird/error'
require 'messagebird/hlr'
require 'messagebird/http_client'
require 'messagebird/verify'
require 'messagebird/list'
require 'messagebird/lookup'
require 'messagebird/message'
require 'messagebird/verify'
require 'messagebird/voicemessage'
require 'messagebird/lookup'

module MessageBird
class ErrorException < StandardError
Expand All @@ -32,6 +34,8 @@ def initialize(access_key = nil, http_client = nil)

def request(method, path, params={})
response_body = @http_client.request(method, path, params)
return if response_body.empty?

json = JSON.parse(response_body)

# If the request returned errors, create Error objects and raise.
Expand Down Expand Up @@ -134,5 +138,28 @@ def lookup_hlr(phoneNumber, params={})
HLR.new(request(:get, "lookup/#{phoneNumber}/hlr", params))
end

def contact_create(phoneNumber, params={})
Contact.new(request(
:post,
'contacts',
params.merge({ :msisdn => phoneNumber.to_s })))
end

def contact(id)
Contact.new(request(:get, "contacts/#{id}"))
end

def contact_delete(id)
request(:delete, "contacts/#{id}")
end

def contact_update(id, params={})
request(:patch, "contacts/#{id}", params)
end

def contact_list(limit = 0, offset = 0)
List.new(Contact, request(:get, "contacts?limit=#{limit}&offset=#{offset}"))
end

end
end
31 changes: 31 additions & 0 deletions lib/messagebird/contact.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
require 'messagebird/base'
require 'messagebird/custom_details'
require 'messagebird/group_reference'
require 'messagebird/message_reference'

module MessageBird
class Contact < MessageBird::Base
attr_accessor :id, :href, :msisdn, :firstName, :lastName, :customDetails,
:groups, :messages, :createdDatetime, :updatedDatetime

def customDetails=(value)
@customDetails = MessageBird::CustomDetails.new(value)
end

def groups=(value)
@groups = MessageBird::GroupReference.new(value)
end

def messages=(value)
@messages = MessageBird::MessageReference.new(value)
end

def createdDatetime=(value)
@createdDatetime = value_to_time(value)
end

def updatedDatetime=(value)
@updatedDatetime = value_to_time(value)
end
end
end
8 changes: 8 additions & 0 deletions lib/messagebird/custom_details.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require 'messagebird/base'

module MessageBird
class CustomDetails < MessageBird::Base
# CustomDetails holds free-input fields for the Contact object.
attr_accessor :custom1, :custom2, :custom3, :custom4
end
end
7 changes: 7 additions & 0 deletions lib/messagebird/group_reference.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'messagebird/base'

module MessageBird
class GroupReference < MessageBird::Base
attr_accessor :href, :totalCount
end
end
27 changes: 17 additions & 10 deletions lib/messagebird/http_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,22 @@ def request(method, path, params={}, check_json=true)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true

# Construct the HTTP GET or POST request.
request = prepare_request(method, uri, params)

# Execute the request and fetch the response.
response = http.request(request)

assert_valid_response_code(response.code.to_i)
assert_json_response_type(response['Content-Type']) unless check_json

response.body
end

def prepare_request(method, uri, params={})
# Construct the HTTP request.
case method
when :delete
request = Net::HTTP::Delete.new(uri.request_uri)
when :get
request = Net::HTTP::Get.new(uri.request_uri)
when :post
Expand All @@ -36,16 +50,9 @@ def request(method, path, params={}, check_json=true)
request['Authorization'] = "AccessKey #{@access_key}"
request['User-Agent'] = "MessageBird/ApiClient/#{CLIENT_VERSION} Ruby/#{RUBY_VERSION}"

# If present, add the HTTP POST parameters.
request.set_form_data(params) if method == :post && !params.empty?

# Execute the request and fetch the response.
response = http.request(request)

assert_valid_response_code(response.code.to_i)
assert_json_response_type(response['Content-Type']) unless check_json

response.body
request
end

# Throw an exception if the response code is not one we expect from the
Expand All @@ -68,4 +75,4 @@ def assert_json_response_type(content_type)

end

end
end
23 changes: 23 additions & 0 deletions lib/messagebird/list.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require 'messagebird/base'

class List < MessageBird::Base

attr_accessor :offset, :limit, :count, :totalCount, :links, :items

# type will be used to create objects for the items, e.g.
# List.new(Contact, {}).
def initialize(type, json)
@type = type

super(json)
end

def items=(value)
@items = value.map { |i| @type.new i }
end

def [](index)
@items[index]
end

end
7 changes: 7 additions & 0 deletions lib/messagebird/message_reference.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'messagebird/base'

module MessageBird
class MessageReference < MessageBird::Base
attr_accessor :href, :totalCount
end
end
Loading

0 comments on commit d500b21

Please sign in to comment.