Module: HttpStore::Helpers::Requestable

Included in:
Client
Defined in:
lib/http_store/helpers/requestable.rb

Instance Method Summary collapse

Instance Method Details

#format_requestObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/http_store/helpers/requestable.rb', line 32

def format_request
  @meta.http_method = get? ? 'GET' : 'POST' # only support get/post

  @meta.headers                ||= { charset: 'UTF-8' }
  @meta.headers[:content_type] = :json if json_request?

  @meta.query_params ||= {}
  @meta.data         ||= {}

  @meta.requestable_id   = requestable.try(:id)
  @meta.requestable_type = requestable.try(:class).try(:to_s)
  @meta.client_type      = self.class.to_s
  @meta.force            = @meta.other_params.delete(:force) || false
  @meta.request_digest   = gen_request_digest

  @meta.request_valid = request_valid?
end

#gen_request_digestObject



55
56
57
# File 'lib/http_store/helpers/requestable.rb', line 55

def gen_request_digest
  Digest::SHA1.hexdigest(storable(request_digest_hash).to_json)
end

#get?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/http_store/helpers/requestable.rb', line 24

def get?
  http_method.to_s.casecmp('get').zero?
end

#json_request?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/http_store/helpers/requestable.rb', line 14

def json_request?
  data_type.to_s.casecmp('json').zero?
end

#post?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/http_store/helpers/requestable.rb', line 28

def post?
  http_method.to_s.casecmp('post').zero?
end

#request_digest_hashObject

http_method url data_type data other_params requestable_id requestable_type



51
52
53
# File 'lib/http_store/helpers/requestable.rb', line 51

def request_digest_hash
  @meta.slice(*HttpStore::DIGEST_KEYS)
end

#request_valid?Boolean

you need rewrite this checker, when return false the request don’t send

Returns:

  • (Boolean)


10
11
12
# File 'lib/http_store/helpers/requestable.rb', line 10

def request_valid?
  true
end

#set_requestObject

will return one hash to set the request meta



5
6
7
# File 'lib/http_store/helpers/requestable.rb', line 5

def set_request
  {}
end

#uriObject



18
19
20
21
22
# File 'lib/http_store/helpers/requestable.rb', line 18

def uri
  return url if query_params.blank?

  "#{url}?#{query_params.to_query}"
end