Module: Teachable::Stats

Extended by:
Configuration
Defined in:
lib/teachable/stats.rb,
lib/teachable/stats/users.rb,
lib/teachable/stats/signup.rb,
lib/teachable/stats/version.rb,
lib/teachable/stats/get_token.rb,
lib/teachable/stats/orders_get.rb,
lib/teachable/stats/authenticate.rb,
lib/teachable/stats/orders_create.rb,
lib/teachable/stats/orders_destroy.rb

Constant Summary collapse

VERSION =
"0.1.1"

Class Method Summary collapse

Methods included from Configuration

configuration, define_setting

Class Method Details

.authenticate(email:, password:) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/teachable/stats/authenticate.rb', line 5

def self.authenticate(email:, password:)
  begin
    user_credentials = get_token(email: email, password: password)
    raise RegistrationError if failed_authentication?(user_credentials)
    self.user_email, self.user_token = user_credentials["email"], user_credentials["tokens"]
    "Logged in as user: #{self.user_email} with token: #{self.user_token}"
  rescue => e
    if e.class == RegistrationError
      "#{e.message}"
    else
      errors = JSON.parse(e.response)
      errors_formatted = errors["error"]
      "Something went wrong when trying to register. These are your errors: #{errors_formatted}"
    end
  end
end

.create_my_order(number: 1, total:, total_quantity:, email:) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/teachable/stats/orders_create.rb', line 5

def self.create_my_order(number: 1, total:, total_quantity:, email:)
  begin
    base_url = "https://fast-bayou-75985.herokuapp.com/api/orders.json"
    response_total_orders = []
    number.times do
      response_total_orders << (RestClient.post base_url,
                              { order: {
                                  total: total,
                                  total_quantity: total_quantity,
                                  email: email,
                                  special_instructions: "special instructions foo bar"
                                }
                              },
                              params: { user_email: self.user_email,
                                          user_token: self.user_token
                              })
    end
    response_total_orders.map do |response|
      JSON.parse(response.body)
    end
  rescue => e
    if e.class == RestClient::Unauthorized
      errors = JSON.parse(e.response)
      errors["error"]
    else
      errors = JSON.parse(e.response)
      errors_formatted = errors["errors"].to_s + errors["error"].to_s
      "Something went wrong when trying to create that order. These are your errors: #{errors_formatted}"
    end
  end
end

.destroy_my_order(order_id:) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/teachable/stats/orders_destroy.rb', line 5

def self.destroy_my_order(order_id: )
  begin
    base_url = "https://fast-bayou-75985.herokuapp.com/api/orders/#{order_id}.json"
    response = RestClient.delete base_url,
                                params: { user_email: self.user_email,
                                          user_token: self.user_token
                                }
    "Order with id: #{order_id} deleted." if response == ""
  rescue => e
    if e.class == RestClient::Unauthorized
      errors = JSON.parse(e.response)
      errors_formatted = errors["error"]
      "#{errors_formatted}"
    else
      errors = JSON.parse(e.response)
      errors_formatted = "That order_id does not exist" if errors["error"] == "Not Found"
      "Something went wrong when trying to delete that order. These are your errors: #{errors_formatted}"
    end
  end
end

.get_ordersObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/teachable/stats/orders_get.rb', line 5

def self.get_orders
  begin
    base_url = "https://fast-bayou-75985.herokuapp.com/api/orders.json"
    header = { accept: :json }
    response = RestClient.get base_url,
                              params: {
                                  user_email: self.user_email,
                                  user_token: self.user_token
                              }
    JSON.parse(response.body)
  rescue => e
    errors = JSON.parse(e.response)
    errors_formatted = errors["error"]
    "#{errors_formatted}"
  end
end

.get_token(email:, password:) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/teachable/stats/get_token.rb', line 5

def self.get_token(email:, password:)
  begin
    base_url = "https://fast-bayou-75985.herokuapp.com/users/sign_in.json"
    header = { accept: :json }
    response = RestClient.post base_url,
                                { "user": {
                                        "email": email,
                                        "password": password
                                        }
                                },
                                header
    JSON.parse(response.body)
  rescue => e
    if e.class == RestClient::Unauthorized
      errors = JSON.parse(e.response)
      errors_formatted = errors["error"]
      "Something went wrong when trying to get your user info and token. These are your errors: #{errors_formatted}"
    end
  end
end

.get_userObject



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/teachable/stats/users.rb', line 5

def self.get_user
  begin
    base_url = "https://fast-bayou-75985.herokuapp.com/api/users/current_user/edit.json"
    response = RestClient.get base_url, params: { user_email: self.user_email,
                                                  user_token: self.user_token }
    JSON.parse(response.body)
  rescue => e
    errors = JSON.parse(e.response)
    errors_formatted = errors["error"]
    "Something went wrong when trying to get your user info. These are your errors: #{errors_formatted}"
  end
end

.register(email:, password:, password_confirmation:) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/teachable/stats/signup.rb', line 5

def self.register(email: , password:, password_confirmation:)
  begin
    base_url = "https://fast-bayou-75985.herokuapp.com/users.json"
    header = { accept: :json }
    response = RestClient.post base_url,
                              { "user" => { "email": email,
                                            "password": password,
                                            "password_confirmation": password_confirmation
                                          }
                              },
                              header
    JSON.parse(response.body)
  rescue => e
    errors = JSON.parse(e.response)
    errors_formatted = errors["errors"].map do |error|
      error[0] + " " + error[1][0]
    end.join(", ")
    "Something went wrong when trying to register. These are your errors: #{errors_formatted}"
  end
end