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,85 @@
1
+ module Textmagic
2
+ module REST
3
+ class Unsubscribers < ListResource
4
+ ##
5
+ # Get unsubscriber by ID.
6
+ # Returns Unsubscriber object.
7
+ #
8
+ # uid:: Unsubscriber ID. Required.
9
+ #
10
+ # Example:
11
+ #
12
+ # @unsubscriber = client.unsubscribers.get 987
13
+ #
14
+ def get(uid)
15
+ super uid
16
+ end
17
+
18
+ ##
19
+ # Unsubscribe phone from your communication by phone number.
20
+ # Returns Unsubscriber object contains id and link to new Unsubscriber.
21
+ #
22
+ # The following *params* keys are supported:
23
+ #
24
+ # phone:: Phone number you want to unsubscribe. Required.
25
+ #
26
+ # Example:
27
+ #
28
+ # @unsubscriber = client.unsubscribers.create {:phone => '999920102'}
29
+ #
30
+ def create(params={})
31
+ super params
32
+ end
33
+
34
+ ##
35
+ # Get all user unsubscribers.
36
+ # Returns PaginateResource object, contains array of Unsubscriber objects.
37
+ #
38
+ # The following *params* keys are supported:
39
+ #
40
+ # page:: Fetch specified results page. Defaults 1
41
+ #
42
+ # limit:: How many results on page. Defaults 10
43
+ #
44
+ # Example:
45
+ #
46
+ # @unsubscribers = client.unsubscribers.list
47
+ #
48
+ def list(params={})
49
+ [:search, 'search'].each do |search|
50
+ params.delete search
51
+ end
52
+ super params
53
+ end
54
+
55
+ ##
56
+ # Deleting is not supported.
57
+ #
58
+ def delete(uid)
59
+ raise '`delete` method is not available for this type of resource.'
60
+ end
61
+
62
+ ##
63
+ # Updating is not supported.
64
+ #
65
+ def update(uid, params={})
66
+ raise '`update` method is not available for this type of resource.'
67
+ end
68
+ end
69
+
70
+ ##
71
+ # A Unsubscriber resource.
72
+ #
73
+ # ==== @id
74
+ #
75
+ # ==== @phone
76
+ #
77
+ # ==== @first_name
78
+ #
79
+ # ==== @last_name
80
+ #
81
+ # ==== @unsubscribe_time
82
+ #
83
+ class Unsubscriber < InstanceResource; end
84
+ end
85
+ end
@@ -0,0 +1,202 @@
1
+ module Textmagic
2
+ module REST
3
+ class Users < ListResource
4
+ def initialize(path, client)
5
+ super path[0..-2], client
6
+ end
7
+
8
+ def get
9
+ response = @client.get "#{@path}", {}
10
+ @instance_class.new "#{@path}", @client, response
11
+ end
12
+
13
+ ##
14
+ # Get all available sender settings which could be used in "from" parameter of POST messages method.
15
+ # Returns Source object, contains arrays of allowed sender ids, shared and dedicated numbers.
16
+ #
17
+ # The following *params* keys are supported:
18
+ #
19
+ # country:: Return sender settings available in specified country only. Optional.
20
+ #
21
+ # Example:
22
+ #
23
+ # @allowed = client.users.sources
24
+ #
25
+ def sources(params={})
26
+ path = '/sources'
27
+ response = @client.get path, params
28
+ Source.new path, @client, response
29
+ end
30
+
31
+ ##
32
+ # Return messaging statistics.
33
+ # Returns an array of hashes each contains messaging stats by given period.
34
+ #
35
+ # The following *params* keys are supported:
36
+ #
37
+ # by:: Group results by specified period: `off`, `day`, `month` or `year`. Default is `off`.
38
+ #
39
+ # start:: Start date in unix timestamp format. Default is 7 days ago.
40
+ #
41
+ # end:: End date in unix timestamp format. Default is now.
42
+ #
43
+ # Example:
44
+ #
45
+ # @msg_stat = client.users.messaging_stat
46
+ #
47
+ def messaging_stat(params={})
48
+ path = '/stats/messaging'
49
+ response = @client.get path, params
50
+ end
51
+
52
+ ##
53
+ # Return account spending statistics.
54
+ # Returns a PaginateResource object contains array of SpendingStat objects.
55
+ #
56
+ # The following *params* keys are supported:
57
+ #
58
+ # page:: Fetch specified results page. Defaults 1
59
+ #
60
+ # limit:: How many results on page. Defaults 10
61
+ #
62
+ # start:: Start date in unix timestamp format. Default is 7 days ago.
63
+ #
64
+ # end:: End date in unix timestamp format. Default is now.
65
+ #
66
+ # Example:
67
+ #
68
+ # @spend_stat = client.users.spending_stat
69
+ #
70
+ def spending_stat(params={})
71
+ path = '/stats/spending'
72
+ response = @client.get path, params
73
+ PaginateResource.new path, @client, response, SpendingStat
74
+ end
75
+
76
+ ##
77
+ # Listing is not supported.
78
+ #
79
+ def list(params={})
80
+ raise '`list` method is not supported for this resource.'
81
+ end
82
+
83
+ ##
84
+ # Get current user info.
85
+ # Returns *true* if success.
86
+ #
87
+ # The following *params* keys are supported:
88
+ #
89
+ # first_name:: User first name. Required.
90
+ #
91
+ # last_name:: User last name. Required.
92
+ #
93
+ # company:: User company. Required.
94
+ #
95
+ # Example:
96
+ #
97
+ # r = client.users.update {:first_name => 'Joye', :last_name => 'Tribbiani', :company => 'TextMagic'}
98
+ #
99
+ def update(params={})
100
+ response = @client.put "#{@path}", params
101
+ @instance_class.new "#{@path}", @client, response
102
+ end
103
+
104
+ ##
105
+ # Deleting is not supported.
106
+ #
107
+ def delete(uid)
108
+ raise '`delete` method is not supported for this resource.'
109
+ end
110
+
111
+ ##
112
+ # Creating is not supporred.
113
+ #
114
+ def create(params={})
115
+ raise '`create` method is not supported for this resource.'
116
+ end
117
+ end
118
+
119
+ ##
120
+ # A Subaccount resource.
121
+ #
122
+ # ==== @id
123
+ #
124
+ # ==== @username
125
+ #
126
+ # ==== @first_name
127
+ #
128
+ # ==== @last_name
129
+ #
130
+ # ==== @balance
131
+ #
132
+ # ==== @company
133
+ #
134
+ # ==== @currency
135
+ #
136
+ # Hash like this:
137
+ # {
138
+ # "id": "GBP",
139
+ # "htmlSymbol": "&pound;"
140
+ # }
141
+ #
142
+ # ==== @timezone
143
+ #
144
+ # Hash like this:
145
+ # {
146
+ # "area": "Pacific",
147
+ # "dst": "0",
148
+ # "offset": "-39600",
149
+ # "timezone": "Pacific/Midway"
150
+ # }
151
+ #
152
+ # ==== @subaccount_type
153
+ #
154
+ class User < InstanceResource
155
+ def refresh
156
+ load_attributes(@client.get("#{@path}", {}))
157
+ self
158
+ end
159
+ end
160
+
161
+ ##
162
+ # A Source resource.
163
+ #
164
+ # ==== @dedicated
165
+ #
166
+ # ==== @shared
167
+ #
168
+ # ==== @sender_ids
169
+ #
170
+ class Source < InstanceResource
171
+ def refresh
172
+ raise '`refresh` method is not supported for this resource.'
173
+ end
174
+ end
175
+
176
+ ##
177
+ # A SpendingStat resource.
178
+ #
179
+ # ==== @id
180
+ #
181
+ # ==== @user_id
182
+ #
183
+ # ==== @date
184
+ #
185
+ # ==== @balance
186
+ #
187
+ # ==== @delta
188
+ #
189
+ # ==== @type
190
+ #
191
+ # ==== @value
192
+ #
193
+ # ==== @comment
194
+ #
195
+ class SpendingStat < InstanceResource
196
+ def refresh
197
+ raise '`refresh` method is not supported for this resource.'
198
+ end
199
+ end
200
+
201
+ end
202
+ end
@@ -0,0 +1,48 @@
1
+ module Textmagic
2
+ module REST
3
+ module Utils
4
+
5
+ def to_camel_case(o, capitalize = true)
6
+ return key_map(o, :to_camel_case, capitalize) if o.is_a? Hash
7
+ string = o.to_s
8
+ string = string.split('_').map do |s_part|
9
+ s_part[0,1].capitalize + s_part[1..-1]
10
+ end.join
11
+ unless capitalize
12
+ return string[0,1].downcase + string[1..-1]
13
+ else
14
+ return string
15
+ end
16
+ end
17
+
18
+ def to_underscore_case(o, capitalize = false)
19
+ return key_map(o, :to_underscore_case) if o.is_a? Hash
20
+ string = o.to_s
21
+ string = string[0,1].downcase + string[1..-1]
22
+ string.gsub(/[A-Z][a-z]*/) {|s| "_#{s.downcase}"}
23
+ end
24
+
25
+ def resource(client, *resources)
26
+ resources.each do |r|
27
+ resource = to_camel_case r
28
+ path = "#{@path}/#{resource}".downcase
29
+ enclosing_module = if @submodule == nil
30
+ Textmagic::REST
31
+ else
32
+ Textmagic::REST.const_get(@submodule)
33
+ end
34
+ resource_class = enclosing_module.const_get resource
35
+ instance_variable_set("@#{r}", resource_class.new(path, client))
36
+ end
37
+ self.class.instance_eval { attr_reader *resources }
38
+ end
39
+
40
+ def key_map(s, method, capitalize = true)
41
+ s = s.to_a.flat_map do |pair|
42
+ [send(method, pair[0], capitalize).to_sym, pair[1]]
43
+ end
44
+ Hash[*s]
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,5 @@
1
+ module Textmagic
2
+ module REST
3
+ VERSION = '2.0.0'
4
+ end
5
+ end
@@ -0,0 +1,57 @@
1
+ require 'spec_helper'
2
+
3
+ describe Textmagic::REST::Client do
4
+
5
+ it 'should set username and token when initialize' do
6
+ client = Textmagic::REST::Client.new 'johnsmith', 'auth_token'
7
+ expect(client.username).to eq('johnsmith')
8
+ expect(client.token).to eq('auth_token')
9
+ end
10
+
11
+ it 'should throw an error if the username and token is not set' do
12
+ expect{Textmagic::REST::Client.new}.to raise_error(ArgumentError)
13
+ end
14
+
15
+ it 'should throw an error if only username or only token set' do
16
+ expect{Textmagic::REST::Client.new 'username'}.to raise_error(ArgumentError)
17
+ expect{Textmagic::REST::Client.new nil, 'token'}.to raise_error(ArgumentError)
18
+ end
19
+
20
+ [:messages, :contacts, :lists, :custom_fields, :unsubscribers,
21
+ :bulks, :chats, :schedules, :sessions, :templates, :invoices, :users,
22
+ :numbers, :senderids, :subaccounts, :replies, :post, :get,
23
+ :put, :delete].each do |method|
24
+ it "should have method #{method}." do
25
+ client = Textmagic::REST::Client.new 'username', 'token'
26
+ expect(client).to respond_to(method)
27
+ end
28
+ end
29
+
30
+ it 'should not raise an error if response body is empty' do
31
+ FakeWeb.register_uri(:any, %r/rest\.textmagic\.com/, body: '')
32
+ client = Textmagic::REST::Client.new 'username', 'token'
33
+ client.messages.delete 1
34
+ end
35
+
36
+ it 'should change default connection address' do
37
+ client = Textmagic::REST::Client.new 'username', 'token', 'https://api.something.com'
38
+ conn = client.instance_variable_get(:@conn)
39
+ expect(conn.address).to eq('api.something.com')
40
+ end
41
+
42
+ it 'should set up proper default ssl connection' do
43
+ client = Textmagic::REST::Client.new 'username', 'token'
44
+ conn = client.instance_variable_get(:@conn)
45
+ expect(conn.address).to eq('rest.textmagic.com')
46
+ expect(conn.port).to eq(443)
47
+ expect(conn.use_ssl?).to eq(true)
48
+ end
49
+
50
+ it 'should have a ping method' do
51
+ FakeWeb.register_uri(:any, %r/rest\.textmagic\.com/, body: '{"ping": "pong"}')
52
+ client = Textmagic::REST::Client.new 'username', 'token'
53
+ expect(client).to respond_to(:ping)
54
+ expect(client.ping).to eq('pong')
55
+ end
56
+
57
+ end
@@ -0,0 +1,36 @@
1
+ require 'spec_helper'
2
+
3
+ describe Textmagic::REST::Contact do
4
+ before do
5
+ @contact = Textmagic::REST::Contact.new 'mockPath', 'mockClient'
6
+ end
7
+
8
+ it 'sets up a client and path' do
9
+ expect(@contact.instance_variable_get(:@client)).to eq('mockClient')
10
+ expect(@contact.instance_variable_get(:@path)).to eq('mockPath')
11
+ end
12
+
13
+ [:refresh].each do |method|
14
+ it "should have method #{method}" do
15
+ expect(@contact).to respond_to(method)
16
+ end
17
+ end
18
+
19
+ end
20
+
21
+ describe Textmagic::REST::Contacts do
22
+ before do
23
+ @contacts = Textmagic::REST::Contacts.new 'mockPath', 'mockClient'
24
+ end
25
+
26
+ [:list, :get, :update, :create, :delete, :lists].each do |method|
27
+ it "should have method #{method}" do
28
+ expect(@contacts).to respond_to(method)
29
+ end
30
+ end
31
+
32
+ it 'sets up a client and path' do
33
+ expect(@contacts.instance_variable_get(:@client)).to eq('mockClient')
34
+ expect(@contacts.instance_variable_get(:@path)).to eq('mockPath')
35
+ end
36
+ end
@@ -0,0 +1,36 @@
1
+ require 'spec_helper'
2
+
3
+ describe Textmagic::REST::CustomField do
4
+ before do
5
+ @custom_field = Textmagic::REST::CustomField.new 'mockPath', 'mockClient'
6
+ end
7
+
8
+ it 'sets up a client and path' do
9
+ expect(@custom_field.instance_variable_get(:@client)).to eq('mockClient')
10
+ expect(@custom_field.instance_variable_get(:@path)).to eq('mockPath')
11
+ end
12
+
13
+ [:refresh].each do |method|
14
+ it "should have method #{method}" do
15
+ expect(@custom_field).to respond_to(method)
16
+ end
17
+ end
18
+
19
+ end
20
+
21
+ describe Textmagic::REST::CustomFields do
22
+ before do
23
+ @custom_fields = Textmagic::REST::CustomFields.new 'mockPath', 'mockClient'
24
+ end
25
+
26
+ [:list, :get, :update, :create, :delete, :update_value].each do |method|
27
+ it "should have method #{method}" do
28
+ expect(@custom_fields).to respond_to(method)
29
+ end
30
+ end
31
+
32
+ it 'sets up a client and path' do
33
+ expect(@custom_fields.instance_variable_get(:@client)).to eq('mockClient')
34
+ expect(@custom_fields.instance_variable_get(:@path)).to eq('mockPath')
35
+ end
36
+ end