Class: Ramekin::APIEndpoint

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

Constant Summary collapse

BASE =
'https://www.smwcentral.net/ajax.php'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**params) ⇒ APIEndpoint

Returns a new instance of APIEndpoint.



9
10
11
12
# File 'lib/ramekin/sample_pack.rb', line 9

def initialize(**params)
  @params = {}
  params.each { |k, v| self[k] = v }
end

Class Method Details

.get!(**params) ⇒ Object



14
15
16
17
18
# File 'lib/ramekin/sample_pack.rb', line 14

def self.get!(**params)
  resp = URI.open(new(**params).to_uri)
  binding.pry unless resp.status == %w(200 OK)
  JSON.parse(resp.read)
end

Instance Method Details

#[]=(k, v) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/ramekin/sample_pack.rb', line 20

def []=(k, v)
  case v
  when Hash
    v.each do |subk, subv|
      self["#{k}[#{subk}]"] = subv
    end
  when Array
    v.each_with_index do |subv, i|
      self["#{k}[#{i}]"] = subv
    end
  when String, Numeric
    @params[k] = v
  end
end

#to_sObject



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

def to_s
  to_uri.to_s
end

#to_uriObject



35
36
37
38
39
# File 'lib/ramekin/sample_pack.rb', line 35

def to_uri
  URI(BASE).tap do |uri|
    uri.query = URI.encode_www_form(@params)
  end
end