Class: Onetime::API

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

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.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/onetime/api.rb', line 45

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') || 2
  @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.



44
45
46
# File 'lib/onetime/api.rb', line 44

def anonymous
  @anonymous
end

#apiversionObject (readonly)

Returns the value of attribute apiversion.



44
45
46
# File 'lib/onetime/api.rb', line 44

def apiversion
  @apiversion
end

#custidObject (readonly)

Returns the value of attribute custid.



44
45
46
# File 'lib/onetime/api.rb', line 44

def custid
  @custid
end

#default_paramsObject (readonly)

Returns the value of attribute default_params.



44
45
46
# File 'lib/onetime/api.rb', line 44

def default_params
  @default_params
end

#keyObject (readonly)

Returns the value of attribute key.



44
45
46
# File 'lib/onetime/api.rb', line 44

def key
  @key
end

#optsObject (readonly)

Returns the value of attribute opts.



44
45
46
# File 'lib/onetime/api.rb', line 44

def opts
  @opts
end

#responseObject (readonly)

Returns the value of attribute response.



44
45
46
# File 'lib/onetime/api.rb', line 44

def response
  @response
end

Class Method Details

.extract_secret_key(value, api_base_uri = base_uri) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/onetime/api.rb', line 142

def extract_secret_key(value, api_base_uri=base_uri)
  return value unless value

  uri = URI.parse(value.to_s)
  return value unless uri.host && uri.path
  return value unless accepted_secret_host?(uri.host, api_base_uri)

  match = uri.path.match(%r{\A/secret/([a-zA-Z0-9]+)\z})
  match ? match[1] : value
rescue URI::InvalidURIError
  value
end

.indifferent_hashObject



139
140
141
# File 'lib/onetime/api.rb', line 139

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

.indifferent_params(params) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/onetime/api.rb', line 122

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

.receipt_key_from_response(response) ⇒ Object



171
172
173
# File 'lib/onetime/api.rb', line 171

def receipt_key_from_response(response)
  response&.dig('record', 'receipt', 'key') || response&.dig('record', 'metadata', 'key')
end

.recipients_from_response(response) ⇒ Object



175
176
177
178
179
180
181
# File 'lib/onetime/api.rb', line 175

def recipients_from_response(response)
  recipients = response&.dig('record', 'receipt', 'recipients')
  if recipients.nil? || recipients == '' || (recipients.is_a?(Array) && recipients.empty?)
    recipients = response&.dig('details', 'recipient')
  end
  Array(recipients).flatten.compact.reject { |recipient| recipient.to_s.empty? }
end

.response_error_message(response) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
# File 'lib/onetime/api.rb', line 155

def response_error_message(response)
  return 'Could not complete request' if response.nil?

  # Symbol lookups preserve behavior for plain Ruby hashes even though
  # parsed API responses usually support indifferent access.
  ['message', :message, 'error', :error, 'field', :field].each do |key|
    value = response[key] if response.respond_to?(:[])
    return value.to_s unless value.to_s.empty?
  end
  response.to_s
end

.secret_key_from_response(response) ⇒ Object



167
168
169
# File 'lib/onetime/api.rb', line 167

def secret_key_from_response(response)
  response&.dig('record', 'secret', 'key')
end

.web_path(*args) ⇒ Object



117
118
119
120
121
# File 'lib/onetime/api.rb', line 117

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

.web_uri(*args) ⇒ Object



112
113
114
115
116
# File 'lib/onetime/api.rb', line 112

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



94
95
96
97
98
# File 'lib/onetime/api.rb', line 94

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



64
65
66
67
68
# File 'lib/onetime/api.rb', line 64

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, request_opts = {}) ⇒ Object



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
# File 'lib/onetime/api.rb', line 69

def post path, params=nil, request_opts={}
  opts = self.opts.clone
  wrap = if request_opts.key?(:wrap)
    request_opts[:wrap]
  elsif request_opts.key?('wrap')
    request_opts['wrap']
  else
    :auto
  end
  body_params = (params || {}).merge default_params

  # V2 API uses JSON format
  opts[:headers] = (opts[:headers] || {}).merge({
    'Content-Type' => 'application/json',
    'Accept' => 'application/json'
  })

  if wrap_secret_body?(path, wrap)
    body_params = { secret: body_params }
  end

  opts[:body] = body_params.to_json

  execute_request :post, path, opts
end