Module: Mondavi::RequestRouter::ClassMethods
- Defined in:
- lib/mondavi/request_router.rb
Instance Method Summary collapse
- #component_index? ⇒ Boolean
- #ensure_service_document ⇒ Object
- #index? ⇒ Boolean
- #issue_external_request(http_verb: nil, url: nil, urn_path: nil) ⇒ Object
- #issue_local_request(http_verb: nil, urn_path: nil) ⇒ Object
-
#post(url_variables:, body:) ⇒ Object
I can’t make too many assumptions around “should” because we will probably break those assumptions immediately: hello there should be no id for a post.
-
#put(id:, url_variables:, body:) ⇒ Object
id should be included in the url_variables.
- #register_get(method, *declared_url_variables) ⇒ Object
- #service_document_expired? ⇒ Boolean
- #url(supplied_url_variables) ⇒ Object
- #urn_path(supplied_url_variables) ⇒ Object
Instance Method Details
#component_index? ⇒ Boolean
58 59 60 |
# File 'lib/mondavi/request_router.rb', line 58 def component_index? self.to_s.match(/::.*IndexApi$/).present? end |
#ensure_service_document ⇒ Object
151 152 153 154 155 156 157 |
# File 'lib/mondavi/request_router.rb', line 151 def ensure_service_document return if (index? || component_index?) if @@service_document.nil? || service_document_expired? @@service_document = IndexComponent::IndexApi.get end end |
#index? ⇒ Boolean
54 55 56 |
# File 'lib/mondavi/request_router.rb', line 54 def index? self == IndexComponent::IndexApi end |
#issue_external_request(http_verb: nil, url: nil, urn_path: nil) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/mondavi/request_router.rb', line 97 def issue_external_request(http_verb: nil, url: nil, urn_path: nil) conn = Faraday.new(:url => url) do |faraday| faraday.request :url_encoded # form-encode POST params faraday.response :logger # log requests to STDOUT faraday.adapter Faraday.default_adapter # make requests with Net::HTTP end # binding.pry # response = conn.get urn_path # response.body end |
#issue_local_request(http_verb: nil, urn_path: nil) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/mondavi/request_router.rb', line 109 def issue_local_request(http_verb: nil, urn_path: nil) env = {} env['rack.input'] = Puma::NullIO.new env['REQUEST_METHOD'] = http_verb env['REQUEST_PATH'] = urn_path env['REQUEST_URI'] = urn_path env['PATH_INFO'] = urn_path response = ApplicationApi.call(env) status = response[0] header = response[1] body = response[2].body.first begin extract_representer = self.to_s.gsub(/^.*::/, '').gsub('Api', 'Representer') representer = Object.const_get(extract_representer) struct = OpenStruct.new struct.extend(WineRepresenter) struct.from_json(body) rescue NameError => e # Representer doesn't exist, use Hashie::Mash Hashie::Mash.new(JSON.parse(JSON.load(body))) end end |
#post(url_variables:, body:) ⇒ Object
I can’t make too many assumptions around “should” because we will probably break those assumptions immediately: hello there should be no id for a post
140 141 142 143 |
# File 'lib/mondavi/request_router.rb', line 140 def post(url_variables:, body:) ensure_service_document end |
#put(id:, url_variables:, body:) ⇒ Object
id should be included in the url_variables
147 148 149 |
# File 'lib/mondavi/request_router.rb', line 147 def put(id:, url_variables:, body:) ensure_service_document end |
#register_get(method, *declared_url_variables) ⇒ Object
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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/mondavi/request_router.rb', line 9 def register_get(method, *declared_url_variables) define_singleton_method(method) do |**supplied_url_variables| @@service_document = nil # check parameters declared_url_variables.each do |required_url_variable| if !supplied_url_variables.keys.include?(required_url_variable) raise ArgumentError.new("key :#{required_url_variable} missing in call to :#{method} on #{self}") end end ensure_service_document is_local = IsComponentLocal.call(target_concept: self) if is_local # mock request and satisfy locally issue_local_request( http_verb: 'GET', urn_path: urn_path(supplied_url_variables) ) else # issue external request # binding.pry # todo # Request and LocalRequest # note to self: # if component_index => supplied_url_variables[:url] + [:urn_path] # will need to get the url from svc-doc issue_external_request( http_verb: 'GET', url: url(supplied_url_variables), urn_path: urn_path(supplied_url_variables) ) end end # not sure why I need this in pact_router # self.def_delegator self, method, method end |
#service_document_expired? ⇒ Boolean
159 160 161 162 |
# File 'lib/mondavi/request_router.rb', line 159 def service_document_expired? return true unless @service_document Time.parse(@@service_document.requested_at) < (Time.now - 24.hours) end |
#url(supplied_url_variables) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/mondavi/request_router.rb', line 81 def url(supplied_url_variables) return "https://chi-winery.herokuapp.com" if index? return supplied_url_variables[:url] if component_index? details = @@service_document.concepts[self.to_s] url = details.url_template # look at each_with_object # maybe we change from url_variable.. to request or 'uri' supplied_url_variables.each do |url_variable_key, url_variable_value| url.gsub!(":#{url_variable_key}", url_variable_value.to_s) end url end |
#urn_path(supplied_url_variables) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/mondavi/request_router.rb', line 62 def urn_path(supplied_url_variables) return "/" if index? return supplied_url_variables[:urn_path] if component_index? # @@service_document should never have a representer # so it will just be a hashie::mash details = @@service_document.concepts[self.to_s] details.url_template path = details.urn_path_template # look at each_with_object supplied_url_variables.each do |url_variable_key, url_variable_value| path.gsub!(":#{url_variable_key}", url_variable_value.to_s) end path end |