Class: Teachable::Jg::Client
- Inherits:
-
Object
- Object
- Teachable::Jg::Client
- Includes:
- HTTParty
- Defined in:
- lib/teachable/jg/client.rb
Instance Attribute Summary collapse
-
#authorized ⇒ Object
Returns the value of attribute authorized.
-
#endpoint ⇒ Object
Returns the value of attribute endpoint.
Instance Method Summary collapse
- #build_path(registration) ⇒ Object
- #confirm_status(options = {}) ⇒ Object
- #create_order(options) ⇒ Object
- #delete_order(options) ⇒ Object
- #destroy_order(path, options) ⇒ Object
- #get(path, options) ⇒ Object
- #has_required_attributes?(options, *attributes) ⇒ Boolean
-
#initialize(options = {}) ⇒ Client
constructor
A new instance of Client.
- #orders(options) ⇒ Object
- #post_to_orders(path, options) ⇒ Object
- #post_to_users(options) ⇒ Object
- #process_body(body) ⇒ Object
- #user_info(options = {}) ⇒ Object
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(={}) = Teachable::Jg..merge() Teachable::Jg::Configuration::VALID_CONFIG_KEYS.each do |key| send("#{key}=", [key]) end @endpoint = Teachable::Jg::Configuration::DEFAULT_ENDPOINT @status_message = confirm_status() end |
Instance Attribute Details
#authorized ⇒ Object
Returns the value of attribute authorized.
11 12 13 |
# File 'lib/teachable/jg/client.rb', line 11 def @authorized end |
#endpoint ⇒ Object
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(={}) if has_required_attributes?(, :email, :password) resp = post_to_users() if resp.code == 200 body = process_body(resp.body) self.delivered = true if body["success"] self. = 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() if path = Teachable::Jg::Configuration::ORDERS_ENDPOINT if has_required_attributes?(, :total, :total_quantity, :email, :user_email, :user_token) resp = post_to_orders(path, ) 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() if path = Teachable::Jg::Configuration::ORDERS_ENDPOINT if has_required_attributes?(, :order_id, :user_email, :user_token) resp = destroy_order(path, ) 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, ) path_with_params = path + "/#{[:order_id]}?user_email=#{[:user_email]}&user_token=#{[: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, ) user_headers = headers.reject {|key| key == "Accept" } query = { user_email: [:user_email], user_token: [: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
198 199 200 201 202 203 204 205 206 |
# File 'lib/teachable/jg/client.rb', line 198 def has_required_attributes?(, *attributes) attributes << :password_confirmation if [:registration] return false if attributes.detect do |attr| !(.has_key?(attr) && ![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() if path = Teachable::Jg::Configuration::ORDERS_ENDPOINT if has_required_attributes?(, :user_email, :user_token) resp = get(path, ) 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, ) query = {order: { "total" => [:total], "total_quantity" => [:total_quantity], "email" => [:email], "special_instructions" => [:special_instructions] }} path_with_params = path + "?user_email=#{[:user_email]}&user_token=#{[: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() path = endpoint + build_path([:registration]) query = {user: { "email" => [:email], "password" => [:password], "password_confirmation" => [: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 user_info(={}) if path = Teachable::Jg::Configuration::CURRENT_USER_ENDPOINT if has_required_attributes?(, :user_email, :user_token) resp = get(path, ) 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 |