Class: Mysigner::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_url:, api_token:, user_email: nil) ⇒ Client

Returns a new instance of Client.



11
12
13
14
15
# File 'lib/mysigner/client.rb', line 11

def initialize(api_url:, api_token:, user_email: nil)
  @api_url = api_url
  @api_token = api_token
  @user_email = user_email
end

Instance Attribute Details

#api_tokenObject (readonly)

Returns the value of attribute api_token.



9
10
11
# File 'lib/mysigner/client.rb', line 9

def api_token
  @api_token
end

#api_urlObject (readonly)

Returns the value of attribute api_url.



9
10
11
# File 'lib/mysigner/client.rb', line 9

def api_url
  @api_url
end

#user_emailObject (readonly)

Returns the value of attribute user_email.



9
10
11
# File 'lib/mysigner/client.rb', line 9

def user_email
  @user_email
end

Instance Method Details

#connectionObject

Expose connection for direct access (e.g., binary downloads)



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/mysigner/client.rb', line 67

def connection
  @connection ||= Faraday.new(url: @api_url) do |f|
    # Request middleware
    f.request :authorization, 'Bearer', @api_token
    f.request :json

    # Add X-User-Email header if email is present
    f.headers['X-User-Email'] = @user_email if @user_email

    # Retry failed requests
    f.request :retry, {
      max: 3,
      interval: 0.5,
      interval_randomness: 0.5,
      backoff_factor: 2,
      retry_statuses: [429, 502, 503, 504],
      methods: %i[get post patch delete]
    }

    # Response middleware
    f.response :json, content_type: /\bjson$/
    # Don't use raise_error - we'll handle errors manually

    # Adapter
    f.adapter Faraday.default_adapter
  end
end

#delete(path) ⇒ Object

DELETE request



50
51
52
53
54
55
# File 'lib/mysigner/client.rb', line 50

def delete(path)
  response = connection.delete(path)
  handle_response(response)
rescue Faraday::Error => e
  handle_faraday_error(e)
end

#get(path, params: {}) ⇒ Object

GET request



18
19
20
21
22
23
24
25
# File 'lib/mysigner/client.rb', line 18

def get(path, params: {})
  response = connection.get(path) do |req|
    req.params = params
  end
  handle_response(response)
rescue Faraday::Error => e
  handle_faraday_error(e)
end

#patch(path, body: {}) ⇒ Object

PATCH request



39
40
41
42
43
44
45
46
47
# File 'lib/mysigner/client.rb', line 39

def patch(path, body: {})
  response = connection.patch(path) do |req|
    req.headers['Content-Type'] = 'application/json'
    req.body = body.to_json
  end
  handle_response(response)
rescue Faraday::Error => e
  handle_faraday_error(e)
end

#post(path, body: {}) ⇒ Object

POST request



28
29
30
31
32
33
34
35
36
# File 'lib/mysigner/client.rb', line 28

def post(path, body: {})
  response = connection.post(path) do |req|
    req.headers['Content-Type'] = 'application/json'
    req.body = body.to_json
  end
  handle_response(response)
rescue Faraday::Error => e
  handle_faraday_error(e)
end

#test_connectionObject

Test connection with status endpoint



58
59
60
61
62
63
64
# File 'lib/mysigner/client.rb', line 58

def test_connection
  get('/api/v1/status')
rescue ClientError => e
  raise e
rescue StandardError => e
  raise ConnectionError, "Failed to connect: #{e.message}"
end