Class: EasySMS::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url = nil) ⇒ Client

Returns a new instance of Client.



5
6
7
8
9
10
# File 'lib/easy_sms/client.rb', line 5

def initialize(url = nil)
  @url = url || ENV['EASYSMS_URL'] || ENV['EASY_SMS_URL']
  raise 'Resource URL not set. Please pass a valid URL to initializer, or set EASYSMS_URL environment variable.' unless @url
  @resource = RestClient::Resource.new(@url)
  @uri = URI.parse(@url)
end

Instance Attribute Details

#uriObject (readonly)

Returns the value of attribute uri.



3
4
5
# File 'lib/easy_sms/client.rb', line 3

def uri
  @uri
end

Instance Method Details

#accountObject



20
21
22
# File 'lib/easy_sms/client.rb', line 20

def 
  AccountResource.new(self)
end

#delete(path) ⇒ Object



52
53
54
55
56
57
58
59
# File 'lib/easy_sms/client.rb', line 52

def delete(path)
  begin
    @resource[path].delete(content_type: :json, accept: :json)
  rescue RestClient::Exception => e
    json = JSON.parse(e.response) rescue nil
    raise Error.new([e, json].compact.join(' '))
  end
end

#get(path, attrs) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/easy_sms/client.rb', line 24

def get(path, attrs)
  begin
    #TODO: add attrs to url or let it be handled by RestClient
    @resource[path].get(content_type: :json, accept: :json)
  rescue RestClient::Exception => e
    json = JSON.parse(e.response) rescue nil
    raise Error.new([e, json].compact.join(' '))
  end
end

#messagesObject



12
13
14
# File 'lib/easy_sms/client.rb', line 12

def messages
  MessageResource.new(self)
end

#phone_numbersObject



16
17
18
# File 'lib/easy_sms/client.rb', line 16

def phone_numbers
  PhoneNumberResource.new(self)
end

#post(path, attrs) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/easy_sms/client.rb', line 34

def post(path, attrs)
  begin
    @resource[path].post(attrs.to_json, content_type: :json, accept: :json)
  rescue RestClient::Exception => e
    json = JSON.parse(e.response) rescue nil
    raise Error.new([e, json].compact.join(' '))
  end
end

#put(path, attrs) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/easy_sms/client.rb', line 43

def put(path, attrs)
  begin
    @resource[path].put(attrs.to_json, content_type: :json, accept: :json)
  rescue RestClient::Exception => e
    json = JSON.parse(e.response) rescue nil
    raise Error.new([e, json].compact.join(' '))
  end
end