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

Instance Method Details

#access_credentials(etsy_account) ⇒ Object



66
67
68
69
70
# File 'lib/betsy/model.rb', line 66

def access_credentials()
  header = {x_api_key: Betsy.api_key}
  header[:Authorization] = "Bearer #{.access_token}" if .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()
  if .token_expired?
    options = {
      grant_type: "refresh_token",
      refresh_token: .refresh_token,
      client_id: Betsy.api_key
    }
    response = JSON.parse(Net::HTTP.post_form(URI("https://api.etsy.com/v3/public/oauth/token"), options).body)

    if response["access_token"].nil?
      raise "Etsy token refresh failed: #{response}"
    end

    .access_token = response["access_token"]
    .expires_in = response["expires_in"]
    .refresh_token = response["refresh_token"]
    .last_token_refresh = DateTime.now
    .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, options = {})
  check_token_expiration(options[:etsy_account]) if options[:etsy_account]
  headers = access_credentials(options[:etsy_account])
  options.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 = options.to_json
  else
    uri.query = URI.encode_www_form(options) if options.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