Module: OBarc::Api

Extended by:
Api
Included in:
Api
Defined in:
lib/obarc/api.rb

Constant Summary

DEFAULT_TIMEOUT =
60 * 60 * 1
VALID_ACTIONS =
{
  get: %i(image profile listings followers following contracts shutdown
    settings connected_peers routing_table notifications chat_messages
    chat_conversations sales purchases order cases order_messages ratings
    btc_price),
  post: %i(login follow unfollow profile social_accounts
    contract make_moderator unmake_moderator purchase_contract
    confirm_order upload_image complete_order settings
    mark_notification_as_read broadcast mark_chat_message_as_read
    check_for_payment dispute_contract close_dispute release_funds refund
    mark_discussion_as_read),
  delete: %i(social_accounts contract chat_conversation)
}.freeze

Instance Method Summary (collapse)

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

- (Object) method_missing(m, *args, &block)



55
56
57
58
59
60
61
62
63
64
65
66
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
94
# File 'lib/obarc/api.rb', line 55

def method_missing(m, *args, &block)
  super unless respond_to_missing?(m)
  
  # Many of the calls to restapi.py are uniform enough for DRY code, but the
  # ones that aren't are mapped here.
  
  rest_method, endpoint, params, options = case m
  when :get_image then [:get, 'get_image', args[0], args[1]]
  when :get_listings then [:get, 'get_listings', args[0], args[1]]
  when :get_followers then [:get, 'get_followers', args[0], args[1]]
  when :get_following then [:get, 'get_following', args[0], args[1]]
  when :post_contract then [:post, 'contracts', args[0], args[1]]
  when :delete_contract then [:delete, 'contracts', args[0], args[1]]
  when :get_notifications then [:get, 'get_notifications', nil, args[0]]
  when :get_chat_conversations then [:get, 'get_chat_conversations', nil, args[0]]
  when :get_sales then [:get, 'get_sales', nil, args[0]]
  when :get_purchases then [:get, 'get_purchases', nil, args[0]]
  when :get_cases then [:get, 'get_cases', nil, args[0]]
  when :get_ratings then [:get, 'get_ratings', args[0], args[1]]
  else
    verb = m.to_s.split('_')
    a = [verb[0].to_sym, verb[1..-1].join('_')]
    a << if args.size == 1
      nil
    else
      args[0]
    end
    a << args[args.size - 1]
  end
  
  url = "#{build_base_url(options)}/#{endpoint}" if !!endpoint && !!options
  if !!rest_method && !!url && !!options
    headers = build_headers(options)
    headers = headers.merge(params: params.compact) if !!params
    return execute method: rest_method, url: url, headers: headers,
      verify_ssl: build_verify_ssl(options)
  end
      
  raise Utils::Exceptions::OBarcError, "Did not handle #{m} as expected, arguments: #{args}"
end

Instance Method Details

- (Object) get_chat_messages(chat_messages, options = {})

GET api/v1/get_chat_messages



126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/obarc/api.rb', line 126

def get_chat_messages(chat_messages, options = {})
  guid = chat_messages[:guid]
  
  if guid.nil? || guid.size != 40
    raise Utils::Exceptions::InvalidArgumentError, guid: "must be present, 40 characters (was: #{guid.inspect})"
  end
  
  url = "#{build_base_url(options)}/get_chat_messages"
  execute method: :get, url: url,
    headers: build_headers(options).merge(params: chat_messages.compact),
    verify_ssl: build_verify_ssl(options)
end

- (Object) get_contracts(contracts, options = {})

GET api/v1/contracts



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/obarc/api.rb', line 109

def get_contracts(contracts, options = {})
  id = contracts[:id]
  guid = contracts[:guid]
  
  if !!id && id.size != 40
    raise Utils::Exceptions::InvalidArgumentError, id: "must be 40 characters, if present (was: #{id.size})"
  elsif !!guid && guid.size != 40
    raise Utils::Exceptions::InvalidArgumentError, guid: "must be 40 characters, if present (was: #{guid.size})"
  end
  
  url = "#{build_base_url(options)}/contracts"
  execute method: :get, url: url,
    headers: build_headers(options).merge(params: contracts.compact),
    verify_ssl: build_verify_ssl(options)
end

- (Object) get_order(order, options = {})

GET api/v1/get_order



140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/obarc/api.rb', line 140

def get_order(order, options = {})
  order_id = order[:order_id]
  
  if order_id.nil?
    raise Utils::Exceptions::InvalidArgumentError, order_id: 'must be present'
  end
  
  url = "#{build_base_url(options)}/get_order"
  execute method: :get, url: url,
    headers: build_headers(options).merge(params: order.compact),
    verify_ssl: build_verify_ssl(options)
end

- (Object) ping(options = {})



38
39
40
41
42
# File 'lib/obarc/api.rb', line 38

def ping(options = {})
  execute(method: :get,
    url: "#{build_base_url(options)}/connected_peers?_=#{Time.now.to_i}",
    headers: build_headers(options), verify_ssl: build_verify_ssl(options))
end

- (Object) post_login(options = {})

POST api/v1/login



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

def (options = {})
  url = "#{build_base_url(options)}/login"
  auth = build_authentication(options)
  
  raise Utils::Exceptions::MissingArgumentError, [:username, :password] if auth.empty?

  execute method: :post, url: url, headers: {params: auth},
    verify_ssl: build_verify_ssl(options)
end

- (Object) post_upload_image(options = {})

POST api/v1/upload_image



97
98
99
100
101
102
103
104
105
106
# File 'lib/obarc/api.rb', line 97

def post_upload_image(options = {})
  elements = [:image, :avatar, :header]
  params = options.slice(*elements)
  options = options.delete_if { |k, v| elements.include? k }
  
  url = "#{build_base_url(options)}/upload_images"
  execute method: :post, url: url,
    headers: build_headers(options).merge(params: params.compact),
    verify_ssl: build_verify_ssl(options)
end

- (Boolean) respond_to_missing?(m, include_private = false)

Returns:

  • (Boolean)


44
45
46
47
48
49
50
51
52
53
# File 'lib/obarc/api.rb', line 44

def respond_to_missing?(m, include_private = false)
  verb = m.to_s.split('_')
  rest_method = verb[0].to_sym
  return false unless VALID_ACTIONS.keys.include?(rest_method)
  
  endpoint = verb[1..-1].join('_')
  return false if endpoint.nil?
  
  VALID_ACTIONS[rest_method].include?(endpoint.to_sym)
end