Class: Yp::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/base.rb

Direct Known Subclasses

Sale

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(signature_key, **params) ⇒ Base



6
7
8
9
# File 'lib/base.rb', line 6

def initialize(signature_key, **params)
  @params = params.merge default_params
  @signature_key = signature_key
end

Instance Attribute Details

#paramsObject (readonly)

Returns the value of attribute params.



4
5
6
# File 'lib/base.rb', line 4

def params
  @params
end

Class Method Details

.digest(str_params) ⇒ Object



37
38
39
# File 'lib/base.rb', line 37

def digest(str_params)
  Digest::SHA512.hexdigest str_params
end

.parse(response) ⇒ Object



45
46
47
# File 'lib/base.rb', line 45

def parse(response)
  ruby_hash_from_response(CGI::parse(response))
end

.serialize_params(params) ⇒ Object



41
42
43
# File 'lib/base.rb', line 41

def serialize_params(params)
  uri_string_from_hash(params)
end

Instance Method Details

#bodyObject



11
12
13
# File 'lib/base.rb', line 11

def body
  @params.clone.tap { |params|  params[:signature] = create_signing_hash }
end

#create_signing_hashObject



15
16
17
# File 'lib/base.rb', line 15

def create_signing_hash
  self.class.digest(self.class.serialize_params(@params) + @signature_key)
end

#sendObject



19
20
21
22
23
24
25
26
27
# File 'lib/base.rb', line 19

def send
  if block_given?
    RestClient.post(URL, body) do |response|
      yield(self.class.parse response)
    end
  else
    self.class.parse(RestClient.post(URL, body))
  end
end