Class: Whiplash::App

Inherits:
Object
  • Object
show all
Extended by:
ApiConfig, Signing
Includes:
Connections, FinderMethods
Defined in:
lib/whiplash/app.rb,
lib/whiplash/app/railtie.rb,
lib/whiplash/app/signing.rb,
lib/whiplash/app/version.rb,
lib/whiplash/app/api_config.rb,
lib/whiplash/app/connections.rb,
lib/whiplash/app/canonical_host.rb,
lib/whiplash/app/finder_methods.rb,
lib/whiplash/app/controller_helpers.rb

Defined Under Namespace

Modules: ApiConfig, CanonicalHost, Connections, ControllerHelpers, FinderMethods, Signing Classes: Railtie

Constant Summary collapse

VERSION =
"0.10.2"

Constants included from Connections

Connections::PER_PAGE_REQUEST_LIMIT

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Signing

signature, verified?

Methods included from ApiConfig

api_url, rate_limit

Methods included from FinderMethods

#count, #create, #destroy, #find, #find_all, #update

Methods included from Connections

#app_request, #app_request!, #base_app_request, #delete, #delete!, #error_codes, #error_response, #get, #get!, #get_body_or_empty, #get_context, #multi_page_get!, #post, #post!, #put, #put!, #sanitize_headers, #select_error, #store_whiplash_error!

Constructor Details

#initialize(token, options = {}) ⇒ App

Returns a new instance of App.



28
29
30
31
32
33
34
# File 'lib/whiplash/app.rb', line 28

def initialize(token, options={})
  @token = format_token(token)
  @customer_id = options[:customer_id]
  @shop_id = options[:shop_id]
  @api_version = options[:api_version] || 2 # can be 2_1
  @retry_errors = options.fetch(:retry_errors, false)
end

Instance Attribute Details

#customer_idObject

Returns the value of attribute customer_id.



26
27
28
# File 'lib/whiplash/app.rb', line 26

def customer_id
  @customer_id
end

#retry_errorsObject

Returns the value of attribute retry_errors.



26
27
28
# File 'lib/whiplash/app.rb', line 26

def retry_errors
  @retry_errors
end

#shop_idObject

Returns the value of attribute shop_id.



26
27
28
# File 'lib/whiplash/app.rb', line 26

def shop_id
  @shop_id
end

#tokenObject

Returns the value of attribute token.



26
27
28
# File 'lib/whiplash/app.rb', line 26

def token
  @token
end

Class Method Details

.client_credentials_tokenObject



106
107
108
109
# File 'lib/whiplash/app.rb', line 106

def client_credentials_token
  client = OAuth2::Client.new(ENV["WHIPLASH_CLIENT_ID"], ENV["WHIPLASH_CLIENT_SECRET"], site: api_url)
  client.client_credentials.get_token(scope: ENV["WHIPLASH_CLIENT_SCOPE"])
end

Instance Method Details

#clientObject



40
41
42
# File 'lib/whiplash/app.rb', line 40

def client
  OAuth2::Client.new(ENV["WHIPLASH_CLIENT_ID"], ENV["WHIPLASH_CLIENT_SECRET"], site: self.class.api_url)
end

#connectionObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/whiplash/app.rb', line 44

def connection
  Faraday.new [self.class.api_url, versioned_api_url].join("/") do |conn|
    # Authentication
    conn.request :authorization, 'Bearer', token.token
    conn.request :json

    if @retry_errors
      conn.request :retry, max: 5, 
        interval: 1, 
        interval_randomness: 0.5, 
        backoff_factor: 2,
        methods: [:get, :post, :put, :delete],
        exceptions: [
          'Faraday::TimeoutError', 
          'Faraday::ConnectionFailed',
          'Errno::ETIMEDOUT', 
          'Timeout::Error'
        ],
        retry_statuses: [502, 504]
    end

    conn.response :json, :content_type => /\bjson$/
    conn.options.timeout = 30 # Total request timeout
    conn.options.open_timeout = 5 # Connection establishment timeout
    
    # Use persistent HTTP connections (requires net-http-persistent gem)
    begin
      conn.adapter :net_http_persistent
    rescue LoadError
      # Fallback to default adapter if net-http-persistent isn't available
      Rails.logger.warn "[WhiplashApp] net-http-persistent gem not found, using default adapter" if defined?(Rails)
      conn.adapter Faraday.default_adapter
    end
  end
end

#refresh_token!Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/whiplash/app.rb', line 84

def refresh_token!
  case ENV["WHIPLASH_CLIENT_SCOPE"]
  when /app_(manage|read)/
    begin
      access_token = self.class.client_credentials_token
    rescue URI::InvalidURIError => e
      raise StandardError, "The provided URL (#{ENV["WHIPLASH_API_URL"]}) is not valid"
    end
  else
    raise StandardError, "You must request an access token before you can refresh it" if token.nil?
    raise StandardError, "Token must either be a Hash or an OAuth2::AccessToken" unless token.is_a?(OAuth2::AccessToken)
    access_token = token.refresh!
  end
  self.token = access_token
end

#token_expired?Boolean

Returns:

  • (Boolean)


100
101
102
103
# File 'lib/whiplash/app.rb', line 100

def token_expired?
  return token.expired? unless token.nil?
  false
end

#versioned_api_urlObject



36
37
38
# File 'lib/whiplash/app.rb', line 36

def versioned_api_url
  "api/v#{@api_version}"
end