Module: Uatu::Connection
- Included in:
- Base
- Defined in:
- lib/uatu/connection.rb
Instance Method Summary collapse
- #build_connection(conn_options) ⇒ Object
- #build_route(method, options = {}) ⇒ Object
- #current_timestamp ⇒ Object
- #hash(timestamp, conn_options) ⇒ Object
- #mandatory_params(conn_options) ⇒ Object
- #prepare_options(options) ⇒ Object
- #request(method, options, conn_options) ⇒ Object
- #unrubify(name) ⇒ Object
- #valid_method(method) ⇒ Object
Instance Method Details
#build_connection(conn_options) ⇒ Object
15 16 17 18 19 20 21 22 |
# File 'lib/uatu/connection.rb', line 15 def build_connection() Faraday.new(url: .base_url) do |faraday| faraday.use Uatu::Response::RaiseMarvelError faraday.request :url_encoded faraday.adapter Faraday.default_adapter end end |
#build_route(method, options = {}) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/uatu/connection.rb', line 24 def build_route(method, ={}) route = "/v1/public/#{valid_method(method)}" if resource_id = ["#{valid_method(method).singularize}_id".to_sym] route += "/#{resource_id}" end # If it is combined, it comes afet the '_' if method.split('_').count>1 && combined_path = method.split('_').last route += "/#{combined_path}" end route end |
#current_timestamp ⇒ Object
66 67 68 |
# File 'lib/uatu/connection.rb', line 66 def DateTime.now.to_s end |
#hash(timestamp, conn_options) ⇒ Object
70 71 72 |
# File 'lib/uatu/connection.rb', line 70 def hash(, ) Digest::MD5.hexdigest("#{}#{.private_key}#{.public_key}") end |
#mandatory_params(conn_options) ⇒ Object
74 75 76 77 |
# File 'lib/uatu/connection.rb', line 74 def mandatory_params() ts = {apikey: .public_key, ts: ts, hash: hash(ts, )} end |
#prepare_options(options) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/uatu/connection.rb', line 38 def () valid_opts = {} # We remove innecessary keys that should go on the route = .reject{|key, value| key.to_s.match(/.*_id/)} # We change the names, so 'format_type' becomes 'formatType' .each{|key, value| valid_opts[unrubify(key)] = value } # An array should become a string with comma separated values valid_opts.each{|key, value| valid_opts[key] = value.join(',') if value.is_a?(Array) } valid_opts end |
#request(method, options, conn_options) ⇒ Object
7 8 9 10 11 12 13 |
# File 'lib/uatu/connection.rb', line 7 def request(method, , ) conn = build_connection() conn_params = ().merge(mandatory_params()) conn_route = build_route(method, ) conn.get conn_route, conn_params end |
#unrubify(name) ⇒ Object
53 54 55 56 57 58 |
# File 'lib/uatu/connection.rb', line 53 def unrubify(name) key = name.to_s unrubified_key_array = key.split('_') unrubified_key_array[1..-1].each(&:capitalize!) unrubified_key_array.join.to_sym end |
#valid_method(method) ⇒ Object
60 61 62 63 64 |
# File 'lib/uatu/connection.rb', line 60 def valid_method(method) _method = method.split('_').first.pluralize raise 'InvalidMethod' unless %w(characters series creators comics events stories).include?(_method) _method end |