Class: Pike13::HTTPClient

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/pike13/http_client.rb

Overview

HTTParty-based HTTP client for Pike13 API Provides HTTP methods with error handling

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_url:, access_token:) ⇒ HTTPClient

Returns a new instance of HTTPClient.



14
15
16
17
# File 'lib/pike13/http_client.rb', line 14

def initialize(base_url:, access_token:)
  @base_url = base_url
  @access_token = access_token
end

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



12
13
14
# File 'lib/pike13/http_client.rb', line 12

def access_token
  @access_token
end

#base_urlObject (readonly)

Returns the value of attribute base_url.



12
13
14
# File 'lib/pike13/http_client.rb', line 12

def base_url
  @base_url
end

Instance Method Details

#delete(path, params = {}) ⇒ Hash

DELETE request

Parameters:

  • path (String)

    The API endpoint path

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

    Query parameters

Returns:

  • (Hash)

    Parsed response body



94
95
96
97
98
99
100
101
102
103
# File 'lib/pike13/http_client.rb', line 94

def delete(path, params = {})
  handle_response do
    self.class.delete(
      full_path(path),
      query: params,
      headers: headers,
      timeout: 30
    )
  end
end

#get(path, params = {}) ⇒ Hash, Array

GET request

Parameters:

  • path (String)

    The API endpoint path

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

    Query parameters

Returns:

  • (Hash, Array)

    Parsed response body



24
25
26
27
28
29
30
31
32
33
# File 'lib/pike13/http_client.rb', line 24

def get(path, params = {})
  handle_response do
    self.class.get(
      full_path(path),
      query: params,
      headers: headers,
      timeout: 30
    )
  end
end

#patch(path, body = {}, params = {}) ⇒ Hash

PATCH request

Parameters:

  • path (String)

    The API endpoint path

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

    Request body

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

    Query parameters

Returns:

  • (Hash)

    Parsed response body



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/pike13/http_client.rb', line 77

def patch(path, body = {}, params = {})
  handle_response do
    self.class.patch(
      full_path(path),
      body: body.to_json,
      query: params,
      headers: headers,
      timeout: 30
    )
  end
end

#post(path, body = {}, params = {}) ⇒ Hash

POST request

Parameters:

  • path (String)

    The API endpoint path

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

    Request body

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

    Query parameters

Returns:

  • (Hash)

    Parsed response body



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/pike13/http_client.rb', line 41

def post(path, body = {}, params = {})
  handle_response do
    self.class.post(
      full_path(path),
      body: body.to_json,
      query: params,
      headers: headers,
      timeout: 30
    )
  end
end

#put(path, body = {}, params = {}) ⇒ Hash

PUT request

Parameters:

  • path (String)

    The API endpoint path

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

    Request body

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

    Query parameters

Returns:

  • (Hash)

    Parsed response body



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/pike13/http_client.rb', line 59

def put(path, body = {}, params = {})
  handle_response do
    self.class.put(
      full_path(path),
      body: body.to_json,
      query: params,
      headers: headers,
      timeout: 30
    )
  end
end