textmagic-ruby 2.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.
Files changed (70) hide show
  1. checksums.yaml +15 -0
  2. data/.gitignore +11 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +4 -0
  5. data/Gemfile +9 -0
  6. data/LICENSE.txt +21 -0
  7. data/Makefile +8 -0
  8. data/README.md +41 -0
  9. data/Rakefile +10 -0
  10. data/conf/cacert.pem +3988 -0
  11. data/examples/bulk_examples.rb +24 -0
  12. data/examples/chat_examples.rb +30 -0
  13. data/examples/contact_examples.rb +116 -0
  14. data/examples/custom_field_examples.rb +78 -0
  15. data/examples/invoice_examples.rb +26 -0
  16. data/examples/list_examples.rb +98 -0
  17. data/examples/message_examples.rb +75 -0
  18. data/examples/number_examples.rb +72 -0
  19. data/examples/reply_examples.rb +32 -0
  20. data/examples/schedule_examples.rb +44 -0
  21. data/examples/senderid_examples.rb +52 -0
  22. data/examples/session_examples.rb +51 -0
  23. data/examples/subaccount_examples.rb +53 -0
  24. data/examples/template_examples.rb +61 -0
  25. data/examples/unsubscriber_examples.rb +56 -0
  26. data/examples/user_examples.rb +105 -0
  27. data/lib/textmagic-ruby.rb +31 -0
  28. data/lib/textmagic-ruby/rest/bulks.rb +89 -0
  29. data/lib/textmagic-ruby/rest/chats.rb +147 -0
  30. data/lib/textmagic-ruby/rest/client.rb +128 -0
  31. data/lib/textmagic-ruby/rest/contacts.rb +174 -0
  32. data/lib/textmagic-ruby/rest/custom_fields.rb +120 -0
  33. data/lib/textmagic-ruby/rest/errors.rb +14 -0
  34. data/lib/textmagic-ruby/rest/instance_resource.rb +33 -0
  35. data/lib/textmagic-ruby/rest/invoices.rb +73 -0
  36. data/lib/textmagic-ruby/rest/list_resource.rb +64 -0
  37. data/lib/textmagic-ruby/rest/lists.rb +166 -0
  38. data/lib/textmagic-ruby/rest/messages.rb +172 -0
  39. data/lib/textmagic-ruby/rest/numbers.rb +164 -0
  40. data/lib/textmagic-ruby/rest/paginate_resource.rb +20 -0
  41. data/lib/textmagic-ruby/rest/replies.rb +85 -0
  42. data/lib/textmagic-ruby/rest/scheduleds.rb +91 -0
  43. data/lib/textmagic-ruby/rest/senderids.rb +114 -0
  44. data/lib/textmagic-ruby/rest/sessions.rb +109 -0
  45. data/lib/textmagic-ruby/rest/subaccounts.rb +135 -0
  46. data/lib/textmagic-ruby/rest/templates.rb +107 -0
  47. data/lib/textmagic-ruby/rest/unsubscribers.rb +85 -0
  48. data/lib/textmagic-ruby/rest/users.rb +202 -0
  49. data/lib/textmagic-ruby/rest/utils.rb +48 -0
  50. data/lib/textmagic-ruby/rest/version.rb +5 -0
  51. data/spec/rest/client_spec.rb +57 -0
  52. data/spec/rest/contact_spec.rb +36 -0
  53. data/spec/rest/custom_fields_spec.rb +36 -0
  54. data/spec/rest/instance_resource_spec.rb +17 -0
  55. data/spec/rest/invoice_spec.rb +56 -0
  56. data/spec/rest/lists_spec.rb +35 -0
  57. data/spec/rest/message_spec.rb +40 -0
  58. data/spec/rest/numbers_spec.rb +66 -0
  59. data/spec/rest/reply_spec.rb +43 -0
  60. data/spec/rest/scheduled_spec.rb +43 -0
  61. data/spec/rest/senderid_spec.rb +39 -0
  62. data/spec/rest/session_spec.rb +43 -0
  63. data/spec/rest/subaccount_spec.rb +39 -0
  64. data/spec/rest/template_spec.rb +36 -0
  65. data/spec/rest/unsubscriber_spec.rb +40 -0
  66. data/spec/rest/user_spec.rb +92 -0
  67. data/spec/rest/utils_spec.rb +57 -0
  68. data/spec/spec_helper.rb +15 -0
  69. data/textmagic-ruby.gemspec +30 -0
  70. metadata +165 -0
@@ -0,0 +1,31 @@
1
+ require 'uri'
2
+ require 'cgi'
3
+ require 'net/http'
4
+ require 'net/https'
5
+ require 'multi_json'
6
+ require 'openssl'
7
+ require 'yaml'
8
+
9
+ require 'textmagic-ruby/rest/utils'
10
+ require 'textmagic-ruby/rest/list_resource'
11
+ require 'textmagic-ruby/rest/instance_resource'
12
+ require 'textmagic-ruby/rest/paginate_resource'
13
+ require 'textmagic-ruby/rest/messages'
14
+ require "textmagic-ruby/rest/version"
15
+ require 'textmagic-ruby/rest/client'
16
+ require 'textmagic-ruby/rest/errors'
17
+ require 'textmagic-ruby/rest/contacts'
18
+ require 'textmagic-ruby/rest/lists'
19
+ require 'textmagic-ruby/rest/custom_fields'
20
+ require 'textmagic-ruby/rest/unsubscribers'
21
+ require 'textmagic-ruby/rest/bulks'
22
+ require 'textmagic-ruby/rest/chats'
23
+ require 'textmagic-ruby/rest/scheduleds'
24
+ require 'textmagic-ruby/rest/sessions'
25
+ require 'textmagic-ruby/rest/templates'
26
+ require 'textmagic-ruby/rest/invoices'
27
+ require 'textmagic-ruby/rest/users'
28
+ require 'textmagic-ruby/rest/numbers'
29
+ require 'textmagic-ruby/rest/senderids'
30
+ require 'textmagic-ruby/rest/subaccounts'
31
+ require 'textmagic-ruby/rest/replies'
@@ -0,0 +1,89 @@
1
+ module Textmagic
2
+ module REST
3
+ class Bulks < ListResource
4
+ ##
5
+ # Get bulks session by ID.
6
+ #
7
+ # uid:: Bulk session ID. Required.
8
+ #
9
+ # Example:
10
+ #
11
+ # @bulks = client.bulks.get 111
12
+ #
13
+ def get(uid)
14
+ super
15
+ end
16
+ ##
17
+ # Get all bulk sending sessions.
18
+ #
19
+ # The following *params* keys are supported:
20
+ #
21
+ # page:: Fetch specified results page. Defaults 1
22
+ #
23
+ # limit:: How many results on page. Defaults 10
24
+ #
25
+ # Example:
26
+ #
27
+ # @bulks = client.bulks.list
28
+ #
29
+ def list(params={})
30
+ [:search, 'search'].each do |search|
31
+ params.delete search
32
+ end
33
+ super params
34
+ end
35
+
36
+ ##
37
+ # Creating is not supported.
38
+ #
39
+ def create(params={})
40
+ raise '`create` method is not supported for this resource.'
41
+ end
42
+
43
+ ##
44
+ # Updating is not supported.
45
+ #
46
+ def update(uid, params={})
47
+ raise '`update` method is not supported for this resource.'
48
+ end
49
+
50
+ ##
51
+ # Deleting is not supported.
52
+ #
53
+ def delete(uid)
54
+ raise '`delete` method is not supported for this resource.'
55
+ end
56
+ end
57
+
58
+ ##
59
+ # A Bulk message session resource.
60
+ #
61
+ # ==== @id
62
+ #
63
+ # ==== @status
64
+ #
65
+ # ==== @items_processed
66
+ #
67
+ # ==== @items_total
68
+ #
69
+ # ==== @created_at
70
+ #
71
+ # ==== @text
72
+ #
73
+ # ==== @session
74
+ #
75
+ # Hash like this
76
+ # {
77
+ # "id" => "34435949",
78
+ # "startTime" => "2015-05-01T21:30:00+0000",
79
+ # "text" => "error",
80
+ # "source" => "O",
81
+ # "referenceId" => "O_xxx_cb5e100e5a9a3e7f6d1fd97512215282_10580074905542fc46b9f157.39758261",
82
+ # "price" => 0.03,
83
+ # "numbersCount" => 1
84
+ # }
85
+ #
86
+ class Bulk < InstanceResource
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,147 @@
1
+ module Textmagic
2
+ module REST
3
+ class Chats < ListResource
4
+ ##
5
+ # Get all user chats.
6
+ #
7
+ # The following *params* keys are supported:
8
+ #
9
+ # page:: Fetch specified results page. Defaults 1
10
+ #
11
+ # limit:: How many results on page. Defaults 10
12
+ #
13
+ # Example:
14
+ #
15
+ # @chats = client.chats.list
16
+ #
17
+ def list(params={})
18
+ [:search, 'search'].each do |search|
19
+ params.delete search
20
+ end
21
+ super params
22
+ end
23
+
24
+ ##
25
+ # Fetch messages from chat with specified phone number.
26
+ #
27
+ # phone:: Phone number in E.164 format. Required.
28
+ #
29
+ # The following *params* keys are supported:
30
+ #
31
+ # page:: Fetch specified results page. Defaults 1
32
+ #
33
+ # limit:: How many results on page. Defaults 10
34
+ #
35
+ # Example:
36
+ #
37
+ # @chat_messages = client.chats.get_by_phone 99990000
38
+ #
39
+ def get_by_phone(phone, params={})
40
+ response = @client.get "#{@path}/#{phone}", params
41
+ PaginateResource.new "#{@path}", @client, response, Textmagic::REST::ChatMessage
42
+ end
43
+
44
+ ##
45
+ # Creating is not supported.
46
+ #
47
+ def create(params={})
48
+ raise '`create` method is not supported for this resource.'
49
+ end
50
+
51
+ ##
52
+ # Getting by ID is not supported. Try get_by_phone instead.
53
+ #
54
+ def get(uid)
55
+ raise '`get` method by ID is not supported for this resource, use `get_by_phone`.'
56
+ end
57
+
58
+ ##
59
+ # Updating is not supported.
60
+ #
61
+ def update(uid, params={})
62
+ raise '`update` method is not supported for this resource.'
63
+ end
64
+
65
+ ##
66
+ # Deleting is not supported.
67
+ #
68
+ def delete(uid)
69
+ raise '`delete` method is not supported for this resource.'
70
+ end
71
+ end
72
+
73
+ ##
74
+ # A Chat resource.
75
+ #
76
+ # ==== @id
77
+ #
78
+ # ==== @phone
79
+ #
80
+ # ==== @contact
81
+ #
82
+ # Hash like this
83
+ # {
84
+ # "id": 4329702,
85
+ # "first_name": "Jonh",
86
+ # "last_name": "Doe",
87
+ # "company_name": "",
88
+ # "phone": "19025555555",
89
+ # "email": "",
90
+ # "country": {
91
+ # "id": "CA",
92
+ # "name": "Canada"
93
+ # },
94
+ # "custom_fields": [
95
+ # {
96
+ # "value": "1970-01-01",
97
+ # "id": 1111,
98
+ # "name": "Birthday",
99
+ # "createdAt": "2015-04-10T06:51:02+0000"
100
+ # }
101
+ # ]
102
+ # }
103
+ #
104
+ # ==== @unread
105
+ #
106
+ # ==== @updated_at
107
+ #
108
+ class Chat < InstanceResource
109
+ def refresh
110
+ raise '`refresh` method is not supported for this resource.'
111
+ end
112
+ end
113
+
114
+ ##
115
+ # A Chat Message resource.
116
+ #
117
+ # ==== @id
118
+ #
119
+ # ==== @direction
120
+ #
121
+ # ==== @sender
122
+ #
123
+ # ==== @message_time
124
+ #
125
+ # ==== @text
126
+ #
127
+ # ==== @receiver
128
+ #
129
+ # ==== @deleted
130
+ #
131
+ # ==== @user_id
132
+ #
133
+ # ==== @status
134
+ #
135
+ # ==== @total
136
+ #
137
+ # ==== @first_name
138
+ #
139
+ # ==== @last_name
140
+ #
141
+ class ChatMessage < InstanceResource
142
+ def refresh
143
+ raise '`refresh` method is not supported for this resource.'
144
+ end
145
+ end
146
+ end
147
+ end
@@ -0,0 +1,128 @@
1
+ module Textmagic
2
+ module REST
3
+ class Client
4
+ include Textmagic::REST::Utils
5
+
6
+ attr_reader :username, :token, :host
7
+
8
+ ##
9
+ # Create a new TextMagic APIv2 REST HTTP client.
10
+ #
11
+ # Example:
12
+ #
13
+ # @client = Textmagic::REST::Client.new username, token
14
+ #
15
+ # === <tt>username: 'my_textmagic_username'</tt>
16
+ #
17
+ # TextMagic account's username. Can be found here:
18
+ # https://my.textmagic.com/online/account/details
19
+ #
20
+ # === <tt>token: 'my_textmagic_apiv2_token'</tt>
21
+ #
22
+ # TextMagic account's token. Can be found here:
23
+ # https://my.textmagic.com/online/api/rest-api/keys
24
+ #
25
+ # === <tt>host: 'https://api.textmagictesting.com'</tt>
26
+ #
27
+ # The domain to which you'd like the client to make HTTP requests. Useful
28
+ # for testing. Defaults to 'https://rest.textmagic.com'.
29
+ def initialize(username=nil, token=nil, host='https://rest.textmagic.com/api/v2')
30
+ @username = username
31
+ @token = token
32
+ @host = host
33
+ if @username.nil? || @token.nil?
34
+ raise ArgumentError, 'Username and token are required'
35
+ end
36
+
37
+ setup_connection
38
+ setup_resources
39
+ end
40
+
41
+ def inspect # :nodoc:
42
+ "<#{self.class} @username=#{@username}>"
43
+ end
44
+
45
+ HTTP_HEADERS = {
46
+ 'Accept' => 'application/json',
47
+ 'Accept-Charset' => 'utf-8',
48
+ 'Accept-Language' => 'en-us',
49
+ 'User-Agent' => "textmagic-ruby.#{Textmagic::REST::VERSION}"
50
+ }
51
+
52
+ [:get, :post, :delete, :put].each do |method|
53
+ method_class = Net::HTTP.const_get method.to_s.capitalize
54
+ define_method method do |path, *args|
55
+ params = args[0]
56
+ params = {} if params.empty?
57
+ path = build_full_path(path, params, method) unless args[1]
58
+ HTTP_HEADERS["X-TM-Username"] = @username
59
+ HTTP_HEADERS["X-TM-Key"] = @token
60
+ request = method_class.new(path, HTTP_HEADERS)
61
+ if [:post, :put].include?(method) or
62
+ ([:delete].include?(method) and !params.empty?)
63
+ request.set_form_data(to_camel_case(params, false))
64
+ HTTP_HEADERS['Content-Type'] = 'application/x-www-form-urlencoded'
65
+ end
66
+ make_request(request)
67
+ end
68
+ end
69
+
70
+ def ping
71
+ path = 'ping'
72
+ response = self.get "/#{path}", {}
73
+ response[path]
74
+ end
75
+
76
+ protected
77
+
78
+ def url_encode(hash)
79
+ hash.to_a.map {|p| p.map {|e| CGI.escape e.to_s}.join '='}.join '&'
80
+ end
81
+
82
+ def build_full_path(path, params, method)
83
+ path << "?#{url_encode(to_camel_case(params, false))}" if method == :get && !params.empty?
84
+ "#{@host}#{path}"
85
+ end
86
+
87
+ def setup_connection
88
+ uri = URI.parse(@host)
89
+ @conn = Net::HTTP.new(uri.host, uri.port)
90
+ setup_ssl
91
+ end
92
+
93
+ def setup_ssl
94
+ @conn.use_ssl = true
95
+ @conn.verify_mode = OpenSSL::SSL::VERIFY_PEER
96
+ @conn.ca_file = File.dirname(__FILE__) + '/../../../conf/cacert.pem'
97
+ end
98
+
99
+ def make_request(request)
100
+
101
+ response = @conn.request request
102
+
103
+ if response.kind_of? Net::HTTPServerError
104
+ raise Textmagic::REST::ServerError
105
+ end
106
+
107
+ if response.body and !response.body.empty?
108
+ object = MultiJson.load response.body
109
+ elsif response.code == 204.to_s
110
+ object = true
111
+ elsif response.kind_of? Net::HTTPBadRequest
112
+ object = {:message => 'Bad request', :code => 400}
113
+ end
114
+
115
+ if response.kind_of? Net::HTTPClientError
116
+ raise Textmagic::REST::RequestError.new object['message'], object['code']
117
+ end
118
+ object
119
+ end
120
+
121
+ def setup_resources
122
+ resource self, :messages, :contacts, :lists, :custom_fields, :unsubscribers,
123
+ :bulks, :chats, :schedules, :sessions, :templates, :invoices, :users,
124
+ :numbers, :senderids, :subaccounts, :replies
125
+ end
126
+ end
127
+ end
128
+ end
@@ -0,0 +1,174 @@
1
+ module Textmagic
2
+ module REST
3
+ class Contacts < ListResource
4
+ ##
5
+ # Get contact by ID.
6
+ # Returns Contact object.
7
+ #
8
+ # uid:: Contact ID. Required.
9
+ #
10
+ # Example:
11
+ #
12
+ # @contact = client.contacts.get 987
13
+ #
14
+ def get(uid)
15
+ super uid
16
+ end
17
+
18
+ ##
19
+ # Create new Contact.
20
+ # Returns Contact object contains id and link to new Contact.
21
+ #
22
+ # The following *params* keys are supported:
23
+ #
24
+ # first_name::
25
+ #
26
+ # last_name::
27
+ #
28
+ # phone:: Contact's phone number. Required.
29
+ #
30
+ # email::
31
+ #
32
+ # company_name::
33
+ #
34
+ # country:: 2-letter ISO country code.
35
+ #
36
+ # lists:: String of Lists separated by commas to assign contact. Required.
37
+ #
38
+ # Example:
39
+ #
40
+ # @contact = client.contacts.create {:phone => '9999999', :lists => '123, 456'}
41
+ #
42
+ def create(params={})
43
+ super params
44
+ end
45
+
46
+ ##
47
+ # Get all user contacts.
48
+ # Returns PaginateResource object, contains array of Contact objects.
49
+ #
50
+ # The following *params* keys are supported:
51
+ #
52
+ # search:: If *true* then search contacts using `query`, `ids` and/or `group_id`. Defaults *false*.
53
+ #
54
+ # page:: Fetch specified results page. Defaults 1
55
+ #
56
+ # limit:: How many results on page. Defaults 10
57
+ #
58
+ # shared:: Should contacts in shared lists to be included. Defaults 0
59
+ #
60
+ # ids:: Find contact by ID(s). Using with `search`=*true*.
61
+ #
62
+ # list_id:: Find contact by List ID. Using with `search`=*true*.
63
+ #
64
+ # query:: Find contact by specified search query. Using with `search`=*true*..
65
+ #
66
+ # Example:
67
+ #
68
+ # @contacts = client.contacts.list
69
+ #
70
+ def list(params={})
71
+ super params
72
+ end
73
+
74
+ ##
75
+ # Updates the existing Contact for the given unique id.
76
+ # Returns Contact object contains id and link to updated Contact.
77
+ #
78
+ # uid:: Contact ID. Required.
79
+ #
80
+ # The following *params* keys are supported:
81
+ #
82
+ # first_name::
83
+ #
84
+ # last_name::
85
+ #
86
+ # phone:: Contact's phone number. Required.
87
+ #
88
+ # email::
89
+ #
90
+ # company_name::
91
+ #
92
+ # country:: 2-letter ISO country code.
93
+ #
94
+ # lists:: String of Lists separated by commas to assign contact. Required.
95
+ #
96
+ # Example:
97
+ #
98
+ # @contact = client.contacts.update 123, {:phone => '9999999', :lists => '123, 456'}
99
+ #
100
+ def update(uid, params={})
101
+ super uid, params
102
+ end
103
+
104
+ ##
105
+ # Delete contact by ID. Returns *true* if success.
106
+ #
107
+ # uid:: Contact ID. Required.
108
+ #
109
+ # Example:
110
+ #
111
+ # r = client.contacts.delete 987
112
+ #
113
+ def delete(uid)
114
+ super uid
115
+ end
116
+
117
+ ##
118
+ # Fetch lists which contact belongs to.
119
+ # Returns PaginateResource object, contains array of List objects.
120
+ #
121
+ # uid:: Contact ID. Required.
122
+ #
123
+ # The following *params* keys are supported:
124
+ #
125
+ # page:: Fetch specified results page. Defaults 1
126
+ #
127
+ # limit:: How many results on page. Defaults 10
128
+ #
129
+ # Example:
130
+ #
131
+ # @contact = client.contacts.lists 123
132
+ #
133
+ def lists(uid, params={})
134
+ response = @client.get "#{@path}/#{uid}/lists", params
135
+ PaginateResource.new "#{@path}", @client, response, Textmagic::REST::List
136
+ end
137
+ end
138
+
139
+ ##
140
+ # A Contact resource.
141
+ #
142
+ # ==== @id
143
+ #
144
+ # ==== @phone
145
+ #
146
+ # ==== @email
147
+ #
148
+ # ==== @first_name
149
+ #
150
+ # ==== @last_name
151
+ #
152
+ # ==== @company_name
153
+ #
154
+ # ==== @country
155
+ #
156
+ # Hash like this
157
+ # {
158
+ # "id" => "US",
159
+ # "name" => "United States",
160
+ # }
161
+ #
162
+ # ==== @custom_fields
163
+ #
164
+ # Array of Hashes, each looks like this
165
+ # {
166
+ # "value" => "30",
167
+ # "id" => "1044",
168
+ # "name" => "Age",
169
+ # "createdAt" => "2015-04-27T09:29:46+0000",
170
+ # }
171
+ #
172
+ class Contact < InstanceResource; end
173
+ end
174
+ end