Class: Streambird::Api

Inherits:
Struct
  • Object
show all
Defined in:
lib/streambird/api.rb,
lib/streambird/api/otp.rb,
lib/streambird/api/user.rb,
lib/streambird/api/oauth.rb,
lib/streambird/api/errors.rb,
lib/streambird/api/wallet.rb,
lib/streambird/api/session.rb,
lib/streambird/api/magic_link.rb,
lib/streambird/api/oauth_connection.rb

Defined Under Namespace

Classes: APIKeyInvalid, BadRequest, ConnectionError, Error, InternalServerError, MagicLink, NotFound, OAuth, OAuthConnection, Otp, Session, TooManyRequests, Unauthorized, User, Wallet

Constant Summary collapse

STREAMBIRD_API_URL =
ENV['STREAMBIRD_API_URL'] || 'https://api.streambird.io/v1/'
STREAMBIRD_GEM_INFO =
Gem.loaded_specs["streambird"]
STREAMBIRD_RUBY_CLIENT_VERSION =
STREAMBIRD_GEM_INFO ? STREAMBIRD_GEM_INFO.version.to_s : '0.1.1'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key

Returns:

  • (Object)

    the current value of api_key



6
7
8
# File 'lib/streambird/api.rb', line 6

def api_key
  @api_key
end

#default_request_paramsObject

Returns the value of attribute default_request_params

Returns:

  • (Object)

    the current value of default_request_params



6
7
8
# File 'lib/streambird/api.rb', line 6

def default_request_params
  @default_request_params
end

#loggingObject

Returns the value of attribute logging

Returns:

  • (Object)

    the current value of logging



6
7
8
# File 'lib/streambird/api.rb', line 6

def logging
  @logging
end

Instance Method Details

#connectionObject



11
12
13
14
15
16
17
18
# File 'lib/streambird/api.rb', line 11

def connection
  @connection ||= Faraday.new(:url => STREAMBIRD_API_URL) do |faraday|
    faraday.request :authorization, 'Bearer', self.api_key
    faraday.request  :url_encoded             # form-encode POST params
    faraday.response :logger if logging       # log requests to STDOUT
    faraday.adapter  Faraday.default_adapter  # make requests with Net::HTTP
  end
end

#delete(url, body = {}) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/streambird/api.rb', line 76

def delete(url, body = {})
  body = default_request_params.merge(body)
  response = connection.delete do |req|
    req.url "#{STREAMBIRD_API_URL}#{url}"
    req.headers['Content-Type'] = 'application/json'
    req.body = body.to_json
    req.headers['X-API-Client'] = "Ruby"
    req.headers["X-API-Client-Version"] = STREAMBIRD_RUBY_CLIENT_VERSION
  end

  if response.status != 200 and response.status != 201
    return handle_error(response)
  end

  response
rescue Faraday::Error::ConnectionFailed
  raise Streambird::Api::ConnectionError
end

#get(url, params = {}) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/streambird/api.rb', line 20

def get(url, params = {})
  response = connection.get do |req|
    req.url "#{STREAMBIRD_API_URL}#{url}"
    req.params.merge!(default_request_params.merge(params))
    req.headers['X-API-Client'] = "Ruby"
    req.headers["X-API-Client-Version"] = STREAMBIRD_RUBY_CLIENT_VERSION
  end

  if response.status != 200
    return handle_error(response)
  end

  response
rescue Faraday::Error::ConnectionFailed
  raise Streambird::Api::ConnectionError
end

#handle_error(response) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/streambird/api.rb', line 95

def handle_error(response)
  error_body = JSON.parse(response.body)
  if response.status == 404
    raise Streambird::Api::NotFound.new(error_body['error_message'], response.status)
  elsif response.status == 429
    raise Streambird::Api::TooManyRequests.new(error_body['error_message'], response.status)
  elsif response.status > 499
    raise Streambird::Api::InternalServerError.new(error_body['error_message'], response.status)
  elsif response.status == 401
    raise Streambird::Api::Unauthorized.new(error_body['error_message'], response.status)
  else
    raise Streambird::Api::BadRequest.new(error_body['error_message'], response.status)
  end
rescue JSON::ParserError
  raise Streambird::Api::InternalServerError
end

#post(url, body = {}) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/streambird/api.rb', line 37

def post(url, body = {})
  body = default_request_params.merge(body)
  response = connection.post do |req|
    req.url "#{STREAMBIRD_API_URL}#{url}"
    req.headers['Content-Type'] = 'application/json'
    req.body = body.to_json
    req.headers['X-API-Client'] = "Ruby"
    req.headers["X-API-Client-Version"] = STREAMBIRD_RUBY_CLIENT_VERSION
  end

  if response.status != 200 and response.status != 201
    return handle_error(response)
  end

  response
rescue Faraday::Error::ConnectionFailed
  raise Streambird::Api::ConnectionError
end

#put(url, body = {}) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/streambird/api.rb', line 56

def put(url, body = {})
  body = default_request_params.merge(body)
  response = connection.put do |req|
    req.url "#{STREAMBIRD_API_URL}#{url}"
    req.headers['Content-Type'] = 'application/json'
    req.body = body.to_json
    req.headers['X-API-Client'] = "Ruby"
    req.headers["X-API-Client-Version"] = STREAMBIRD_RUBY_CLIENT_VERSION
  end

  if response.status != 200 and response.status != 201
    return handle_error(response)
  end

  response
rescue Faraday::Error::ConnectionFailed
  raise Streambird::Api::ConnectionError
end