Class: Teachable::Jg::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/teachable/jg/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



13
14
15
16
17
18
19
20
21
22
# File 'lib/teachable/jg/client.rb', line 13

def initialize(options={})
  merged_options = Teachable::Jg.options.merge(options)

  Teachable::Jg::Configuration::VALID_CONFIG_KEYS.each do |key|
    send("#{key}=", merged_options[key])
  end

  @endpoint = Teachable::Jg::Configuration::DEFAULT_ENDPOINT
  @status_message = confirm_status(options)
end

Instance Attribute Details

#authorizedObject

Returns the value of attribute authorized.



11
12
13
# File 'lib/teachable/jg/client.rb', line 11

def authorized
  @authorized
end

#endpointObject

Returns the value of attribute endpoint.



11
12
13
# File 'lib/teachable/jg/client.rb', line 11

def endpoint
  @endpoint
end

Instance Method Details

#build_path(registration) ⇒ Object



165
166
167
# File 'lib/teachable/jg/client.rb', line 165

def build_path(registration)
  registration ? "/register" : "/sign-in"
end

#confirm_status(options = {}) ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/teachable/jg/client.rb', line 169

def confirm_status(options={})
  if has_required_attributes?(options, :email, :password)

    resp = post_to_users(options)

    if resp.code == 200
      body = process_body(resp.body)

      self.delivered = true if body["success"]
      self.authorized = true if body["login"] == "verified"

      return body
    else
      return resp.code
    end
  else
    self.delivered = false
    {"success"=>false, "user_info"=>"missing or invalid params"}
  end
end

#create_order(options) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/teachable/jg/client.rb', line 78

def create_order(options)
  if authorized
    path = Teachable::Jg::Configuration::ORDERS_ENDPOINT
    if has_required_attributes?(options, :total, :total_quantity, :email, :user_email, :user_token)
      resp = post_to_orders(path, options)
    else
      self.delivered = false
      {"success"=>false, "create_order"=>"missing or invalid params"}
    end
  else
    self.delivered = false
    {"success"=>false, "login"=>"failed to authorize"}
  end
end

#delete_order(options) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/teachable/jg/client.rb', line 108

def delete_order(options)
  if authorized
    path = Teachable::Jg::Configuration::ORDERS_ENDPOINT
    if has_required_attributes?(options, :order_id, :user_email, :user_token)
      resp = destroy_order(path, options)
    else
      self.delivered = false
      {"success"=>false, "delete_order"=>"missing or invalid params"}
    end
  else
    self.delivered = false
    {"success"=>false, "login"=>"failed to authorize"}
  end
end

#destroy_order(path, options) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/teachable/jg/client.rb', line 148

def destroy_order(path, options)
  path_with_params = path + "/#{options[:order_id]}?user_email=#{options[:user_email]}&user_token=#{options[:user_token]}"

  resp = HTTParty.delete(
    path_with_params,
    headers: headers
  )

  if resp.code == 200
    body = process_body(resp.body)
    self.delivered = true if body["success"]
    return body
  else
    return resp.code
  end
end

#get(path, options) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/teachable/jg/client.rb', line 39

def get(path, options)
  user_headers = headers.reject {|key| key == "Accept" }

  query = {
    user_email: options[:user_email],
    user_token: options[:user_token]
  }

  resp = HTTParty.get(
    path,
    query: query,
    headers: user_headers
  )

  if resp.code == 200
    body = process_body(resp.body)
    self.delivered = true if body["success"]
    return body
  else
    return resp.code
  end
end

#has_required_attributes?(options, *attributes) ⇒ Boolean

Returns:

  • (Boolean)


198
199
200
201
202
203
204
205
206
# File 'lib/teachable/jg/client.rb', line 198

def has_required_attributes?(options, *attributes)
  attributes << :password_confirmation if options[:registration]

  return false if attributes.detect do |attr|
    !(options.has_key?(attr) && !options[attr].nil?)
  end

  true
end

#orders(options) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/teachable/jg/client.rb', line 93

def orders(options)
  if authorized
    path = Teachable::Jg::Configuration::ORDERS_ENDPOINT
    if has_required_attributes?(options, :user_email, :user_token)
      resp = get(path, options)
    else
      self.delivered = false
      {"success"=>false, "get_orders"=>"missing or invalid params"}
    end
  else
    self.delivered = false
    {"success"=>false, "login"=>"failed to authorize"}
  end
end

#post_to_orders(path, options) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/teachable/jg/client.rb', line 123

def post_to_orders(path, options)
  query = {order: {
    "total"                => options[:total],
    "total_quantity"       => options[:total_quantity],
    "email"                => options[:email],
    "special_instructions" => options[:special_instructions]
  }}

  path_with_params = path + "?user_email=#{options[:user_email]}&user_token=#{options[:user_token]}"

  resp = HTTParty.post(
    path_with_params,
    query: query,
    headers: headers
  )

  if resp.code == 200
    body = process_body(resp.body)
    self.delivered = true if body["success"]
    return body
  else
    return resp.code
  end
end

#post_to_users(options) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/teachable/jg/client.rb', line 62

def post_to_users(options)
  path = endpoint + build_path(options[:registration])

  query = {user: {
    "email"                 => options[:email],
    "password"              => options[:password],
    "password_confirmation" => options[:password_confirmation]
  }}

  resp = HTTParty.post(
    path,
    query: query,
    headers: headers
  )
end

#process_body(body) ⇒ Object



190
191
192
193
194
195
196
# File 'lib/teachable/jg/client.rb', line 190

def process_body(body)
  if body.is_a?(String)
    JSON.parse(body)
  else
    {"success"=>false, "login"=>"no json response"}
  end
end

#user_info(options = {}) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/teachable/jg/client.rb', line 24

def (options={})
  if authorized
    path = Teachable::Jg::Configuration::CURRENT_USER_ENDPOINT
    if has_required_attributes?(options, :user_email, :user_token)
      resp = get(path, options)
    else
      self.delivered = false
      {"success"=>false, "user_info"=>"missing or invalid params"}
    end
  else
    self.delivered = false
    {"success"=>false, "login"=>"failed to authorize"}
  end
end