Module: TexMindbody::Api

Defined in:
lib/tex_mindbody/api.rb,
lib/tex_mindbody/api/model.rb,
lib/tex_mindbody/api/version.rb,
lib/tex_mindbody/api/model/client.rb,
lib/tex_mindbody/api/configuration.rb,
lib/tex_mindbody/api/endpoint/client.rb,
lib/tex_mindbody/api/endpoint/usertoken.rb

Defined Under Namespace

Modules: Model Classes: Configuration

Constant Summary collapse

VERSION =
"0.1.0"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.headersObject (readonly)

Returns the value of attribute headers.



15
16
17
# File 'lib/tex_mindbody/api.rb', line 15

def headers
  @headers
end

.loggerObject

Returns the value of attribute logger.



16
17
18
# File 'lib/tex_mindbody/api.rb', line 16

def logger
  @logger
end

Class Method Details

.clients(q = {}) ⇒ Object



4
5
6
7
8
9
10
# File 'lib/tex_mindbody/api/endpoint/client.rb', line 4

def clients q = {}
  query_opts = %i{ClientIds SearchText IsProspect LastModifiedDate}
  validate_query q, query_opts
  path = "#{TexMindbody::Api.config.rest_api}/client/clients"
  clients = paginated_get path, 'Clients', q
  clients.map {|c| TexMindbody::Api::Model::Client.new c}
end

.configObject



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

def config
  @config ||= TexMindbody::Api::Configuration.new
end

.configure {|config| ... } ⇒ Object

Yields:



18
19
20
21
# File 'lib/tex_mindbody/api.rb', line 18

def configure
  yield config
  init
end

.initObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/tex_mindbody/api.rb', line 27

def init
  logger ||= Logger.new STDOUT
  logger.level = Logger::WARN

  if TexMindbody::Api.config.rest_api.nil? || TexMindbody::Api.config.rest_api.empty?
    TexMindbody::Api.config.rest_api = "https://api.mindbodyonline.com/public/v6"
  end

  if TexMindbody::Api.config.username.nil? || TexMindbody::Api.config.password.nil?
    Raise ArgumentError, "config.username & config.password must be set"
  end

  # Get the auth_token
  if TexMindbody::Api.config.auth_token.nil?
    TexMindbody::Api.issue
  end
  update_headers
end

.issueObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/tex_mindbody/api/endpoint/usertoken.rb', line 4

def issue
  path = "#{TexMindbody::Api.config.rest_api}/usertoken/issue"
  headers = {
    "Content-Type": 'application/json',
    "Api-Key": TexMindbody::Api.config.api_key,
    "SiteId": TexMindbody::Api.config.site_id
  }
  body = {
    "Username": TexMindbody::Api.config.username,
    "Password": TexMindbody::Api.config.password
  }
  response = HTTParty.post path, :headers => headers, :query => {}, :body => body.to_json
  raise IOError.new response if response.server_error?
  raise ArgumentError.new response unless response.success?
  raise RuntimeError.new response if response['AccessToken'].nil?
  TexMindbody::Api.config.auth_token = response['AccessToken']
end

.paginated_get(path, response_item, q = {}) ⇒ Object



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

def paginated_get path, response_item, q = {}
  results = []
  offset = 0
  query_params = q.map { |k,v| "#{k}=#{v}"}.join('&')
  query_params += '&' unless query_params.empty?
  loop do
    path_paginated = "#{path}?#{query_params}offset=#{offset}&limit=200"
    response = HTTParty.get path_paginated, headers: @headers
    raise IOError.new response if response.server_error?
    raise ArgumentError.new response unless response.success?
    results += response[response_item]
    break if ( (response['PaginationResponse']['PageSize'] + response['PaginationResponse']['RequestedOffset'] ) == response['PaginationResponse']['TotalResults'] )
    offset += 200
  end
  results
end

.update_headersObject



46
47
48
49
50
51
52
# File 'lib/tex_mindbody/api.rb', line 46

def update_headers
  @headers = {
    "Api-Key": TexMindbody::Api.config.api_key,
    "SiteId": TexMindbody::Api.config.site_id,
    "Authorization": TexMindbody::Api.config.auth_token
  }
end

.validate_query(q, query_opts) ⇒ Object



54
55
56
# File 'lib/tex_mindbody/api.rb', line 54

def validate_query q, query_opts
  raise ArgumentError.new "Invalid query parameters used #{q.keys.to_s}" unless q.keys.to_set.subset?(query_opts.to_set)
end