Class: Gamifier::Engine
- Inherits:
-
Object
- Object
- Gamifier::Engine
- Defined in:
- lib/gamifier/engine.rb
Constant Summary collapse
- MODELS =
%w{ApiKey Network Activity ActivityDefinition Group Player Reward RewardDefinition Site Track Unit User}
Instance Attribute Summary collapse
- #config ⇒ Object
-
#connection ⇒ Object
Returns the value of attribute connection.
Instance Method Summary collapse
-
#initialize(opts = {}) ⇒ Engine
constructor
A new instance of Engine.
- #ok? ⇒ Boolean
-
#transmit(method, path, opts = {}) ⇒ Object
- Sends an HTTP request corresponding to
method, onpath, with any options given inopts:query - a hash of query parameters to pass with the request
head - a hash of HTTP headers to pass with the request
body -
the body to send with the request.
- a hash of HTTP headers to pass with the request
- a hash of query parameters to pass with the request
- Sends an HTTP request corresponding to
- #uri_to(path = '') ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ Engine
Returns a new instance of Engine.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/gamifier/engine.rb', line 12 def initialize(opts = {}) opts.each{|k,v| config[k.to_sym] = v} raise ArgumentError, "Please configure a :uri and :key first." unless ok? @connection = Faraday.new(:url => uri_to) do |builder| builder.use Faraday::Request::Multipart builder.use Faraday::Request::UrlEncoded # convert request params as "www-form-urlencoded" builder.use Faraday::Response::Logger, Gamifier.logger builder.use Faraday::Adapter::NetHttp # make http requests with Net::HTTP builder.use Faraday::Response::ParseJson, :content_type => /\bjson$/ builder.[:timeout] = Gamifier.config[:timeout] if Gamifier.config[:timeout] builder.[:read_timeout] = Gamifier.config[:read_timeout] if Gamifier.config[:read_timeout] builder.[:open_timeout] = Gamifier.config[:open_timeout] if Gamifier.config[:open_timeout] end end |
Instance Attribute Details
#config ⇒ Object
27 28 29 |
# File 'lib/gamifier/engine.rb', line 27 def config @config ||= Gamifier.config.dup end |
#connection ⇒ Object
Returns the value of attribute connection.
9 10 11 |
# File 'lib/gamifier/engine.rb', line 9 def connection @connection end |
Instance Method Details
#ok? ⇒ Boolean
31 32 33 34 35 36 37 |
# File 'lib/gamifier/engine.rb', line 31 def ok? if config[:enterprise_key].nil? || config[:enterprise_key].empty? !config.values_at(:key, :uri).any?{|k| k.nil? || k.empty?} else !(config[:uri].nil? || config[:uri].empty?) end end |
#transmit(method, path, opts = {}) ⇒ Object
Sends an HTTP request corresponding to method, on path, with any options given in opts:
query-
a hash of query parameters to pass with the request
head-
a hash of HTTP headers to pass with the request
body-
the body to send with the request. If you pass a Ruby object, it will be automatically converted into x-www-form-urlencoded by default.
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 |
# File 'lib/gamifier/engine.rb', line 46 def transmit(method, path, opts = {}) res = connection.send(method) do |req| req.url [path, "json"].join("."), (opts[:query] || {}) (opts[:head] || {}).each do |k,v| req.headers[k] = v end req.body = opts[:body] unless opts[:body].nil? Gamifier.logger.debug "#{method.to_s.upcase} #{req.inspect}" end Gamifier.logger.debug "#{res.inspect}" case res.status when 204 true when 200...300 res.body when 404 nil when 422 # Badgeville returns 422 when an entry already exists or is not valid [:get, :head].include?(method) ? nil : false when 400...500 raise HTTPClientError, res.body when 500...600 raise HTTPServerError, res.body else raise HTTPError, res.body end end |
#uri_to(path = '') ⇒ Object
75 76 77 |
# File 'lib/gamifier/engine.rb', line 75 def uri_to(path = '') URI.join(File.join(self.config[:uri], self.config[:key] || '', ''), path) end |