Class: SunlightApi::UriGenerator

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(public_key, private_key, test, uri_suffix, get_params = {}) ⇒ UriGenerator

Returns a new instance of UriGenerator.



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/sunlight_api.rb', line 73

def initialize( public_key, private_key, test, uri_suffix, get_params={} )
  @public_key = public_key
  @private_key = private_key
  @test = test
  if @test
    @base_url = "https://hortservices.sunlightsupply.com/v1/#{uri_suffix}?"
  else
    @base_url = "https://services.sunlightsupply.com/v1/#{uri_suffix}?"
  end
  @get_params = get_params
  @time_stamp = Time.now.utc.strftime("%Y-%m-%dT%H:%M:%SZ")
  append_get_params
  append_api_key
  append_time_stamp
  append_signature
end

Instance Attribute Details

#urlObject (readonly)

Returns the value of attribute url.



72
73
74
# File 'lib/sunlight_api.rb', line 72

def url
  @url
end

Instance Method Details

#append_api_keyObject



97
98
99
100
# File 'lib/sunlight_api.rb', line 97

def append_api_key
  @base_url += "&" if @get_params.size > 0
  @base_url += "X-ApiKey=#{@public_key}"
end

#append_get_paramsObject



90
91
92
93
94
95
# File 'lib/sunlight_api.rb', line 90

def append_get_params
  @get_params.each_with_index do |(key,value), i|
    @base_url += "#{key}=#{value}"
    @base_url += "&" unless i == (@get_params.size - 1)
  end
end

#append_signatureObject



106
107
108
109
110
# File 'lib/sunlight_api.rb', line 106

def append_signature
  @digest  = OpenSSL::Digest::Digest.new('sha256')
  @signature = OpenSSL::HMAC.hexdigest(@digest, @private_key, @base_url).upcase
  @url = "#{@base_url}&signature=#{@signature}"
end

#append_time_stampObject



102
103
104
# File 'lib/sunlight_api.rb', line 102

def append_time_stamp
  @base_url += "&time=#{@time_stamp}"
end