module DomoscioRails

Constants

VERSION

Attributes

configuration[RW]

Public Class Methods

api_uri(url='') click to toggle source
# File lib/domoscio_rails.rb, line 49
def self.api_uri(url='')
  URI(configuration.root_url + url)
end
configure() { |configuration| ... } click to toggle source
# File lib/domoscio_rails.rb, line 44
def self.configure
  self.configuration ||= Configuration.new
  yield configuration
end
request(method, url, params={}, filters={}, headers = request_headers, before_request_proc = nil) click to toggle source
  • method: HTTP method; lowercase symbol, e.g. :get, :post etc.

  • url: the part after Configuration#root_url

  • params: hash; entity data for creation, update etc.; will dump it by JSON and assign to Net::HTTPRequest#body

  • filters: hash; pagination params etc.; will encode it by URI and assign to URI#query

  • headers: hash; request_headers by default

  • before_request_proc: optional proc; will call it passing the Net::HTTPRequest instance just before Net::HTTPRequest#request

Raises DomoscioRails::ResponseError if response code != 200.

# File lib/domoscio_rails.rb, line 63
  def self.request(method, url, params={}, filters={}, headers = request_headers, before_request_proc = nil)
    uri = api_uri(url)
    uri.query = URI.encode_www_form(filters) unless filters.empty?
    
    res = Net::HTTP.start(uri.host, uri.port) do |http| # , use_ssl: uri.scheme == 'https') do |http|
      req = Net::HTTP::const_get(method.capitalize).new(uri.request_uri, headers)
      req.body = DomoscioRails::JSON.dump(params)
      before_request_proc.call(req) if before_request_proc
      http.request req
    end

    # decode json data
    begin
      data = DomoscioRails::JSON.load(res.body.nil? ? '' : res.body)
    rescue MultiJson::LoadError
      data = {}
    end

    ############### TEMP!!!! #######################################################
    #pp method, uri.request_uri, params #, filters, headers
    #pp res, data
    #puts

    # if (!(res.is_a? Net::HTTPOK))
    #   ex = DomoscioRails::ResponseError.new(uri, res.code, data)
    #   ############## TEMP!!!! ########################################################
    #   #pp ex, data
    #   raise ex
    # end

    # copy pagination info if any
    # ['x-number-of-pages', 'x-number-of-items'].each { |k|
#       filters[k.gsub('x-number-of-', 'total_')] = res[k].to_i if res[k]
#     }

    data
  end

Private Class Methods

get_uname() click to toggle source
# File lib/domoscio_rails.rb, line 115
def self.get_uname
  `uname -a 2>/dev/null`.strip if RUBY_PLATFORM =~ /linux|darwin/i
rescue Errno::ENOMEM
  'uname lookup failed'
end
request_headers() click to toggle source
# File lib/domoscio_rails.rb, line 121
  def self.request_headers
    auth_token = DomoscioRails::AuthorizationToken::Manager.get_token
    headers = {
      'user_agent' => "DomoscioRails V2 RubyBindings/#{DomoscioRails::VERSION}",
      'Authorization' => "Token token=#{DomoscioRails.configuration.client_passphrase}",#"#{auth_token['token_type']} #{auth_token['access_token']}",
      'Content-Type' => 'application/json'
    }
    # begin
#       headers.update('x_mangopay_client_user_agent' => DomoscioRails::JSON.dump(user_agent))
#     rescue => e
#       headers.update('x_mangopay_client_raw_user_agent' => user_agent.inspect, error: "#{e} (#{e.class})")
#     end
  end
user_agent() click to toggle source
# File lib/domoscio_rails.rb, line 103
def self.user_agent
  @uname ||= get_uname

  {
    bindings_version: DomoscioRails::VERSION,
    lang: 'ruby',
    lang_version: "#{RUBY_VERSION} p#{RUBY_PATCHLEVEL} (#{RUBY_RELEASE_DATE})",
    platform: RUBY_PLATFORM,
    uname: @uname
  }
end