Module: ThrillcallAPI

Defined in:
lib/thrillcall-api/result.rb,
lib/thrillcall-api.rb,
lib/thrillcall-api/version.rb,
lib/thrillcall-api/exceptions.rb

Overview

Defined Under Namespace

Classes: Error, Result

Constant Summary collapse

RETRY_EXCEPTIONS =
{
  Errno::ECONNREFUSED           => nil,
  Errno::ECONNRESET             => nil,
  Errno::EHOSTUNREACH           => nil,
  Errno::EHOSTDOWN              => nil,
  Errno::EINVAL                 => nil,
  Net::HTTPBadResponse          => nil,
  Net::HTTPRequestTimeOut       => nil,
  Net::HTTPServerError          => nil,   # 5xx
  Net::HTTPInternalServerError  => nil,   # 500
  Net::HTTPNotImplemented       => nil,   # 501
  Net::HTTPBadGateway           => nil,   # 502
  Net::HTTPServiceUnavailable   => nil,   # 503
  Net::HTTPGatewayTimeOut       => nil,   # 504
  Net::HTTPVersionNotSupported  => nil,   # 505
  OpenURI::HTTPError            => /5[0-9][0-9]|400/,
  Timeout::Error                => nil,
  EOFError                      => nil,
  SocketError                   => nil
}
VERSION =
"0.1.1"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.baseObject

Returns the value of attribute base.



35
36
37
# File 'lib/thrillcall-api.rb', line 35

def base
  @base
end

.connObject

Returns the value of attribute conn.



35
36
37
# File 'lib/thrillcall-api.rb', line 35

def conn
  @conn
end

.cur_api_keyObject

Returns the value of attribute cur_api_key.



35
36
37
# File 'lib/thrillcall-api.rb', line 35

def cur_api_key
  @cur_api_key
end

.resultObject

Returns the value of attribute result.



35
36
37
# File 'lib/thrillcall-api.rb', line 35

def result
  @result
end

Class Method Details

.get(endpoint, params) ⇒ Object



99
100
101
102
103
104
105
106
107
# File 'lib/thrillcall-api.rb', line 99

def get(endpoint, params)
  r = nil
  retriable :on => @retry_exceptions, :tries => @retry_tries, :timeout => @retry_timeout, :on_retry => on_retry_exception do
    r = @conn.get do |req|
      req.url endpoint, params.merge(:api_key => @cur_api_key)
    end
  end
  return JSON.parse(r.body)
end

.method_missing(method, *args, &block) ⇒ Object



124
125
126
127
128
# File 'lib/thrillcall-api.rb', line 124

def method_missing(method, *args, &block)
  r = Result.new
  r.send(method, args, block)
  return r
end

.new(cur_api_key, options = {}) ⇒ Object

Set up the Faraday connection based on configuration



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/thrillcall-api.rb', line 38

def new(cur_api_key, options = {})
  default_options = {
    :base_url   => "https://api.thrillcall.com/api/",
    :version    => 3,
    :logger     => false,
    :timeout    => 20
  }

  opts = default_options.merge(options)

  @retry_exceptions = opts[:retry_exceptions] || RETRY_EXCEPTIONS
  @retry_tries      = opts[:retry_tries]      || 5
  @retry_timeout    = opts[:timeout]          || 10

  @cur_api_key  = cur_api_key
  base_url      = opts[:base_url]
  version       = opts[:version]
  logger        = opts[:logger]

  faraday_opts  = {
    :timeout      => opts[:timeout],
    :open_timeout => opts[:timeout]
  }

  # Make sure the base_url is in the form https://.../
  unless base_url.match /^(http|https):\/\//
    base_url = "https://" + base_url
  end

  unless base_url.match /\/$/
    base_url = base_url + "/"
  end

  # Set JSON accept header and custom user agent
  @headers = {
    :accept     => 'application/json',
    :user_agent => "Thrillcall API Wrapper version #{ThrillcallAPI::VERSION}"
  }

  @base   = "#{base_url}v#{version}/"
  @conn   = Faraday.new( :url => @base, :headers => @headers, :options => faraday_opts ) do |builder|
    builder.adapter Faraday.default_adapter
    if logger
      builder.response :logger
    end
    # This will raise an exception if an error occurs during a request
    builder.response :raise_error
  end

  @result = Result.new

  return self
end

.on_retry_exceptionObject



92
93
94
95
96
97
# File 'lib/thrillcall-api.rb', line 92

def on_retry_exception
  Proc.new do |exception, tries|
    msg = "ThrillcallAPI : #{exception.class}: '#{exception.message}' - #{tries} attempts."
    @conn.send(:warn, msg)
  end
end

.post(endpoint, params, method = :post) ⇒ Object



109
110
111
112
113
114
115
116
117
118
# File 'lib/thrillcall-api.rb', line 109

def post(endpoint, params, method = :post)
  r = nil
  block = lambda do |req|
    req.url endpoint, params.merge(:api_key => @cur_api_key)
  end
  retriable :on => @retry_exceptions, :tries => @retry_tries, :timeout => @retry_timeout, :on_retry => on_retry_exception do
    r = @conn.send(method, &block)
  end
  JSON.parse(r.body)
end

.put(endpoint, params) ⇒ Object



120
121
122
# File 'lib/thrillcall-api.rb', line 120

def put(endpoint, params)
  post(endpoint, params, :put)
end