Class: ResasKit::Client

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

Overview

Client for the RESAS API

Constant Summary collapse

API_ENDPOINT =
'https://opendata.resas-portal.go.jp'.freeze
API_VERSION =
'v1'.freeze
USER_AGENT =
"ResasKit Ruby Gem #{ResasKit::VERSION}".freeze
METHOD_PREFIX =
'get_'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Initialize a new Client object with given options

Parameters:

  • options (Hash) (defaults to: {})

    Initialize options

Options Hash (options):

  • :api_key (String)

    RESAS API key

  • :api_version (String)

    RESAS API version



24
25
26
27
28
29
30
31
# File 'lib/resas_kit/client.rb', line 24

def initialize(options = {})
  @api_key     = ENV['RESAS_API_KEY']
  @api_version = ENV['RESAS_API_VERSION']

  options.each do |key, value|
    instance_variable_set(:"@#{key}", value)
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, params = {}) ⇒ Object (private)



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/resas_kit/client.rb', line 73

def method_missing(method, params = {})
  if respond_to?(method)
    path = method.to_s
      .sub(/\A#{METHOD_PREFIX}/, '')
      .gsub(/__/, '/')
      .gsub(/_(.)/) { Regexp.last_match(1).camelize }
    get(path, params)
  else
    super
  end
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



17
18
19
# File 'lib/resas_kit/client.rb', line 17

def api_key
  @api_key
end

#api_versionObject

Returns the value of attribute api_version.



17
18
19
# File 'lib/resas_kit/client.rb', line 17

def api_version
  @api_version
end

Instance Method Details

#get(path, params = {}) ⇒ ResasKit::Response

Make a HTTP GET request

Parameters:

  • path (String)

    Path for request

  • params (Hash) (defaults to: {})

    Request parameters

Returns:



38
39
40
# File 'lib/resas_kit/client.rb', line 38

def get(path, params = {})
  request(:get, path, params)
end