Class: Lib

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key = "") ⇒ Lib

Initializes a Lib object with an optional API key.



11
12
13
14
15
16
# File 'lib/mslm/lib.rb', line 11

def initialize(api_key = "")
    @api_key = api_key
    @http = nil
    @base_url = URI.parse(Mslm::Mslm::BASE_URL)
    @user_agent = self.class.get_user_agent('mslm')
end

Instance Attribute Details

#api_keyObject

Generic utility class for handling HTTP requests and responses.



8
9
10
# File 'lib/mslm/lib.rb', line 8

def api_key
  @api_key
end

#base_urlObject

Generic utility class for handling HTTP requests and responses.



8
9
10
# File 'lib/mslm/lib.rb', line 8

def base_url
  @base_url
end

#httpObject

Generic utility class for handling HTTP requests and responses.



8
9
10
# File 'lib/mslm/lib.rb', line 8

def http
  @http
end

#user_agentObject

Generic utility class for handling HTTP requests and responses.



8
9
10
# File 'lib/mslm/lib.rb', line 8

def user_agent
  @user_agent
end

Class Method Details

.get_user_agent(pkg) ⇒ Object

Static method to generate a user agent string.



39
40
41
# File 'lib/mslm/lib.rb', line 39

def self.get_user_agent(pkg)
    "#{pkg}/ruby/#{Mslm::VERSION}"
end

Instance Method Details

#prepare_url(url_path, query_params, opt) ⇒ Object

Prepares the URL for making a request.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/mslm/lib.rb', line 44

def prepare_url(url_path, query_params, opt)
    query_params['apikey'] = opt.api_key

    target_url = @base_url.dup
    target_url.path = url_path
    query_params_str = URI.encode_www_form(query_params)

    target_url.query = if target_url.query
                    "#{target_url.query}&#{query_params_str}"
                else
                    query_params_str
                end
    target_url
end

#req_and_resp(target_url, opt, method = 'GET', data = nil) ⇒ Object

Makes an HTTP request and returns the response.



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/mslm/lib.rb', line 60

def req_and_resp(target_url, opt, method = 'GET', data = nil)
    uri = URI.parse(target_url)
    @http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = true if uri.scheme == 'https'
    headers = {
        'User-Agent' => @user_agent,
    }
    
    if method.upcase == 'GET'
    response = @http.get(uri.request_uri,headers)
    elsif method.upcase == 'POST'
    headers['Content-Type'] = 'application/json'
    response = @http.post(uri.request_uri,data,headers)
    else
    raise ArgumentError, 'Invalid HTTP method. Supported methods are GET and POST.'
    end

    response
end

#set_api_key(api_key) ⇒ Object

Sets the API key for authentication.



34
35
36
# File 'lib/mslm/lib.rb', line 34

def set_api_key(api_key)
    @api_key = api_key
end

#set_base_url(base_url_str) ⇒ Object

Sets the base URL for API requests.



24
25
26
# File 'lib/mslm/lib.rb', line 24

def set_base_url(base_url_str)
    @base_url = URI.parse(base_url_str)
end

#set_http_client(http_client) ⇒ Object

Sets the HTTP client for making requests.



19
20
21
# File 'lib/mslm/lib.rb', line 19

def set_http_client(http_client)
    @http = http_client
end

#set_user_agent(user_agent) ⇒ Object

Sets the user agent for HTTP requests.



29
30
31
# File 'lib/mslm/lib.rb', line 29

def set_user_agent(user_agent)
    @user_agent = user_agent
end