Class: IfreeSms::API

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

Direct Known Subclasses

SMSDirectAPI

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(login = nil, pass = nil) ⇒ API

initialize with an access token



19
20
21
22
# File 'lib/ifree_sms.rb', line 19

def initialize( = nil, pass = nil)
  @login =  || IfreeSms.config.
  @pass = pass || IfreeSms.config.password
end

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



23
24
25
# File 'lib/ifree_sms.rb', line 23

def access_token
  @access_token
end

Instance Method Details

#api(path, args = {}, verb = "get", options = {}) {|body| ... } ⇒ Object

Yields:

  • (body)

Raises:



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ifree_sms.rb', line 25

def api(path, args = {}, verb = "get", options = {}, &error_checking_block)
  # Fetches the given path in the Graph API.
  args["login"] = @login
  args["pass"] = @pass
  
  # add a leading /
  path = "/#{path}" unless path =~ /^(\/|http|https|ftp)/

  # make the request via the provided service
  result = IfreeSms.make_request(path, args, verb, options)

  # Check for any 500 errors before parsing the body
  # since we're not guaranteed that the body is valid JSON
  # in the case of a server error
  raise APIError.new({"type"=>"HTTP #{result.status.to_s}", "message"=>"Response body: #{result.body}"}) if result.status >= 500
  
  body = result.body
  yield body if error_checking_block

  # if we want a component other than the body (e.g. redirect header for images), return that
  options[:http_component] ? result.send(options[:http_component]) : body
end