Module: Betsy::Model::ClassMethods
- Defined in:
- lib/betsy/model.rb
Constant Summary collapse
- BASE_ETSY_API_URL =
"https://openapi.etsy.com"- REQUEST_CLASSES =
{ get: Net::HTTP::Get, post: Net::HTTP::Post, put: Net::HTTP::Put, patch: Net::HTTP::Patch, delete: Net::HTTP::Delete }.freeze
Instance Method Summary collapse
- #access_credentials(etsy_account) ⇒ Object
- #attribute(name) ⇒ Object
- #build_objects(response) ⇒ Object
- #check_token_expiration(etsy_account) ⇒ Object
- #handle_response(response) ⇒ Object
- #make_request(request_type, endpoint, options = {}) ⇒ Object
Instance Method Details
#access_credentials(etsy_account) ⇒ Object
66 67 68 69 70 |
# File 'lib/betsy/model.rb', line 66 def access_credentials(etsy_account) header = {x_api_key: Betsy.api_key} header[:Authorization] = "Bearer #{etsy_account.access_token}" if etsy_account.present? header end |
#attribute(name) ⇒ Object
6 7 8 9 10 |
# File 'lib/betsy/model.rb', line 6 def attribute(name) define_method name do @result[name.to_s] end end |
#build_objects(response) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/betsy/model.rb', line 82 def build_objects(response) if response["count"].nil? objects = new(response) else objects = [] response["results"].each do |data| objects.append(new(data)) end objects end end |
#check_token_expiration(etsy_account) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/betsy/model.rb', line 45 def check_token_expiration(etsy_account) if etsy_account.token_expired? = { grant_type: "refresh_token", refresh_token: etsy_account.refresh_token, client_id: Betsy.api_key } response = JSON.parse(Net::HTTP.post_form(URI("https://api.etsy.com/v3/public/oauth/token"), ).body) if response["access_token"].nil? raise "Etsy token refresh failed: #{response}" end etsy_account.access_token = response["access_token"] etsy_account.expires_in = response["expires_in"] etsy_account.refresh_token = response["refresh_token"] etsy_account.last_token_refresh = DateTime.now etsy_account.save end end |
#handle_response(response) ⇒ Object
72 73 74 75 76 77 78 79 80 |
# File 'lib/betsy/model.rb', line 72 def handle_response(response) if response.code.to_i == 200 return nil if response.body.empty? response = JSON.parse(response.body) build_objects(response) else Betsy::Error.new(JSON.parse(response.body).merge!("status" => response.code.to_i)) end end |
#make_request(request_type, endpoint, options = {}) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/betsy/model.rb', line 20 def make_request(request_type, endpoint, = {}) check_token_expiration([:etsy_account]) if [:etsy_account] headers = access_credentials([:etsy_account]) .delete(:etsy_account) uri = URI("#{BASE_ETSY_API_URL}#{endpoint}") if [:post, :put, :patch].include?(request_type) headers = headers.reverse_merge(content_type: "application/json") request = REQUEST_CLASSES[request_type].new(uri) request.body = .to_json else uri.query = URI.encode_www_form() if .any? request = REQUEST_CLASSES[request_type].new(uri) end headers.each { |key, value| request[key.to_s] = value } response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == "https") do |http| http.request(request) end handle_response(response) end |