Class: Onetime::API

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/onetime/api.rb,
lib/onetime/api.rb

Defined Under Namespace

Modules: VERSION

Constant Summary collapse

LIB_HOME =
File.expand_path File.dirname(__FILE__)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(custid = nil, key = nil, opts = {}) ⇒ API

Returns a new instance of API.



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

def initialize custid=nil, key=nil, opts={}
  unless ENV['ONETIME_HOST'].to_s.empty?
    self.class.base_uri ENV['ONETIME_HOST'] 
  end
  @apiversion = opts.delete(:apiversion) || opts.delete('apiversion') || 1
  @opts = opts
  @default_params = {}
  @custid = custid || ENV['ONETIME_CUSTID']
  @key = key || ENV['ONETIME_APIKEY']
  if @custid.to_s.empty? && @key.to_s.empty?
    @anonymous = true
  elsif @custid.to_s.empty? || @key.to_s.empty?
    raise RuntimeError, "You provided a custid without an apikey" if @key.to_s.empty?
    raise RuntimeError, "You provided an apikey without a custid" if @custid.to_s.empty?
  else
    @anonymous = false
    opts[:basic_auth] ||= { :username => @custid, :password => @key }
  end
end

Instance Attribute Details

#anonymousObject (readonly)

Returns the value of attribute anonymous.



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

def anonymous
  @anonymous
end

#apiversionObject

Returns the value of attribute apiversion.



55
56
57
# File 'lib/onetime/api.rb', line 55

def apiversion
  @apiversion
end

#custidObject (readonly)

Returns the value of attribute custid.



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

def custid
  @custid
end

#default_paramsObject (readonly)

Returns the value of attribute default_params.



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

def default_params
  @default_params
end

#keyObject (readonly)

Returns the value of attribute key.



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

def key
  @key
end

#optsObject (readonly)

Returns the value of attribute opts.



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

def opts
  @opts
end

#responseObject (readonly)

Returns the value of attribute response.



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

def response
  @response
end

Class Method Details

.indifferent_hashObject



124
125
126
# File 'lib/onetime/api.rb', line 124

def indifferent_hash
  Hash.new {|hash,key| hash[key.to_s] if Symbol === key }
end

.indifferent_params(params) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/onetime/api.rb', line 107

def indifferent_params(params)
  if params.is_a?(Hash)
    params = indifferent_hash.merge(params)
    params.each do |key, value|
      next unless value.is_a?(Hash) || value.is_a?(Array)
      params[key] = indifferent_params(value)
    end
  elsif params.is_a?(Array)
    params.collect! do |value|
      if value.is_a?(Hash) || value.is_a?(Array)
        indifferent_params(value)
      else
        value
      end
    end
  end
end

.web_path(*args) ⇒ Object



102
103
104
105
106
# File 'lib/onetime/api.rb', line 102

def web_path *args
  args.unshift [''] # force leading slash
  path = args.flatten.join('/')
  path.gsub '//', '/'
end

.web_uri(*args) ⇒ Object



97
98
99
100
101
# File 'lib/onetime/api.rb', line 97

def web_uri *args
  uri = URI.parse(OT::API.base_uri)
  uri.path = web_path *args
  uri
end

Instance Method Details

#api_path(*args) ⇒ Object



85
86
87
88
89
# File 'lib/onetime/api.rb', line 85

def api_path *args
  args.unshift ['', "v#{apiversion}"] # force leading slash and version
  path = args.flatten.join('/')
  path.gsub '//', '/'
end

#get(path, params = nil) ⇒ Object



75
76
77
78
79
# File 'lib/onetime/api.rb', line 75

def get path, params=nil
  opts = self.opts.clone
  opts[:query] = (params || {}).merge default_params
  execute_request :get, path, opts
end

#post(path, params = nil) ⇒ Object



80
81
82
83
84
# File 'lib/onetime/api.rb', line 80

def post path, params=nil
  opts = self.opts.clone
  opts[:body] = (params || {}).merge default_params
  execute_request :post, path, opts
end