Module: Beintoo

Defined in:
lib/beintoo.rb,
lib/beintoo/app.rb,
lib/beintoo/user.rb,
lib/beintoo/vgood.rb,
lib/beintoo/player.rb,
lib/beintoo/railtie.rb,
lib/beintoo/version.rb,
lib/beintoo/api_exception.rb,
lib/beintoo/model_additions.rb

Defined Under Namespace

Modules: ModelAdditions Classes: ApiException, App, Player, Railtie, User, UserAlreadyRegisteredException, Vgood

Constant Summary collapse

SERVER_URL =
"https://api.beintoo.com/api/rest/"
SANDBOX_SERVER_URL =
"https://sandbox.beintoo.com/api/rest/"
DEFAULT_HEADER =
{
  'accept' => '*/*',
  'Content-Type' => 'application/x-www-form-urlencoded;charset=UTF-8',
  'X-BEINTOO-SDK-VERSION' => '0.0.1-ruby'
}
LOG_DEBUG =
0
LOG_INFO =
1
LOG_WARNING =
2
LOG_ERROR =
3
VERSION =
"0.0.2"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.apikeyObject

Returns the value of attribute apikey.



25
26
27
# File 'lib/beintoo.rb', line 25

def apikey
  @apikey
end

.debugObject

Returns the value of attribute debug.



25
26
27
# File 'lib/beintoo.rb', line 25

def debug
  @debug
end

.loggerObject

Returns the value of attribute logger.



25
26
27
# File 'lib/beintoo.rb', line 25

def logger
  @logger
end

.redirect_uriObject

Returns the value of attribute redirect_uri.



25
26
27
# File 'lib/beintoo.rb', line 25

def redirect_uri
  @redirect_uri
end

.sandboxObject

Returns the value of attribute sandbox.



25
26
27
# File 'lib/beintoo.rb', line 25

def sandbox
  @sandbox
end

.transformerObject

Returns the value of attribute transformer.



25
26
27
# File 'lib/beintoo.rb', line 25

def transformer
  @transformer
end

Class Method Details

.build_headers(headers = {}) ⇒ Object



63
64
65
66
# File 'lib/beintoo.rb', line 63

def build_headers(headers = {})
  raise Beintoo::ApiException, "Api Key not provided" if Beintoo::apikey.nil?
  {apikey: apikey, sandbox: sandbox}.merge headers
end

.configure {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:

  • _self (Beintoo)

    the object that the method was called on



31
32
33
34
35
36
37
# File 'lib/beintoo.rb', line 31

def configure
  yield self
  self.sandbox = false if sandbox != true
  if defined? Rails
    self.logger = Rails.logger
  end
end

.get(url, headers = {}, params = {}) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/beintoo.rb', line 68

def get(url, headers = {}, params = {})
  url = "#{Beintoo::SERVER_URL}#{url}"
  start = Time.now
  headers.merge! Beintoo::DEFAULT_HEADER
  url += "?#{hash_to_params(params)}" unless params.empty?

  log "#"*80
  log "       Calling Beintoo Api"
  log url
  log headers.inspect
  log "#"*80

  resource = RestClient::Resource.new url, headers: headers

  log "Connecting GET to #{url} with headers #{headers}"

  begin
    response = resource.get
    stop = Time.now
    http_code = response.code

    log "------  RAW RESULT  ------"
    log response.body
    log "------ /RAW RESULT  ------"

    result = JSON.parse(response.body)
    result = result.with_indifferent_access if result.is_a? Hash
    result
  rescue RestClient::Exception => e
    log "Exception grabbed, response is", LOG_ERROR
    log e.response.inspect, LOG_ERROR
    log e.inspect, LOG_ERROR
    mex = JSON.parse(e.response)["message"]
    if mex == "An user with this email already registered"
      raise Beintoo::UserAlreadyRegisteredException, mex
    else
      raise Beintoo::ApiException, mex
    end
  end
end

.get_connect_url(guid, display = nil, signup = "facebook", logged_uri = nil) ⇒ Object

generates beintoo-connect url when user creation fails XXX logged_uri parameter is from php API but don’t know yet how it works see documentation.beintoo.com/home/api-docs/user-player-connection redirect will have param userext=XXXXXXXXXXXXXyouruserGUID



165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/beintoo.rb', line 165

def get_connect_url(guid, display=nil, ="facebook", logged_uri=nil)
  return false if guid.nil?
  url = "http://www.beintoo.com/connect.html?guid=#{guid}&apikey=#{self.apikey}&redirect_uri=#{self.redirect_uri}"
  unless display.nil?
    url += "&display=#{display}"
  end
  url += "&signup=#{}"
  unless logged_uri.nil?
    url += "&logged_uri=#{logged_uri}"
  end
  url
end

.hash_to_params(data = {}) ⇒ Object



151
152
153
154
155
156
157
158
159
# File 'lib/beintoo.rb', line 151

def hash_to_params(data = {})
  out = []
  data.each do |k, v|
    if ![Hash, Array].include? v.class
      out << "#{k.to_s}=#{v.to_s}"
    end
  end
  out.join '&'
end

.log(message, level = LOG_DEBUG) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/beintoo.rb', line 39

def log(message, level = LOG_DEBUG)
  begin
    if defined? Rails
      case level
      when LOG_DEBUG
        self.logger.debug message
      when LOG_INFO, LOG_WARNING
        self.logger.info message
      when LOG_ERROR
        self.logger.error message
      end
    elsif self.logger.is_a?(String) && self.logger == "file"
      if @log_file.nil?
        @log_file = File.open 'beintoo_log.log', 'a'
      end
      if debug || level >= LOG_INFO
        @log_file.write message + "\n"
      end
    end
  rescue Exception => e
    puts e.message
  end
end

.post(url, headers = {}, params = {}) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/beintoo.rb', line 109

def post(url, headers = {}, params = {})
  url = "#{Beintoo::SERVER_URL}#{url}"
  start = Time.now
  headers.merge! Beintoo::DEFAULT_HEADER
  ll = {"Content-Type" => nil}
  headers.merge! ll
  if params.empty?
    params = ""
  end

  log "#"*80
  log "       Calling Beintoo Api"
  log url
  log headers.inspect
  log "#"*80

  resource = RestClient::Resource.new url, headers: headers
  if debug
    Beintoo.log "Connecting POST to #{url} with headers #{headers}"
    Beintoo.log "and posting #{params.inspect}"
  end
  begin
    response = resource.post params

    stop = Time.now
    http_code = response.code
    result = JSON.parse(response.body).with_indifferent_access
    log "RESULT IS #{result.inspect}"
    result
  rescue RestClient::Exception => e
    log "Exception grabbed, response is", LOG_ERROR
    log e.response.inspect, LOG_ERROR
    log e.inspect, LOG_ERROR
    mex = JSON.parse(e.response)["message"]
    if mex == "An user with this email already registered"
      raise Beintoo::UserAlreadyRegisteredException, mex
    else
      raise Beintoo::ApiException, mex
    end
  end
end