Class: Primecontext::Api
- Inherits:
-
Object
- Object
- Primecontext::Api
- Defined in:
- lib/primecontext/api.rb
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #generate_sign(data) ⇒ Object
-
#initialize(options = {}) ⇒ Api
constructor
A new instance of Api.
- #new_record(data = {}) ⇒ Object
- #send_request(data) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Api
Returns a new instance of Api.
10 11 12 13 |
# File 'lib/primecontext/api.rb', line 10 def initialize( = {}) @options = Primecontext::.merge() @data = { user_id: @options[:user_id] } end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
8 9 10 |
# File 'lib/primecontext/api.rb', line 8 def data @data end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
8 9 10 |
# File 'lib/primecontext/api.rb', line 8 def @options end |
Instance Method Details
#generate_sign(data) ⇒ Object
21 22 23 24 25 |
# File 'lib/primecontext/api.rb', line 21 def generate_sign(data) data_str = data.sort.join.mb_chars.downcase digest = OpenSSL::Digest.new 'sha256' OpenSSL::HMAC.hexdigest digest, @options[:secret_key], data_str end |
#new_record(data = {}) ⇒ Object
15 16 17 18 19 |
# File 'lib/primecontext/api.rb', line 15 def new_record(data = {}) @data = @data.merge(Hash[data.map{ |(k,v)| [k.to_sym, v] }]) @data.merge!({ sign: generate_sign(@data) }) send_request(@data) end |
#send_request(data) ⇒ Object
27 28 29 30 31 32 33 |
# File 'lib/primecontext/api.rb', line 27 def send_request(data) uri = URI.parse(@options[:api_url]) http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE http.request_post(uri.request_uri, URI.encode_www_form(data)) end |