Module: Betsy

Defined in:
lib/betsy.rb,
lib/betsy/shop.rb,
lib/betsy/user.rb,
lib/betsy/error.rb,
lib/betsy/model.rb,
lib/betsy/other.rb,
lib/betsy/engine.rb,
lib/betsy/review.rb,
lib/betsy/payment.rb,
lib/betsy/version.rb,
lib/betsy/ledger_entry.rb,
lib/betsy/shop_listing.rb,
lib/betsy/shop_receipt.rb,
lib/betsy/shop_section.rb,
lib/betsy/user_address.rb,
lib/betsy/seller_taxonomy.rb,
lib/betsy/shop_listing_file.rb,
lib/betsy/shop_listing_image.rb,
lib/betsy/shop_listing_product.rb,
lib/betsy/shop_listing_offering.rb,
lib/betsy/shop_shipping_profile.rb,
lib/betsy/shop_listing_inventory.rb,
lib/betsy/shop_production_partner.rb,
lib/betsy/shop_listing_translation.rb,
lib/betsy/shop_receipt_transaction.rb,
lib/betsy/shop_listing_variation_image.rb,
lib/generators/betsy/install_generator.rb

Defined Under Namespace

Modules: Generators, Model Classes: Engine, Error, LedgerEntry, Other, Payment, ResponseListenerController, Review, SellerTaxonomy, Shop, ShopListing, ShopListingFile, ShopListingImage, ShopListingInventory, ShopListingOffering, ShopListingProduct, ShopListingTranslation, ShopListingVariationImage, ShopProductionPartner, ShopReceipt, ShopReceiptTransaction, ShopSection, ShopShippingProfile, User, UserAddress

Constant Summary collapse

ALL_SCOPES =
["address_r",
"address_w",
"billing_r",
"cart_r",
"cart_w",
"email_r",
"favorites_r",
"favorites_w",
"feedback_r",
"listings_d",
"listings_r",
"listings_w",
"profile_r",
"profile_w",
"recommend_r",
"recommend_w",
"shops_r",
"shops_w",
"transactions_r",
"transactions_w"]
CODE_CHALLENGE_CHARACTERS =
("A".."Z").to_a + ("a".."z").to_a + ("0".."9").to_a + [".", "_", "~", "-"]
VERSION =
"0.2.1"

Class Method Summary collapse

Class Method Details

.account_classObject



109
110
111
# File 'lib/betsy.rb', line 109

def self.
  @@account_class ||= .constantize
end

.authorization_url(scope: ALL_SCOPES) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/betsy.rb', line 61

def self.authorization_url(scope: ALL_SCOPES)
  if api_key.nil? && redirect_uri_base.nil?
    raise "Betsy.api_key and Betsy.redirect_uri_base must be set"
  elsif api_key.nil?
    raise "Betsy.api_key must be set"
  elsif redirect_uri_base.nil?
    raise "Betsy.redirect_uri_base must be set"
  end

  redirect_uri = "#{redirect_uri_base}/etsy_response_listener"
  scope = scope.join("%20")
  state = generate_state
  code_verifier = generate_code_verifier
  code_challenge = Digest::SHA256.base64digest(code_verifier).tr("+/", "-_").tr("=", "")

  .create(state: state, code_verifier: code_verifier)

  "https://www.etsy.com/oauth/connect" \
  "?response_type=code" \
  "&client_id=#{api_key}" \
  "&redirect_uri=#{redirect_uri}" \
  "&scope=#{scope}" \
  "&state=#{state}" \
  "&code_challenge=#{code_challenge}" \
  "&code_challenge_method=S256"
end

.request_access_token(params) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/betsy.rb', line 88

def self.request_access_token(params)
   = .find_by(state: params[:state])
  if .present?
    options = {
      grant_type: "authorization_code",
      client_id: api_key,
      redirect_uri: "#{redirect_uri_base}/etsy_response_listener",
      code: params[:code],
      code_verifier: .code_verifier
    }
    response = JSON.parse(Faraday.post("https://api.etsy.com/v3/public/oauth/token", options).body)
    .access_token = response["access_token"]
    .refresh_token = response["refresh_token"]
    .expires_in = response["expires_in"]
    .last_token_refresh = DateTime.now
    .save
  else
    raise "The state provided to /etsy_response_listener was an invalid state, this could be a sign of a CSRF attack"
  end
end