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

HOME =
File.expand_path File.join(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.



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/onetime/api.rb', line 62

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.



60
61
62
# File 'lib/onetime/api.rb', line 60

def anonymous
  @anonymous
end

#apiversionObject

Returns the value of attribute apiversion.



61
62
63
# File 'lib/onetime/api.rb', line 61

def apiversion
  @apiversion
end

#custidObject (readonly)

Returns the value of attribute custid.



60
61
62
# File 'lib/onetime/api.rb', line 60

def custid
  @custid
end

#default_paramsObject (readonly)

Returns the value of attribute default_params.



60
61
62
# File 'lib/onetime/api.rb', line 60

def default_params
  @default_params
end

#keyObject (readonly)

Returns the value of attribute key.



60
61
62
# File 'lib/onetime/api.rb', line 60

def key
  @key
end

#optsObject (readonly)

Returns the value of attribute opts.



60
61
62
# File 'lib/onetime/api.rb', line 60

def opts
  @opts
end

#responseObject (readonly)

Returns the value of attribute response.



60
61
62
# File 'lib/onetime/api.rb', line 60

def response
  @response
end

Class Method Details

.indifferent_hashObject



130
131
132
# File 'lib/onetime/api.rb', line 130

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

.indifferent_params(params) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/onetime/api.rb', line 113

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



108
109
110
111
112
# File 'lib/onetime/api.rb', line 108

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

.web_uri(*args) ⇒ Object



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

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



91
92
93
94
95
# File 'lib/onetime/api.rb', line 91

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



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

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



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

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