Created
February 3, 2020 03:32
-
-
Save rayray-py/743cf6716ca62283ad2274b732319ed1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'open-uri' | |
| require 'uri' | |
| require 'net/https' | |
| class ImageSearch | |
| include ActiveModel::Model | |
| attr_accessor :image_base64 | |
| def initialize(attrs) | |
| @image_base64 = Base64.strict_encode64(open("https://kichijoji-atsumaru-fudosan.com/blog/wp-content/uploads/2019/09/floor-cushion-picture-3.jpg").read) | |
| end | |
| def get_result | |
| url = URI(Settings.image_search.url) | |
| request_params = { | |
| InstanceName: Settings.image_search.instance, | |
| PicContent: image_base64, | |
| CategoryId: 9 | |
| } | |
| http = Net::HTTP.new(url.host, url.port) | |
| http.use_ssl = true | |
| http.verify_mode = OpenSSL::SSL::VERIFY_NONE | |
| request = Net::HTTP::Post.new(url) | |
| request["date"] = Time.now().httpdate.to_s | |
| request["accept"] = 'application/json' | |
| request["x-acs-version"] = Settings.image_search.api_version | |
| request["x-acs-signature-method"] = Settings.image_search.signature_method | |
| request["x-acs-signature-nonce"] = SecureRandom.hex() | |
| request["cache-control"] = 'no-cache' | |
| request.set_form_data(request_params) | |
| request["content-type"] = Settings.image_search.content_type | |
| request["content-md5"] = Base64.strict_encode64(Digest::MD5.digest(request.body)) | |
| header_string_to_sign = | |
| "POST" + "\n" + | |
| request["accept"] + "\n" + | |
| request["content-md5"] + "\n" + | |
| request["content-type"] + "\n" + | |
| request["date"] + "\n" + | |
| "x-acs-signature-method:HMAC-SHA1\n" + | |
| "x-acs-signature-nonce:" + request["x-acs-signature-nonce"] + "\n" + | |
| "x-acs-version:2019-03-25" + "\n" | |
| resource_string_to_sign = url.path | |
| string_to_sign = header_string_to_sign + resource_string_to_sign | |
| signature = Base64.strict_encode64(OpenSSL::HMAC.digest('sha1', Settings.image_search.access_key_secret, string_to_sign)) | |
| request["authorization"] = "acs " + Settings.image_search.access_key_id + ":" + signature | |
| response = http.request(request) | |
| return JSON.parse(response.body) | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment